Repository navigation
Feat/federated sharing - #8184
luka-nextcloud wants to merge 13 commits into
Conversation
|
🐢 Performance warning. |
9af44d8 to
e176ad6
Compare
e176ad6 to
1eca4c9
Compare
|
🐢 Performance warning. |
|
@CarlSchwan @come-nc I would need advice from experts of files_sharing app: |
1eca4c9 to
38e34df
Compare
|
🐢 Performance warning. |
|
🐢 Performance warning. |
8c7bc98 to
866cdf7
Compare
|
🐢 Performance warning. |
866cdf7 to
a3bce6f
Compare
fdeff1a to
2be9ceb
Compare
benjaminfrueh
left a comment
There was a problem hiding this comment.
@luka-nextcloud thanks for working on this! I left some comments, mostly about the attachment part.
One more general thing: the PR doesn't add tests for the new federation code yet. It would be great to have some.
The PR description is still empty. Could you add a short summary of what's included and how to test it?
| // check for users | ||
| foreach ($acls as $acl) { | ||
| if ($acl->getType() === Acl::PERMISSION_TYPE_USER && $acl->getParticipant() === $userId) { | ||
| if (in_array($acl->getType(), [Acl::PERMISSION_TYPE_USER, Acl::PERMISSION_TYPE_REMOTE]) && $acl->getParticipant() === $userId) { |
There was a problem hiding this comment.
This now also matches remote ACLs by participant ID. Since local user IDs can contain @, a local user whose ID is the same as a remote participants cloud ID (e.g. user1@other.server) would get that remote participants permissions. Could we keep remote ACLs out of userCan() and handle the federated case separately?
There was a problem hiding this comment.
This still compares the remote user cloud ID with the local user ID, so a local user whose ID is the same as a remote participant's cloud ID still gets their permissions. I tested and such a local user can read the board and create cards.
Since only DeckShareProvider::create() needs this for the remote sharedBy, maybe keep userCan() for local users only and check remote participants there explicitly, e.g.:
if ($this->userManager->userExists($sharedBy)) {
// existing checkPermission() for local users
} elseif (!$this->permissionService->remoteParticipantCan($boardId, $sharedBy, Acl::PERMISSION_EDIT)) {
$valid = false;
}with remoteParticipantCan() only checking PERMISSION_TYPE_REMOTE ACLs and probably also checking that federation is enabled like externalUserCan() does.
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
Signed-off-by: Luka Trovic <luka@nextcloud.com>
2be9ceb to
5792eaf
Compare
benjaminfrueh
left a comment
There was a problem hiding this comment.
@luka-nextcloud thanks for the updates, a lot of things are fixed and improved now 🚀
I did a second round of review and added some follow-ups, mostly around the remote user check, the cleanup when someone leaves, the files_sharing dependency and the comment mentions.
More generally: the share lookups are now spread as raw queries across RemoteShareHelper, FilesAppService and DeckShareProvider. Moving them into one place, e.g. a FederatedShareMapper like the existing mappers, would make the logic easier to follow and to test.
Tests for the new federation code and a short PR description with what's included and how to test it would still be great.
|
|
||
| public function acceptRemoteAttachment(int $cardId, string $token, $receiverUserId): void { | ||
| $this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT); | ||
| $this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT, $receiverUserId); |
There was a problem hiding this comment.
Thanks for adding the user check, in RemoteShareHelper::getRemoteFile() it works now. In AttachmentService::acceptRemoteAttachment() the user comes from the remote request, and the second PermissionService::checkPermission() doesn't check it, since federated requests only check the access token. So a remote server could still send another users share.
The share always goes to the user the board was shared by, so that user has to be the owner or have share permission. Could we check that instead?
| $this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT, $receiverUserId); | |
| $boardId = $this->cardMapper->findBoardId($cardId); | |
| if (!$this->permissionService->canUserShareBoard($boardId, $receiverUserId)) { | |
| throw new NoPermissionException('Permission denied for remote attachment'); | |
| } |
With a small helper in the PermissionService:
public function canUserShareBoard(int $boardId, string $userId): bool {
if ($this->userIsBoardOwner($boardId, $userId)) {
return true;
}
return $this->userCan($this->aclMapper->findAll($boardId), Acl::PERMISSION_SHARE, $userId);
}| $deleteQb = $this->dbConnection->getQueryBuilder(); | ||
| $deleteQb->delete('share') | ||
| ->where($deleteQb->expr()->in('id', $deleteQb->createNamedParameter($shareIds, IQueryBuilder::PARAM_INT_ARRAY))); | ||
| $deleteQb->executeStatement(); |
There was a problem hiding this comment.
Thanks for adding the cleanup!
This does almost the same as DeckShareProvider::deleteRemoteShares() but deletes the shares with a direct query, so the remote server isn't notified and the share stays on their side.
Could we move both into one method in RemoteShareHelper that deletes through the share manager? $federatedCloudId could be optional, so when a participant leaves, only their shares are deleted and when an attachment is deleted, the shares of all participants are deleted.
$share = $this->shareManager->getShareByToken((string)$row['token']);
$this->shareManager->deleteShare($share);| ->andWhere($qb->expr()->eq('st.board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT))) | ||
| ->andWhere($qb->expr()->eq('c.deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))); |
There was a problem hiding this comment.
This skips cards in the trash, so if a participant leaves or is removed while a card is deleted, their shares for its attachments aren't removed and they can still read those files (also after the card is restored). Could we drop this condition?
| ->andWhere($qb->expr()->eq('st.board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT))) | |
| ->andWhere($qb->expr()->eq('c.deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))); | |
| ->andWhere($qb->expr()->eq('st.board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT))); |
| // check for users | ||
| foreach ($acls as $acl) { | ||
| if ($acl->getType() === Acl::PERMISSION_TYPE_USER && $acl->getParticipant() === $userId) { | ||
| if (in_array($acl->getType(), [Acl::PERMISSION_TYPE_USER, Acl::PERMISSION_TYPE_REMOTE]) && $acl->getParticipant() === $userId) { |
There was a problem hiding this comment.
This still compares the remote user cloud ID with the local user ID, so a local user whose ID is the same as a remote participant's cloud ID still gets their permissions. I tested and such a local user can read the board and create cards.
Since only DeckShareProvider::create() needs this for the remote sharedBy, maybe keep userCan() for local users only and check remote participants there explicitly, e.g.:
if ($this->userManager->userExists($sharedBy)) {
// existing checkPermission() for local users
} elseif (!$this->permissionService->remoteParticipantCan($boardId, $sharedBy, Acl::PERMISSION_EDIT)) {
$valid = false;
}with remoteParticipantCan() only checking PERMISSION_TYPE_REMOTE ACLs and probably also checking that federation is enabled like externalUserCan() does.
| } | ||
|
|
||
| $attachmentNode = $userFolder->get($attachmentFolder); | ||
| if (!$attachmentNode instanceof Folder) { |
There was a problem hiding this comment.
Small edge case: if the users attachment folder is a share they received, the attachment would be mounted inside someone elses shared folder. FilesAppService::createForRemote() handles this with isShared(), maybe we could just skip the move in that case:
| if (!$attachmentNode instanceof Folder) { | |
| if (!$attachmentNode instanceof Folder || $attachmentNode->isShared()) { |
| public function mountExternalShare(ExternalShare $externalShare, IUser $user): ExternalShareMount { | ||
| $this->setupManager->tearDown(); | ||
| $this->setupManager->setupForUser($user); | ||
|
|
||
| $mountPoint = '/' . $user->getUID() . '/files/' . ltrim($externalShare->getMountpoint(), '/'); | ||
| $existingMount = $this->getMountedExternalShare($mountPoint); | ||
| if ($existingMount !== null) { | ||
| return $existingMount; | ||
| } | ||
|
|
||
| throw new GenericShareException('Failed to mount external share.'); | ||
| } |
There was a problem hiding this comment.
Thanks for switching to the setup manager!
This now runs tearDown() + setupForUser() for every remote attachment on every load of the attachment list, even when the share is already mounted. Could we check for the existing mount first and only set up the filesystem again if it isn't mounted yet?
Something like:
public function mountExternalShare(ExternalShare $externalShare, IUser $user): ExternalShareMount {
$mountPoint = '/' . $user->getUID() . '/files/' . ltrim($externalShare->getMountpoint(), '/');
$existingMount = $this->getMountedExternalShare($mountPoint);
if ($existingMount !== null) {
return $existingMount;
}
$this->setupManager->tearDown();
$this->setupManager->setupForUser($user);
return $this->getMountedExternalShare($mountPoint) ?? throw new GenericShareException('Failed to mount external share.');
}| public function __construct( | ||
| private readonly IDBConnection $dbConnection, | ||
| private readonly IManager $shareManager, | ||
| private readonly ExternalShareManager $externalShareManager, |
There was a problem hiding this comment.
Thanks for moving this into a separate helper!
Deck still fails with a 500 when files_sharing is disabled though, because RemoteShareHelper is injected in a lot of places and needs ExternalShareManager in its constructor. Could we fetch it lazily instead, like CirclesService does for the circles app, so it is only loaded when a federated share is handled?
private function getExternalShareManager(): ExternalShareManager {
return $this->container->get(ExternalShareManager::class);
}with ContainerInterface $container injected instead of ExternalShareManager
| private readonly IJobList $jobList, | ||
| private readonly IRootFolder $rootFolder, | ||
| private readonly IPreview $preview, | ||
| private readonly \OC\Files\Mount\Manager $mountManager, |
There was a problem hiding this comment.
Could we inject the public IMountManager instead of the private \OC\Files\Mount\Manager? Only find() is used, which is part of the interface.
| return $localizedUsers; | ||
| } | ||
|
|
||
| public function localizeRemoteComments(Board $localBoard, array $comments): array { |
There was a problem hiding this comment.
About comment mentions on federated boards: this currently feels a bit overengineered and error prone.
ExternalBoardService::localizeRemoteComments() rewrites the message text and the mentions list separately, and CommentItem.vue strips federated_user/ again, so they easily get out of sync, e.g. with more than one mention in a comment or with quoted mentions like @"bob".
I think the mention handling could be simplified by leaving the message text as it is:
CommentService::formatComment()adds the cloud ID to each mention (bob->bob@nextcloud.local).ExternalBoardService::localizeRemoteComments()only checks for each mention whether that cloud ID is a local user and adds the user to show without touching the message.CommentItem.vuefinds the mention in the text by its original ID and renders that user, so thefederated_user/replacing isn't needed anymore.
That would also reduce the logic and code quite a bit.
| /** | ||
| * @param int[] $deckShareIds | ||
| */ | ||
| private function deleteExternalSharesSharedByFederatedUser(array $deckShareIds, string $federatedCloudId): void { |
There was a problem hiding this comment.
Similar to the comment on deleteFederatedAttachmentShares(): this removes the attachments the participant uploaded with direct queries, and reimplements what DeckShareProvider::delete() already does. Could we reuse that by deleting the attachments through the share manager instead? That would simplify the logic here quite a bit.
while ($row = $cursor->fetchAssociative()) {
try {
$this->shareManager->deleteShare($this->shareManager->getShareById('deck:' . $row['id']));
} catch (\Exception) {
continue;
}
}The catch keeps one broken attachment from stopping the whole cleanup.
Summary
TODO
Checklist