diff --git a/lib/Db/RowCellUsergroupMapper.php b/lib/Db/RowCellUsergroupMapper.php index af975a8c37..35f63f4514 100644 --- a/lib/Db/RowCellUsergroupMapper.php +++ b/lib/Db/RowCellUsergroupMapper.php @@ -10,6 +10,7 @@ use OCA\Tables\Constants\UsergroupType; use OCA\Tables\Helper\CircleHelper; use OCA\Tables\Helper\GroupHelper; +use OCA\Tables\Share\ShareContext; use OCP\IDBConnection; use OCP\IUserManager; use OCP\IUserSession; @@ -26,6 +27,7 @@ public function __construct( private CircleHelper $circleHelper, private GroupHelper $groupHelper, protected IUserSession $userSession, + private ShareContext $shareContext, ) { parent::__construct($db, $this->table, RowCellUsergroup::class); } @@ -49,7 +51,7 @@ public function formatRowData(Column $column, array $row) { if ($valueType === UsergroupType::USER) { $displayName = $this->userManager->getDisplayName($value) ?? $value; } elseif ($valueType === UsergroupType::CIRCLE) { - $displayName = $this->circleHelper->getCircleDisplayName($value, ($this->userSession->getUser()?->getUID() ?: '')) ?: $value; + $displayName = $this->circleHelper->getCircleDisplayName($value, $this->getCurrentUserId()) ?: $value; } elseif ($valueType === UsergroupType::GROUP) { $displayName = $this->groupHelper->getGroupDisplayName($value) ?: $value; } @@ -63,4 +65,13 @@ public function formatRowData(Column $column, array $row) { public function hasMultipleValues(): bool { return true; } + + private function getCurrentUserId(): string { + $remoteUserId = $this->shareContext->getRemoteUserId(); + if ($remoteUserId !== null) { + return $remoteUserId; + } + + return $this->userSession->getUser()?->getUID() ?: ''; + } } diff --git a/lib/Helper/CircleHelper.php b/lib/Helper/CircleHelper.php index 34b2477e95..d362b2f0ea 100644 --- a/lib/Helper/CircleHelper.php +++ b/lib/Helper/CircleHelper.php @@ -23,6 +23,9 @@ class CircleHelper { private bool $circlesEnabled; private ?CirclesManager $circlesManager; + private array $circleDisplayNames = []; + private array $federatedUsers = []; + private array $unreachableUserIds = []; /** * @psalm-suppress UndefinedClass @@ -50,21 +53,26 @@ public function isCirclesEnabled(): bool { } public function getCircleDisplayName(string $circleId, string $userId): string { - if (!$this->circlesEnabled) { + return $this->circleDisplayNames[$userId][$circleId] ??= $this->resolveCircleDisplayName($circleId, $userId); + } + + private function resolveCircleDisplayName(string $circleId, string $userId): string { + if (!$this->circlesEnabled || isset($this->unreachableUserIds[$userId])) { return $circleId; } try { - $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER); + $federatedUser = $this->federatedUsers[$userId] ??= $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER); $this->circlesManager->startSession($federatedUser); $circle = $this->circlesManager->getCircle($circleId); return $circle ? ($circle->getDisplayName() ?: $circleId) : $circleId; } catch (Throwable $e) { if ($e->getCode() === 404) { - return $this->l10n->t('Deleted team %s.', [$circleId]); + return $this->l10n->t('Team not found'); } + $this->unreachableUserIds[$userId] = true; $this->logger->warning('Failed to get circle display name: ' . $e->getMessage(), [ 'circleId' => $circleId, 'userId' => $userId diff --git a/lib/Middleware/ShareControlMiddleware.php b/lib/Middleware/ShareControlMiddleware.php index 76bfa0bcac..206b8442f0 100644 --- a/lib/Middleware/ShareControlMiddleware.php +++ b/lib/Middleware/ShareControlMiddleware.php @@ -17,6 +17,7 @@ use OCA\Tables\Service\ConfigService; use OCA\Tables\Service\ShareService; use OCA\Tables\Service\ValueObject\ShareToken; +use OCA\Tables\Share\ShareContext; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -41,6 +42,7 @@ public function __construct( private readonly IOCMDiscoveryService $ocmDiscoveryService, private readonly ICloudIdManager $cloudIdManager, private readonly ConfigService $configService, + private readonly ShareContext $shareContext, ) { } @@ -56,6 +58,7 @@ public function beforeController(Controller $controller, string $methodName): vo $tokenInput = $this->request->getParam('token', ''); $this->assertShareTokenIsValidAndExisting($tokenInput); $this->assertIsAccessible($tokenInput); + $this->shareContext->setShare($this->share); } } diff --git a/lib/Share/ShareContext.php b/lib/Share/ShareContext.php new file mode 100644 index 0000000000..081fced2f1 --- /dev/null +++ b/lib/Share/ShareContext.php @@ -0,0 +1,28 @@ +share = $share; + } + + public function getRemoteUserId(): ?string { + if ($this->share?->getReceiverType() !== ShareReceiverType::REMOTE) { + return null; + } + + return $this->share->getReceiver(); + } +}