Skip to content

Commit 1693666

Browse files
authored
Merge pull request #1073 from nextcloud/fix/restrict-tag-creation
Restrict tag creation
2 parents e546b4b + ffdb4ed commit 1693666

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

lib/Migrator/FilesMigrator.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use OCP\IUser;
2626
use OCP\SystemTag\ISystemTagManager;
2727
use OCP\SystemTag\ISystemTagObjectMapper;
28+
use OCP\SystemTag\TagAlreadyExistsException;
29+
use OCP\SystemTag\TagCreationForbiddenException;
2830
use OCP\SystemTag\TagNotFoundException;
2931
use OCP\UserMigration\IExportDestination;
3032
use OCP\UserMigration\IImportSource;
@@ -300,8 +302,14 @@ public function import(
300302
$taggedFiles = json_decode($importSource->getFileContents(static::PATH_TAGS), true, 512, JSON_THROW_ON_ERROR);
301303
$tagger = $this->tagManager->load(Application::APP_ID, [], false, $uid);
302304
foreach ($taggedFiles as $path => $tags) {
305+
try {
306+
$node = $userFolder->get((string)$path);
307+
} catch (NotFoundException) {
308+
/* File/Folder not found, skip */
309+
continue;
310+
}
303311
foreach ($tags as $tag) {
304-
if ($tagger->tagAs($userFolder->get($path)->getId(), $tag) === false) {
312+
if ($tagger->tagAs($node->getId(), $tag) === false) {
305313
throw new UserMigrationException("Failed to import tag $tag for path $path");
306314
}
307315
}
@@ -311,16 +319,40 @@ public function import(
311319

312320
$systemTaggedFiles = json_decode($importSource->getFileContents(static::PATH_SYSTEMTAGS), true, 512, JSON_THROW_ON_ERROR);
313321
foreach ($systemTaggedFiles as $path => $systemTags) {
322+
try {
323+
$node = $userFolder->get((string)$path);
324+
if (!$node->isUpdateable()) {
325+
/* Node is read-only, cannot tag */
326+
continue;
327+
}
328+
} catch (NotFoundException) {
329+
/* File/Folder not found, skip */
330+
continue;
331+
}
314332
$systemTagIds = [];
315333
foreach ($systemTags as $systemTag) {
316334
try {
317335
$systemTagObject = $this->systemTagManager->getTag($systemTag, true, true);
318-
} catch (TagNotFoundException $e) {
319-
$systemTagObject = $this->systemTagManager->createTag($systemTag, true, true);
336+
if (!$this->systemTagManager->canUserAssignTag($systemTagObject, $user)) {
337+
/* Should not happen because we requested an assignable and visible tag, but let’s make sure */
338+
continue;
339+
}
340+
} catch (TagNotFoundException) {
341+
try {
342+
if (!$this->systemTagManager->canUserCreateTag($user)) {
343+
/* FIXME This should be remove when passing the user to the createTag method is supported by all supported Nextcloud versions */
344+
throw new TagCreationForbiddenException();
345+
}
346+
/** @psalm-suppress TooManyArguments The extra argument is supported on >=34 and ignored below */
347+
$systemTagObject = $this->systemTagManager->createTag($systemTag, true, true, $user);
348+
} catch (TagCreationForbiddenException|TagAlreadyExistsException) {
349+
/* Not allowed to create tag or a restricted tag with the same name exists, skip */
350+
continue;
351+
}
320352
}
321353
$systemTagIds[] = $systemTagObject->getId();
322354
}
323-
if ($this->systemTagMapper->assignTags((string)$userFolder->get($path)->getId(), 'files', $systemTagIds) === false) {
355+
if ($this->systemTagMapper->assignTags((string)$node->getId(), 'files', $systemTagIds) === false) {
324356
throw new UserMigrationException("Failed to import system tags for path $path");
325357
}
326358
}
@@ -329,6 +361,12 @@ public function import(
329361

330362
$comments = json_decode($importSource->getFileContents(static::PATH_COMMENTS), true, 512, JSON_THROW_ON_ERROR);
331363
foreach ($comments as $path => $fileComments) {
364+
try {
365+
$node = $userFolder->get((string)$path);
366+
} catch (NotFoundException) {
367+
/* File/Folder not found, skip */
368+
continue;
369+
}
332370
foreach ($fileComments as $fileComment) {
333371
if (($fileComment['actorType'] === 'users') || ($fileComment['actorType'] === ICommentsManager::DELETED_USER)) {
334372
$actorId = $fileComment['actorId'];
@@ -340,7 +378,7 @@ public function import(
340378
$actorId = ICommentsManager::DELETED_USER;
341379
$actorType = ICommentsManager::DELETED_USER;
342380
}
343-
$commentObject = $this->commentsManager->create($actorType, $actorId, 'files', (string)$userFolder->get($path)->getId());
381+
$commentObject = $this->commentsManager->create($actorType, $actorId, 'files', (string)$node->getId());
344382
$commentObject->setMessage($fileComment['message']);
345383
$commentObject->setVerb($fileComment['verb']);
346384
$commentObject->setCreationDateTime(new \DateTime($fileComment['creationDateTime']));

0 commit comments

Comments
 (0)