|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Tables\Service; |
| 10 | + |
| 11 | +use OCA\Tables\AppInfo\Application; |
| 12 | +use OCA\Tables\Constants\ShareReceiverType; |
| 13 | +use OCA\Tables\Db\Share; |
| 14 | +use OCA\Tables\Db\ShareMapper; |
| 15 | +use OCA\Tables\Db\UserArchiveMapper; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Removes per-user archive overrides once a user loses access to a node. |
| 20 | + * |
| 21 | + * Overrides are pure UX metadata: a stale row has no effect for a user |
| 22 | + * without access (the entity flag fallback applies), so every method here |
| 23 | + * logs failures instead of throwing to never break the triggering flow. |
| 24 | + */ |
| 25 | +class ArchiveCleanupService { |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private readonly UserArchiveMapper $userArchiveMapper, |
| 29 | + private readonly ShareMapper $shareMapper, |
| 30 | + private readonly PermissionsService $permissionsService, |
| 31 | + private readonly LoggerInterface $logger, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Remove archive overrides of the given users on all nodes shared with |
| 37 | + * the given receiver, for every node the user can no longer access. |
| 38 | + * |
| 39 | + * Called when a user is removed from a group or from a circle. |
| 40 | + * |
| 41 | + * @param string[] $userIds |
| 42 | + */ |
| 43 | + public function cleanupAfterMembershipLoss(array $userIds, string $receiver, string $receiverType): void { |
| 44 | + try { |
| 45 | + $nodes = $this->shareMapper->findNodesByReceiver($receiver, $receiverType); |
| 46 | + } catch (\Throwable $e) { |
| 47 | + $this->logNonFatal(__FUNCTION__, $e); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $nodeIdsByType = [ |
| 52 | + Application::NODE_TYPE_TABLE => [], |
| 53 | + Application::NODE_TYPE_CONTEXT => [], |
| 54 | + ]; |
| 55 | + foreach ($nodes as $node) { |
| 56 | + $nodeType = $this->shareNodeType2Const($node['nodeType']); |
| 57 | + if ($nodeType !== null) { |
| 58 | + $nodeIdsByType[$nodeType][] = $node['nodeId']; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + foreach ($userIds as $userId) { |
| 63 | + foreach ($nodeIdsByType as $nodeType => $nodeIds) { |
| 64 | + if ($nodeIds === []) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + try { |
| 68 | + $overrides = $this->userArchiveMapper->findAllOverridesForUser($userId, $nodeType, $nodeIds); |
| 69 | + } catch (\Throwable $e) { |
| 70 | + $this->logNonFatal(__FUNCTION__, $e); |
| 71 | + continue; |
| 72 | + } |
| 73 | + foreach (array_keys($overrides) as $nodeId) { |
| 74 | + $this->removeOverrideIfStale($userId, $nodeType, $nodeId); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Remove the archive overrides of every user who can no longer access |
| 82 | + * the given node. |
| 83 | + * |
| 84 | + * Called when a group or circle receiver of the node is deleted, or when |
| 85 | + * a group or circle share of the node is removed. |
| 86 | + */ |
| 87 | + public function purgeNodeOverrides(int $nodeType, int $nodeId): void { |
| 88 | + try { |
| 89 | + $userIds = $this->userArchiveMapper->findUserIdsForNode($nodeType, $nodeId); |
| 90 | + } catch (\Throwable $e) { |
| 91 | + $this->logNonFatal(__FUNCTION__, $e); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + foreach ($userIds as $userId) { |
| 96 | + $this->removeOverrideIfStale($userId, $nodeType, $nodeId); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Remove archive overrides that became stale because a share was deleted. |
| 102 | + */ |
| 103 | + public function cleanupAfterShareDeletion(Share $share): void { |
| 104 | + $nodeType = $this->shareNodeType2Const((string)$share->getNodeType()); |
| 105 | + if ($nodeType === null) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + $receiverType = $share->getReceiverType(); |
| 110 | + if ($receiverType === ShareReceiverType::USER) { |
| 111 | + $this->removeOverrideIfStale((string)$share->getReceiver(), $nodeType, (int)$share->getNodeId()); |
| 112 | + } elseif ($receiverType === ShareReceiverType::GROUP || $receiverType === ShareReceiverType::CIRCLE) { |
| 113 | + $this->purgeNodeOverrides($nodeType, (int)$share->getNodeId()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Remove every archive override of a deleted user account. |
| 119 | + */ |
| 120 | + public function cleanupDeletedUser(string $userId): void { |
| 121 | + try { |
| 122 | + $this->userArchiveMapper->deleteAllForUser($userId); |
| 123 | + } catch (\Throwable $e) { |
| 124 | + $this->logNonFatal(__FUNCTION__, $e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private function removeOverrideIfStale(string $userId, int $nodeType, int $nodeId): void { |
| 129 | + try { |
| 130 | + if ($this->hasAccess($userId, $nodeType, $nodeId)) { |
| 131 | + return; |
| 132 | + } |
| 133 | + $this->userArchiveMapper->deleteForUser($userId, $nodeType, $nodeId); |
| 134 | + } catch (\Throwable $e) { |
| 135 | + $this->logNonFatal(__FUNCTION__, $e); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private function hasAccess(string $userId, int $nodeType, int $nodeId): bool { |
| 140 | + if ($nodeType === Application::NODE_TYPE_CONTEXT) { |
| 141 | + return $this->permissionsService->canAccessContextById($nodeId, $userId); |
| 142 | + } |
| 143 | + return $this->permissionsService->canAccessNodeById($nodeType, $nodeId, $userId); |
| 144 | + } |
| 145 | + |
| 146 | + private function shareNodeType2Const(string $nodeType): ?int { |
| 147 | + return match ($nodeType) { |
| 148 | + 'table' => Application::NODE_TYPE_TABLE, |
| 149 | + 'context' => Application::NODE_TYPE_CONTEXT, |
| 150 | + default => null, |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + private function logNonFatal(string $method, \Throwable $e): void { |
| 155 | + $this->logger->warning(static::class . ' - ' . $method . ': archive override cleanup failed: ' . $e->getMessage(), [ |
| 156 | + 'exception' => $e, |
| 157 | + ]); |
| 158 | + } |
| 159 | +} |
0 commit comments