Skip to content

Commit d3d116e

Browse files
committed
fix(federated-sharing): avoid duplicate shares
Signed-off-by: Hamza <hamzamahjoubi221@gmail.com>
1 parent b3f362f commit d3d116e

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

apps/federatedfilesharing/lib/FederatedShareProvider.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCP\IUserManager;
3131
use OCP\Security\ISecureRandom;
3232
use OCP\Server;
33+
use OCP\Share\Exceptions\AlreadySharedException;
3334
use OCP\Share\Exceptions\GenericShareException;
3435
use OCP\Share\Exceptions\ShareNotFound;
3536
use OCP\Share\IShare;
@@ -101,20 +102,25 @@ public function create(IShare $share): IShare {
101102
throw new \Exception($message_t);
102103
}
103104

105+
$cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
106+
104107
/*
105-
* Check if file is not already shared with the remote user
108+
* Check if file is not already shared with the remote user.
109+
* Has to be looked up by the normalized cloud ID, because that is what
110+
* gets stored below. Otherwise spellings like "user@server.com/" slip
111+
* past this check and create a duplicate share.
106112
*/
107-
$alreadyShared = $this->getSharedWith($shareWith, IShare::TYPE_REMOTE, $share->getNode(), 1, 0);
108-
$alreadySharedGroup = $this->getSharedWith($shareWith, IShare::TYPE_REMOTE_GROUP, $share->getNode(), 1, 0);
109-
if (!empty($alreadyShared) || !empty($alreadySharedGroup)) {
113+
// getSharedWith() ignores its $shareType argument and always
114+
// queries all supported remote types, so a single lookup covers both.
115+
$alreadyShared = $this->getSharedWith($cloudId->getId(), IShare::TYPE_REMOTE, $share->getNode(), 1, 0);
116+
if (!empty($alreadyShared)) {
110117
$message = 'Sharing %1$s failed, because this item is already shared with %2$s';
111118
$message_t = $this->l->t('Sharing %1$s failed, because this item is already shared with the account %2$s', [$share->getNode()->getName(), $shareWith]);
112119
$this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
113-
throw new \Exception($message_t);
120+
throw new AlreadySharedException($message_t, $alreadyShared[0]);
114121
}
115122

116123
// don't allow federated shares if source and target server are the same
117-
$cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
118124
$currentServer = $this->addressHandler->generateRemoteURL();
119125
$currentUser = $sharedBy;
120126
if ($this->addressHandler->compareAddresses($cloudId->getUser(), $cloudId->getRemote(), $currentUser, $currentServer)) {

0 commit comments

Comments
 (0)