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
29 changes: 28 additions & 1 deletion apps/files_trashbin/lib/Trash/LegacyTrashBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Files_Trashbin\Helper;
use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trashbin;
use OCP\Federation\ICloudIdManager;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
Expand All @@ -26,6 +27,7 @@ class LegacyTrashBackend implements ITrashBackend {
public function __construct(
private IRootFolder $rootFolder,
private IUserManager $userManager,
private ICloudIdManager $cloudIdManager,
) {
}

Expand All @@ -38,7 +40,7 @@ private function mapTrashItem(FileInfo $file, IUser $user, ?ITrashItem $parent =
$originalLocation = $file->getName();
}
/** @psalm-suppress UndefinedInterfaceMethod */
$deletedBy = $this->userManager->get($file['deletedBy']) ?? $parent?->getDeletedBy();
$deletedBy = $this->resolveDeletedBy($file['deletedBy']) ?? $parent?->getDeletedBy();
$trashFilename = Trashbin::getTrashFilename($file->getName(), $file->getMtime());
return new TrashItem(
$this,
Expand Down Expand Up @@ -119,4 +121,29 @@ public function getTrashNodeById(IUser $user, int $fileId) {
return null;
}
}

/**
* Resolve the user that deleted a trash item. Files deleted by a federated share
* recipient only carry the recipient's remote cloud ID, which no local IUserManager
* backend can resolve, so fall back to a display-only user for the cloud ID in that
* case instead of leaving the item without an "Unknown" deleted by user.
*/
private function resolveDeletedBy(?string $uid): ?IUser {
if (!$uid) {
return null;
}

$user = $this->userManager->get($uid);
if ($user !== null) {
return $user;
}

try {
$cloudId = $this->cloudIdManager->resolveCloudId($uid);
} catch (\InvalidArgumentException $e) {
return null;
}

return $this->userManager->getFederatedUser($cloudId);
}
}
5 changes: 5 additions & 0 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\Config\IUserConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudId;
use OCP\HintException;
use OCP\ICache;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -851,4 +852,8 @@ public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator {
public function getExistingUser(string $userId, ?string $displayName = null): IUser {
return new LazyUser($userId, $this, $displayName);
}

public function getFederatedUser(ICloudId $cloudId): IUser {
return new LazyUser($cloudId->getDisplayId(), $this, $cloudId->getDisplayId());
}
}
11 changes: 11 additions & 0 deletions lib/public/IUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,15 @@ public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator;
* @since 33.0.0
*/
public function getExistingUser(string $userId, ?string $displayName = null): IUser;

/**
* Get a read-only user from a cloud ID for showing the display name of a remote
* federation user (e.g. the "deleted by" user of a federated share) that has no
* local account.
*
* @param \OCP\Federation\ICloudId $federatedUserId A cloud ID of the federated user
* @return IUser
* @since 35.0.0
*/
public function getFederatedUser(\OCP\Federation\ICloudId $cloudId): IUser;
}
16 changes: 16 additions & 0 deletions tests/lib/User/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OC\User\User;
use OCP\Config\IUserConfig;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudId;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
Expand Down Expand Up @@ -762,4 +763,19 @@ public function testGetExistingUser() {
$this->assertEquals('nobody', $user->getUID());
$this->assertEquals('None', $user->getDisplayName());
}

public function testGetFederatedUser(): void {
$userId = 'test@example.com';

$cloudId = $this->createMock(ICloudId::class);
$cloudId->expects($this->exactly(2))
->method('getDisplayId')
->willReturn($userId);

$manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$user = $manager->getFederatedUser($cloudId);

$this->assertEquals($userId, $user->getUID());
$this->assertEquals($userId, $user->getDisplayName());
}
}
Loading