Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion apps/dav/lib/SystemTag/SystemTagPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCA\DAV\Connector\Sabre\Node;
use OCP\AppFramework\Http;
use OCP\Constants;
use OCP\Files\IRootFolder;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
Expand Down Expand Up @@ -68,7 +70,8 @@ public function __construct(
protected ISystemTagManager $tagManager,
protected IGroupManager $groupManager,
protected IUserSession $userSession,
private ISystemTagObjectMapper $tagMapper,
protected IRootFolder $rootFolder,
protected ISystemTagObjectMapper $tagMapper,
) {
}

Expand Down Expand Up @@ -387,6 +390,11 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
}

if (isset($props[self::OBJECTIDS_PROPERTYNAME])) {
$user = $this->userSession->getUser();
if (!$user) {
throw new Forbidden('You don’t have permissions to update tags');
}

$propValue = $props[self::OBJECTIDS_PROPERTYNAME];
if (!$propValue instanceof SystemTagsObjectList || count($propValue->getObjects()) === 0) {
throw new BadRequest('Invalid object-ids property');
Expand All @@ -399,10 +407,35 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
throw new BadRequest('Invalid object-ids property. All object types must be of the same type: ' . $node->getName());
}

// Only files are supported at the moment
// Also see SystemTagsRelationsCollection file
if ($objectTypes[0] !== 'files') {
throw new BadRequest('Invalid object-ids property type. Only files are supported');
}

// Get all current tagged objects
$taggedObjects = $this->tagMapper->getObjectIdsForTags([$node->getSystemTag()->getId()], 'files');
$toAddObjects = array_map(fn ($value) => (string)$value, array_keys($objects));

// Compute the tags to add and remove
$addedObjects = array_values(array_diff($toAddObjects, $taggedObjects));
$removedObjects = array_values(array_diff($taggedObjects, $toAddObjects));

// Check permissions for each object to be freshly tagged or untagged
if (!$this->canUpdateTagForFileIds(array_merge($addedObjects, $removedObjects))) {
throw new Forbidden('You don’t have permissions to update tags');
}

$this->tagMapper->setObjectIdsForTag($node->getSystemTag()->getId(), $node->getName(), array_keys($objects));
}

if ($props[self::OBJECTIDS_PROPERTYNAME] === null) {
// Check the user have permissions to remove the tag from all currently tagged objects
$taggedObjects = $this->tagMapper->getObjectIdsForTags([$node->getSystemTag()->getId()], 'files');
if (!$this->canUpdateTagForFileIds($taggedObjects)) {
throw new Forbidden('You don’t have permissions to update tags');
}

$this->tagMapper->setObjectIdsForTag($node->getSystemTag()->getId(), $node->getName(), []);
}

Expand Down Expand Up @@ -483,4 +516,24 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
return true;
});
}

/**
* Check if the user can update the tag for the given file ids
*
* @param list<string> $fileIds
* @return bool
*/
private function canUpdateTagForFileIds(array $fileIds): bool {
$user = $this->userSession->getUser();
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
foreach ($fileIds as $fileId) {
$nodes = $userFolder->getById((int)$fileId);
foreach ($nodes as $node) {
if (($node->getPermissions() & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE) {
return true;
}
}
}
return false;
}
}
7 changes: 6 additions & 1 deletion apps/dav/lib/SystemTag/SystemTagsObjectList.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function __construct(
) {
}

/**
* Get the object ids and their types.
*
* @return array<string, string>
*/
public function getObjects(): array {
return $this->objects;
}
Expand All @@ -55,7 +60,7 @@ public static function xmlDeserialize(Reader $reader) {
}
}
if ($id !== '' && $type !== '') {
$objects[$id] = $type;
$objects[(string)$id] = (string)$type;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function __construct(
IRootFolder $rootFolder,
) {
$children = [
// Only files are supported at the moment
// Also see SystemTagPlugin::OBJECTIDS_PROPERTYNAME supported types
new SystemTagsObjectTypeCollection(
'files',
$tagManager,
Expand Down
10 changes: 10 additions & 0 deletions apps/dav/tests/unit/SystemTag/SystemTagPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\DAV\SystemTag\SystemTagPlugin;
use OCA\DAV\SystemTag\SystemTagsByIdCollection;
use OCA\DAV\SystemTag\SystemTagsObjectMappingCollection;
use OCP\Files\IRootFolder;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
Expand Down Expand Up @@ -56,6 +57,11 @@ class SystemTagPluginTest extends \Test\TestCase {
*/
private $userSession;

/**
* @var IRootFolder
*/
private $rootFolder;

/**
* @var IUser
*/
Expand Down Expand Up @@ -95,13 +101,17 @@ protected function setUp(): void {
->expects($this->any())
->method('isLoggedIn')
->willReturn(true);

$this->tagMapper = $this->getMockBuilder(ISystemTagObjectMapper::class)
->getMock();
$this->rootFolder = $this->getMockBuilder(IRootFolder::class)
->getMock();

$this->plugin = new SystemTagPlugin(
$this->tagManager,
$this->groupManager,
$this->userSession,
$this->rootFolder,
$this->tagMapper
);
$this->plugin->initialize($this->server);
Expand Down
Loading