Skip to content

Commit

Permalink
getOurTagIdFromSystemTagManager
Browse files Browse the repository at this point in the history
  • Loading branch information
michielbdejong committed Dec 21, 2023
1 parent 46d7328 commit becba17
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ private static function castObjectType($type)
return $type;
}

private static function getOurTagId(){
public static function getOurTagIdFromSystemTagManager($systemTagManager){
try{
$tags = $this->systemTagManager->getAllTags(
$tags = $systemTagManager->getAllTags(
null,
self::TAG_NAME
);

if(count($tags) < 1){
$tag = $this->systemTagManager->createTag(self::TAG_NAME, false, false);
$tag = $systemTagManager->createTag(self::TAG_NAME, false, false);
} else {
$tag = current($tags);
}
Expand Down
32 changes: 7 additions & 25 deletions lib/Controller/MfazonesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,16 @@ public function get($source)
try {
$userRoot = $this->rootFolder->getUserFolder($this->userId);
$node = $userRoot->get($source);
$tag = Application::getOurTagId();
if ($tag === false) {
$tagId = Application::getOurTagIdFromSystemTagManager($this->systemTagManager);
if ($tagId === false) {
error_log('A server admin should log in so the MFA Zone tag and flow can be created.');
return new JSONResponse(
array(
'error' => 'A server admin should log in so the MFA Zone tag and flow can be created'
)
);
}
$tagId = $tag->getId();
$type = $this->castObjectType($node->getType());
$type = Application::castObjectType($node->getType());
$result = $this->tagMapper->haveTag($node->getId(), $type, $tagId);

return new JSONResponse(
Expand Down Expand Up @@ -173,7 +172,7 @@ public function getList($nodeIds)
$results = [];
foreach($nodeIds as $nodeId) {
$node = $userRoot->getById($nodeId);
$type = $this->castObjectType($node->getType());
$type = Application::castObjectType($node->getType());
$results[$nodeId] = $this->tagMapper->haveTag($nodeId, $type, $tagId);
}

Expand Down Expand Up @@ -211,22 +210,16 @@ public function set($source, $protect)
if ($node->getType() !== 'dir') {
return new DataResponse(['not a directory'], Http::STATUS_FORBIDDEN);
}
$tags = $this->systemTagManager->getAllTags(
null,
Application::TAG_NAME
);
$tag = current($tags);
if ($tag === false) {
$tagId = Application::getOurTagIdFromSystemTagManager($this->systemTagManager);
if ($tagId === false) {
error_log('A server admin should log in so the MFA Zone tag and flow can be created.');
return new JSONResponse(
array(
'error' => 'A server admin should log in so the MFA Zone tag and flow can be created'
)
);
}
$tagId = $tag->getId();

$type = $this->castObjectType($node->getType());
$type = Application::castObjectType($node->getType());

if ($protect === "true") {
$this->tagMapper->assignTags($node->getId(), $type, $tagId);
Expand Down Expand Up @@ -254,15 +247,4 @@ public function access($source)
)
);
}

private function castObjectType($type)
{
if ($type === 'file') {
return "files";
}
if ($type === "dir") {
return "files";
}
return $type;
}
}
7 changes: 6 additions & 1 deletion lib/MFAPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
use Sabre\HTTP\ResponseInterface;

class MFAPlugin extends ServerPlugin {
/** @var ISystemTagManager */
private $sytemTagMapper;

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

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

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

Expand All @@ -35,7 +40,7 @@ public function initialize(Server $server) {

public function propFind(PropFind $propFind, INode $node): void {
$propFind->handle(self::ATTR_NAME, function() {
$tagId = Application::getOurTagId();
$tagId = Application::getOurTagIdFromSystemTagManager($this->systemTagManager);
if ($tagId === false) {
return false;
}
Expand Down

0 comments on commit becba17

Please sign in to comment.