Skip to content

Commit d57fb90

Browse files
committed
fix: Improve error handling and permissions checks in FilesMigrator
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 5f221f0 commit d57fb90

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

lib/Migrator/FilesMigrator.php

Lines changed: 38 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,35 @@ 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->isUpdatable()) {
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, $user);
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+
$systemTagObject = $this->systemTagManager->createTag($systemTag, true, true, $user);
343+
} catch (TagCreationForbiddenException|TagAlreadyExistsException) {
344+
/* Not allowed to create tag or a restricted tag with the same name exists, skip */
345+
continue;
346+
}
320347
}
321348
$systemTagIds[] = $systemTagObject->getId();
322349
}
323-
if ($this->systemTagMapper->assignTags((string)$userFolder->get($path)->getId(), 'files', $systemTagIds) === false) {
350+
if ($this->systemTagMapper->assignTags((string)$node->getId(), 'files', $systemTagIds) === false) {
324351
throw new UserMigrationException("Failed to import system tags for path $path");
325352
}
326353
}
@@ -329,6 +356,12 @@ public function import(
329356

330357
$comments = json_decode($importSource->getFileContents(static::PATH_COMMENTS), true, 512, JSON_THROW_ON_ERROR);
331358
foreach ($comments as $path => $fileComments) {
359+
try {
360+
$node = $userFolder->get((string)$path);
361+
} catch (NotFoundException) {
362+
/* File/Folder not found, skip */
363+
continue;
364+
}
332365
foreach ($fileComments as $fileComment) {
333366
if (($fileComment['actorType'] === 'users') || ($fileComment['actorType'] === ICommentsManager::DELETED_USER)) {
334367
$actorId = $fileComment['actorId'];
@@ -340,7 +373,7 @@ public function import(
340373
$actorId = ICommentsManager::DELETED_USER;
341374
$actorType = ICommentsManager::DELETED_USER;
342375
}
343-
$commentObject = $this->commentsManager->create($actorType, $actorId, 'files', (string)$userFolder->get($path)->getId());
376+
$commentObject = $this->commentsManager->create($actorType, $actorId, 'files', (string)$node->getId());
344377
$commentObject->setMessage($fileComment['message']);
345378
$commentObject->setVerb($fileComment['verb']);
346379
$commentObject->setCreationDateTime(new \DateTime($fileComment['creationDateTime']));

0 commit comments

Comments
 (0)