Skip to content

Commit

Permalink
Fix tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mickenordin committed Aug 26, 2024
1 parent 1bf117b commit f323a7b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 75 deletions.
64 changes: 13 additions & 51 deletions mfazones/lib/Controller/MfazonesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,23 @@
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\ISession;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use Psr\Log\LoggerInterface;

class MfazonesController extends Controller
{
/** @var IRootFolder */
private $rootFolder;

/** @var string */
private $userId;

/** @var ISession */
protected $session;

/** @var ISystemTagManager */
protected ISystemTagManager $systemTagManager;

/** @var IGroupManager */
private $groupManager;

/** @var ISystemTagObjectMapper */
private $tagMapper;

/** @var MfaVerified */
private $mfaVerified;

/** @var LoggerInterface */
private $logger;

public function __construct(
IRequest $request,
IRootFolder $rootFolder,
IGroupManager $groupManager,
string $userId,
ISession $session,
ISystemTagObjectMapper $tagMapper,
ISystemTagManager $systemTagManager,
MfaVerified $mfaVerified,
LoggerInterface $logger
private IRequest $request,
private IRootFolder $rootFolder,
private IGroupManager $groupManager,
private string $userId,
private ISession $session,
private Utils $utils,
private ISystemTagObjectMapper $tagMapper,
private MfaVerified $mfaVerified,
private LoggerInterface $logger
) {
parent::__construct(Application::APP_ID, $request);
$this->rootFolder = $rootFolder;
$this->userId = $userId;
$this->groupManager = $groupManager;
$this->tagMapper = $tagMapper;
$this->session = $session;
$this->systemTagManager = $systemTagManager;
$this->mfaVerified = $mfaVerified;
$this->logger = $logger;
}

private function castObjectType($type): string
Expand Down Expand Up @@ -133,7 +100,7 @@ public function get(): JSONResponse
try {
$userRoot = $this->rootFolder->getUserFolder($this->userId);
$node = $userRoot->get($source);
$tagId = Utils::getOurTagIdFromSystemTagManager($this->systemTagManager);
$tagId = $this->utils->getTagId();
if ($tagId === '') {
$this->logger->error('The MFA Zone tag and flow has not been created, which should happen on app enable.');
return new JSONResponse(
Expand Down Expand Up @@ -174,20 +141,15 @@ public function getList($nodeIds): JSONResponse
{
try {
$userRoot = $this->rootFolder->getUserFolder($this->userId);
$tags = $this->systemTagManager->getAllTags(
null,
Utils::TAG_NAME
);
$tag = current($tags);
if ($tag === false) {
$tagId = $this->utils->getTagId();
if ($tagId === '') {
$this->logger->error('The MFA Zone tag and flow has not been created, which should happen on app enable.');
return new JSONResponse(
array(
'error' => 'The MFA Zone tag and flow has not been created, which should happen on app enable.'
)
);
}
$tagId = $tag->getId();
$results = [];
foreach ($nodeIds as $nodeId) {
$node = $userRoot->getById($nodeId);
Expand Down Expand Up @@ -230,7 +192,7 @@ public function set($source, $protect)
if ($node->getType() !== 'dir') {
return new DataResponse(['not a directory'], Http::STATUS_FORBIDDEN);
}
$tagId = Utils::getOurTagIdFromSystemTagManager($this->systemTagManager);
$tagId = $this->utils->getTagId();
if ($tagId === '') {
$this->logger->error('The MFA Zone tag and flow has not been created, which should happen on app enable.');
return new JSONResponse(
Expand Down
6 changes: 3 additions & 3 deletions mfazones/lib/Listeners/AppDisableEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AppDisableEventListener implements IEventListener
{
public function __construct(
private IDBConnection $connection,
private ISystemTagManager $systemTagManager,
private Utils $utils,
private LoggerInterface $logger,
private Manager $manager
) {
Expand All @@ -70,7 +70,7 @@ public function handle(Event $event): void

$this->logger->debug("MFA: removing flow.");

$tagId = Utils::getOurTagIdFromSystemTagManager($this->systemTagManager); // will create the tag if necessary
$tagId = $this->utils->getTagId(); // will create the tag if necessary

try {

Expand All @@ -97,7 +97,7 @@ public function handle(Event $event): void
$this->manager->deleteOperation($operationId, $context);
$this->deleteCheckById($mfaVerifiedId);
$this->deleteCheckById($fileSystemTagsId);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->logger->error('MFA: Error when removing flow on disabling mfazones app', ['exception' => $e]);
}
}
Expand Down
5 changes: 2 additions & 3 deletions mfazones/lib/Listeners/AppEnableEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use OCP\App\Events\AppEnableEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\SystemTag\ISystemTagManager;
use OCP\WorkflowEngine\IManager;
use Psr\Log\LoggerInterface;

Expand All @@ -45,7 +44,7 @@ class AppEnableEventListener implements IEventListener
{
public function __construct(
private Manager $manager,
private ISystemTagManager $systemTagManager,
private Utils $utils,
private LoggerInterface $logger
) {
}
Expand All @@ -67,7 +66,7 @@ public function handle(Event $event): void
$this->logger->debug("MFA: setting up flow.");


$tagId = Utils::getOurTagIdFromSystemTagManager($this->systemTagManager); // will create the tag if necessary
$tagId = $this->utils->getTagId(); // will create the tag if necessary

$context = new ScopeContext(IManager::SCOPE_ADMIN);
$class = "OCA\\FilesAccessControl\\Operation";
Expand Down
13 changes: 3 additions & 10 deletions mfazones/lib/MFAPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@

use OCA\DAV\Connector\Sabre\Node;
use OCA\mfazones\Utils;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;

class MFAPlugin extends ServerPlugin
{
private ISystemTagManager $systemTagManager;
private ISystemTagObjectMapper $tagMapper;

public const ATTR_NAME = '{http://nextcloud.org/ns}requires-mfa';

public function __construct(
ISystemTagManager $systemTagManager,
ISystemTagObjectMapper $tagMapper
private Utils $utils,
private ISystemTagObjectMapper $tagMapper
) {
$this->systemTagManager = $systemTagManager;
$this->tagMapper = $tagMapper;
}

public function initialize(Server $server)
Expand All @@ -39,8 +33,7 @@ public function initialize(Server $server)
public function propFind(PropFind $propFind, Node $node): void
{
$propFind->handle(self::ATTR_NAME, function () use (&$node) {
$systemTagManager = $this->systemTagManager;
$tagId = Utils::getOurTagIdFromSystemTagManager($systemTagManager);
$tagId = $this->utils->getTagId();
if ($tagId === '') {
return false;
}
Expand Down
20 changes: 12 additions & 8 deletions mfazones/lib/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\mfazones;

use OCP\SystemTag\ISystemTagManager;

class Utils {
class Utils
{
public const TAG_NAME = 'mfazone';

public function __construct(
private ISystemTagManager $systemTagManager
) {}
/**
* @param ISystemTagManager $systemTagManager
* @return string
*/
public static function getOurTagIdFromSystemTagManager($systemTagManager)
* @return string
*/
public function getTagId()
{
try {
$tags = $systemTagManager->getAllTags();
$tags = $$this->systemTagManager->getAllTags();
foreach ($tags as $tag) {
if ($tag->getName() === self::TAG_NAME) {
return (string) $tag->getId();
Expand All @@ -30,8 +34,8 @@ public static function getOurTagIdFromSystemTagManager($systemTagManager)
$uservisible = false;
// But we want it to be restricted so the user can not escape it.
$userassignable = false;
$tag = $systemTagManager->createTag(self::TAG_NAME, $uservisible, $userassignable);
return (string) $tag->getId();
$tag = $this->systemTagManager->createTag(self::TAG_NAME, $uservisible, $userassignable);
return $tag->getId();
} catch (\Exception) {
return '';
}
Expand Down

0 comments on commit f323a7b

Please sign in to comment.