Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable20] send share notification instead of erroring on duplicate share #26125

Merged
merged 1 commit into from
Mar 19, 2021
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
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
'OCP\\Share' => $baseDir . '/lib/public/Share.php',
'OCP\\Share\\Events\\ShareCreatedEvent' => $baseDir . '/lib/public/Share/Events/ShareCreatedEvent.php',
'OCP\\Share\\Events\\VerifyMountPointEvent' => $baseDir . '/lib/public/Share/Events/VerifyMountPointEvent.php',
'OCP\\Share\\Exceptions\\AlreadySharedException' => $baseDir . '/lib/public/Share/Exceptions/AlreadySharedException.php',
'OCP\\Share\\Exceptions\\GenericShareException' => $baseDir . '/lib/public/Share/Exceptions/GenericShareException.php',
'OCP\\Share\\Exceptions\\IllegalIDChangeException' => $baseDir . '/lib/public/Share/Exceptions/IllegalIDChangeException.php',
'OCP\\Share\\Exceptions\\ShareNotFound' => $baseDir . '/lib/public/Share/Exceptions/ShareNotFound.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Share' => __DIR__ . '/../../..' . '/lib/public/Share.php',
'OCP\\Share\\Events\\ShareCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/ShareCreatedEvent.php',
'OCP\\Share\\Events\\VerifyMountPointEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/VerifyMountPointEvent.php',
'OCP\\Share\\Exceptions\\AlreadySharedException' => __DIR__ . '/../../..' . '/lib/public/Share/Exceptions/AlreadySharedException.php',
'OCP\\Share\\Exceptions\\GenericShareException' => __DIR__ . '/../../..' . '/lib/public/Share/Exceptions/GenericShareException.php',
'OCP\\Share\\Exceptions\\IllegalIDChangeException' => __DIR__ . '/../../..' . '/lib/public/Share/Exceptions/IllegalIDChangeException.php',
'OCP\\Share\\Exceptions\\ShareNotFound' => __DIR__ . '/../../..' . '/lib/public/Share/Exceptions/ShareNotFound.php',
Expand Down
132 changes: 69 additions & 63 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
use OCP\Share;
use OCP\Share\Exceptions\AlreadySharedException;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
Expand Down Expand Up @@ -564,7 +565,7 @@ protected function userCreateChecks(IShare $share) {

// Identical share already existst
if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
throw new \Exception('Path is already shared with this user');
throw new AlreadySharedException('Path is already shared with this user', $existingShare);
}

// The share is already shared with this user via a group share
Expand All @@ -574,7 +575,7 @@ protected function userCreateChecks(IShare $share) {
$user = $this->userManager->get($share->getSharedWith());

if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
throw new \Exception('Path is already shared with this user');
throw new AlreadySharedException('Path is already shared with this user', $existingShare);
}
}
}
Expand Down Expand Up @@ -619,7 +620,7 @@ protected function groupCreateChecks(IShare $share) {
}

if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
throw new \Exception('Path is already shared with this group');
throw new AlreadySharedException('Path is already shared with this group', $existingShare);
}
}
}
Expand Down Expand Up @@ -733,77 +734,82 @@ public function createShare(IShare $share) {
}
}

//Verify share type
if ($share->getShareType() === IShare::TYPE_USER) {
$this->userCreateChecks($share);

//Verify the expiration date
$share = $this->validateExpirationDateInternal($share);
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
$this->groupCreateChecks($share);
try {
//Verify share type
if ($share->getShareType() === IShare::TYPE_USER) {
$this->userCreateChecks($share);

//Verify the expiration date
$share = $this->validateExpirationDateInternal($share);
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
$this->linkCreateChecks($share);
$this->setLinkParent($share);
//Verify the expiration date
$share = $this->validateExpirationDateInternal($share);
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
$this->groupCreateChecks($share);

/*
* For now ignore a set token.
*/
$share->setToken(
$this->secureRandom->generate(
\OC\Share\Constants::TOKEN_LENGTH,
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
)
);
//Verify the expiration date
$share = $this->validateExpirationDateInternal($share);
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
$this->linkCreateChecks($share);
$this->setLinkParent($share);

/*
* For now ignore a set token.
*/
$share->setToken(
$this->secureRandom->generate(
\OC\Share\Constants::TOKEN_LENGTH,
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
)
);

//Verify the expiration date
$share = $this->validateExpirationDate($share);
//Verify the expiration date
$share = $this->validateExpirationDate($share);

//Verify the password
$this->verifyPassword($share->getPassword());
//Verify the password
$this->verifyPassword($share->getPassword());

// If a password is set. Hash it!
if ($share->getPassword() !== null) {
$share->setPassword($this->hasher->hash($share->getPassword()));
// If a password is set. Hash it!
if ($share->getPassword() !== null) {
$share->setPassword($this->hasher->hash($share->getPassword()));
}
} elseif ($share->getShareType() === IShare::TYPE_EMAIL) {
$share->setToken(
$this->secureRandom->generate(
\OC\Share\Constants::TOKEN_LENGTH,
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
)
);
}
} elseif ($share->getShareType() === IShare::TYPE_EMAIL) {
$share->setToken(
$this->secureRandom->generate(
\OC\Share\Constants::TOKEN_LENGTH,
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
)
);
}

// Cannot share with the owner
if ($share->getShareType() === IShare::TYPE_USER &&
$share->getSharedWith() === $share->getShareOwner()) {
throw new \InvalidArgumentException('Can’t share with the share owner');
}
// Cannot share with the owner
if ($share->getShareType() === IShare::TYPE_USER &&
$share->getSharedWith() === $share->getShareOwner()) {
throw new \InvalidArgumentException('Can’t share with the share owner');
}

// Generate the target
$target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName();
$target = \OC\Files\Filesystem::normalizePath($target);
$share->setTarget($target);
// Generate the target
$target = $this->config->getSystemValue('share_folder', '/') . '/' . $share->getNode()->getName();
$target = \OC\Files\Filesystem::normalizePath($target);
$share->setTarget($target);

// Pre share event
$event = new GenericEvent($share);
$this->legacyDispatcher->dispatch('OCP\Share::preShare', $event);
if ($event->isPropagationStopped() && $event->hasArgument('error')) {
throw new \Exception($event->getArgument('error'));
}
// Pre share event
$event = new GenericEvent($share);
$this->legacyDispatcher->dispatch('OCP\Share::preShare', $event);
if ($event->isPropagationStopped() && $event->hasArgument('error')) {
throw new \Exception($event->getArgument('error'));
}

$oldShare = $share;
$provider = $this->factory->getProviderForType($share->getShareType());
$share = $provider->create($share);
//reuse the node we already have
$share->setNode($oldShare->getNode());
$oldShare = $share;
$provider = $this->factory->getProviderForType($share->getShareType());
$share = $provider->create($share);
//reuse the node we already have
$share->setNode($oldShare->getNode());

// Reset the target if it is null for the new share
if ($share->getTarget() === '') {
$share->setTarget($target);
// Reset the target if it is null for the new share
if ($share->getTarget() === '') {
$share->setTarget($target);
}
} catch (AlreadySharedException $e) {
// if a share for the same target already exists, dont create a new one, but do trigger the hooks and notifications again
$share = $e->getExistingShare();
}

// Post share event
Expand Down
50 changes: 50 additions & 0 deletions lib/public/Share/Exceptions/AlreadySharedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\Share\Exceptions;

use OCP\Share\IShare;

/**
* @since 22.0.0
*/
class AlreadySharedException extends GenericShareException {
/** @var IShare */
private $existingShare;

/**
* @since 22.0.0
*/
public function __construct(string $message, IShare $existingShare) {
parent::__construct($message);

$this->existingShare = $existingShare;
}

/**
* @since 22.0.0
*/
public function getExistingShare(): IShare {
return $this->existingShare;
}
}