Merge pull request #40164 from nextcloud/exceptionsTranslateShare

pull/47738/head
John Molakvoæ 2024-09-04 09:50:49 +07:00 committed by GitHub
commit 99b225f06b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 70 deletions

@ -103,7 +103,7 @@ class Manager implements IManager {
if ($password === null) {
// No password is set, check if this is allowed.
if ($this->shareApiLinkEnforcePassword()) {
throw new \InvalidArgumentException('Passwords are enforced for link and mail shares');
throw new \InvalidArgumentException($this->l->t('Passwords are enforced for link and mail shares'));
}
return;
@ -130,63 +130,63 @@ class Manager implements IManager {
if ($share->getShareType() === IShare::TYPE_USER) {
// We expect a valid user as sharedWith for user shares
if (!$this->userManager->userExists($share->getSharedWith())) {
throw new \InvalidArgumentException('SharedWith is not a valid user');
throw new \InvalidArgumentException($this->l->t('SharedWith is not a valid user'));
}
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
// We expect a valid group as sharedWith for group shares
if (!$this->groupManager->groupExists($share->getSharedWith())) {
throw new \InvalidArgumentException('SharedWith is not a valid group');
throw new \InvalidArgumentException($this->l->t('SharedWith is not a valid group'));
}
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
// No check for TYPE_EMAIL here as we have a recipient for them
if ($share->getSharedWith() !== null) {
throw new \InvalidArgumentException('SharedWith should be empty');
throw new \InvalidArgumentException($this->l->t('SharedWith should be empty'));
}
} elseif ($share->getShareType() === IShare::TYPE_EMAIL) {
if ($share->getSharedWith() === null) {
throw new \InvalidArgumentException('SharedWith should not be empty');
throw new \InvalidArgumentException($this->l->t('SharedWith should not be empty'));
}
} elseif ($share->getShareType() === IShare::TYPE_REMOTE) {
if ($share->getSharedWith() === null) {
throw new \InvalidArgumentException('SharedWith should not be empty');
throw new \InvalidArgumentException($this->l->t('SharedWith should not be empty'));
}
} elseif ($share->getShareType() === IShare::TYPE_REMOTE_GROUP) {
if ($share->getSharedWith() === null) {
throw new \InvalidArgumentException('SharedWith should not be empty');
throw new \InvalidArgumentException($this->l->t('SharedWith should not be empty'));
}
} elseif ($share->getShareType() === IShare::TYPE_CIRCLE) {
$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith());
if ($circle === null) {
throw new \InvalidArgumentException('SharedWith is not a valid circle');
throw new \InvalidArgumentException($this->l->t('SharedWith is not a valid circle'));
}
} elseif ($share->getShareType() === IShare::TYPE_ROOM) {
} elseif ($share->getShareType() === IShare::TYPE_DECK) {
} elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) {
} else {
// We cannot handle other types yet
throw new \InvalidArgumentException('unknown share type');
throw new \InvalidArgumentException($this->l->t('Unknown share type'));
}
// Verify the initiator of the share is set
if ($share->getSharedBy() === null) {
throw new \InvalidArgumentException('SharedBy should be set');
throw new \InvalidArgumentException($this->l->t('SharedBy should be set'));
}
// Cannot share with yourself
if ($share->getShareType() === IShare::TYPE_USER &&
$share->getSharedWith() === $share->getSharedBy()) {
throw new \InvalidArgumentException('Cannot share with yourself');
throw new \InvalidArgumentException($this->l->t('Cannot share with yourself'));
}
// The path should be set
if ($share->getNode() === null) {
throw new \InvalidArgumentException('Path should be set');
throw new \InvalidArgumentException($this->l->t('Path should be set'));
}
// And it should be a file or a folder
if (!($share->getNode() instanceof \OCP\Files\File) &&
!($share->getNode() instanceof \OCP\Files\Folder)) {
throw new \InvalidArgumentException('Path should be either a file or a folder');
throw new \InvalidArgumentException($this->l->t('Path should be either a file or a folder'));
}
// And you cannot share your rootfolder
@ -196,18 +196,17 @@ class Manager implements IManager {
$userFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
}
if ($userFolder->getId() === $share->getNode()->getId()) {
throw new \InvalidArgumentException('You cannot share your root folder');
throw new \InvalidArgumentException($this->l->t('You cannot share your root folder'));
}
// Check if we actually have share permissions
if (!$share->getNode()->isShareable()) {
$message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getName()]);
throw new GenericShareException($message_t, $message_t, 404);
throw new GenericShareException($this->l->t('You are not allowed to share %s', [$share->getNode()->getName()]), code: 404);
}
// Permissions should be set
if ($share->getPermissions() === null) {
throw new \InvalidArgumentException('A share requires permissions');
throw new \InvalidArgumentException($this->l->t('A share requires permissions'));
}
$permissions = 0;
@ -225,8 +224,7 @@ class Manager implements IManager {
// Check that we do not share with more permissions than we have
if ($share->getPermissions() & ~$permissions) {
$path = $userFolder->getRelativePath($share->getNode()->getPath());
$message_t = $this->l->t('Cannot increase permissions of %s', [$path]);
throw new GenericShareException($message_t, $message_t, 404);
throw new GenericShareException($this->l->t('Cannot increase permissions of %s', [$path]), code: 404);
}
@ -236,17 +234,15 @@ class Manager implements IManager {
|| $share->getShareType() === IShare::TYPE_EMAIL;
if (!$noReadPermissionRequired &&
($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) {
throw new \InvalidArgumentException('Shares need at least read permissions');
throw new \InvalidArgumentException($this->l->t('Shares need at least read permissions'));
}
if ($share->getNode() instanceof \OCP\Files\File) {
if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
$message_t = $this->l->t('Files cannot be shared with delete permissions');
throw new GenericShareException($message_t);
throw new GenericShareException($this->l->t('Files cannot be shared with delete permissions'));
}
if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
$message_t = $this->l->t('Files cannot be shared with create permissions');
throw new GenericShareException($message_t);
throw new GenericShareException($this->l->t('Files cannot be shared with create permissions'));
}
}
}
@ -287,8 +283,7 @@ class Manager implements IManager {
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
if ($date >= $expirationDate) {
$message = $this->l->t('Expiration date is in the past');
throw new GenericShareException($message, $message, 404);
throw new GenericShareException($this->l->t('Expiration date is in the past'), code: 404);
}
}
@ -313,15 +308,14 @@ class Manager implements IManager {
// If we enforce the expiration date check that is does not exceed
if ($isEnforced) {
if (empty($expirationDate)) {
throw new \InvalidArgumentException('Expiration date is enforced');
throw new \InvalidArgumentException($this->l->t('Expiration date is enforced'));
}
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
$date->add(new \DateInterval('P' . $defaultExpireDays . 'D'));
if ($date < $expirationDate) {
$message = $this->l->n('Cannot set expiration date more than %n day in the future', 'Cannot set expiration date more than %n days in the future', $defaultExpireDays);
throw new GenericShareException($message, $message, 404);
throw new GenericShareException($this->l->n('Cannot set expiration date more than %n day in the future', 'Cannot set expiration date more than %n days in the future', $defaultExpireDays), code: 404);
}
}
}
@ -367,8 +361,7 @@ class Manager implements IManager {
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
if ($date >= $expirationDate) {
$message = $this->l->t('Expiration date is in the past');
throw new GenericShareException($message, $message, 404);
throw new GenericShareException($this->l->t('Expiration date is in the past'), code: 404);
}
}
@ -394,15 +387,17 @@ class Manager implements IManager {
// If we enforce the expiration date check that is does not exceed
if ($isEnforced) {
if (empty($expirationDate)) {
throw new \InvalidArgumentException('Expiration date is enforced');
throw new \InvalidArgumentException($this->l->t('Expiration date is enforced'));
}
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$date->setTime(0, 0, 0);
$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
if ($date < $expirationDate) {
$message = $this->l->n('Cannot set expiration date more than %n day in the future', 'Cannot set expiration date more than %n days in the future', $this->shareApiLinkDefaultExpireDays());
throw new GenericShareException($message, $message, 404);
throw new GenericShareException(
$this->l->n('Cannot set expiration date more than %n day in the future', 'Cannot set expiration date more than %n days in the future', $this->shareApiLinkDefaultExpireDays()),
code: 404,
);
}
}
@ -448,8 +443,7 @@ class Manager implements IManager {
$groups = array_diff($groups, $excludedGroups);
if (empty($groups)) {
$message_t = $this->l->t('Sharing is only allowed with group members');
throw new \Exception($message_t);
throw new \Exception($this->l->t('Sharing is only allowed with group members'));
}
}
@ -472,8 +466,7 @@ class Manager implements IManager {
// Identical share already exists
if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
$message = $this->l->t('Sharing %s failed, because this item is already shared with the account %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]);
throw new AlreadySharedException($message, $existingShare);
throw new AlreadySharedException($this->l->t('Sharing %s failed, because this item is already shared with the account %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]), $existingShare);
}
// The share is already shared with this user via a group share
@ -483,8 +476,7 @@ class Manager implements IManager {
$user = $this->userManager->get($share->getSharedWith());
if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) {
$message = $this->l->t('Sharing %s failed, because this item is already shared with the account %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]);
throw new AlreadySharedException($message, $existingShare);
throw new AlreadySharedException($this->l->t('Sharing %s failed, because this item is already shared with the account %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]), $existingShare);
}
}
}
@ -500,7 +492,7 @@ class Manager implements IManager {
protected function groupCreateChecks(IShare $share) {
// Verify group shares are allowed
if (!$this->allowGroupSharing()) {
throw new \Exception('Group sharing is now allowed');
throw new \Exception($this->l->t('Group sharing is now allowed'));
}
// Verify if the user can share with this group
@ -511,7 +503,7 @@ class Manager implements IManager {
// optional excluded groups
$excludedGroups = $this->shareWithGroupMembersOnlyExcludeGroupsList();
if (is_null($sharedWith) || in_array($share->getSharedWith(), $excludedGroups) || !$sharedWith->inGroup($sharedBy)) {
throw new \Exception('Sharing is only allowed within your own groups');
throw new \Exception($this->l->t('Sharing is only allowed within your own groups'));
}
}
@ -532,7 +524,7 @@ class Manager implements IManager {
}
if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) {
throw new AlreadySharedException('Path is already shared with this group', $existingShare);
throw new AlreadySharedException($this->l->t('Path is already shared with this group'), $existingShare);
}
}
}
@ -546,13 +538,13 @@ class Manager implements IManager {
protected function linkCreateChecks(IShare $share) {
// Are link shares allowed?
if (!$this->shareApiAllowLinks()) {
throw new \Exception('Link sharing is not allowed');
throw new \Exception($this->l->t('Link sharing is not allowed'));
}
// Check if public upload is allowed
if ($share->getNodeType() === 'folder' && !$this->shareApiLinkAllowPublicUpload() &&
($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) {
throw new \InvalidArgumentException('Public upload is not allowed');
throw new \InvalidArgumentException($this->l->t('Public upload is not allowed'));
}
}
@ -587,7 +579,7 @@ class Manager implements IManager {
$mounts = $this->mountManager->findIn($path->getPath());
foreach ($mounts as $mount) {
if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) {
throw new \InvalidArgumentException('Path contains files shared with you');
throw new \InvalidArgumentException($this->l->t('Path contains files shared with you'));
}
}
}
@ -601,11 +593,11 @@ class Manager implements IManager {
*/
protected function canShare(IShare $share) {
if (!$this->shareApiEnabled()) {
throw new \Exception('Sharing is disabled');
throw new \Exception($this->l->t('Sharing is disabled'));
}
if ($this->sharingDisabledForUser($share->getSharedBy())) {
throw new \Exception('Sharing is disabled for you');
throw new \Exception($this->l->t('Sharing is disabled for you'));
}
}
@ -689,7 +681,7 @@ class Manager implements IManager {
// Cannot share with the owner
if ($share->getShareType() === IShare::TYPE_USER &&
$share->getSharedWith() === $share->getShareOwner()) {
throw new \InvalidArgumentException('Cannot share with the share owner');
throw new \InvalidArgumentException($this->l->t('Cannot share with the share owner'));
}
// Generate the target
@ -724,7 +716,8 @@ class Manager implements IManager {
$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
// If a share for the same target already exists, dont create a new one,
// but do trigger the hooks and notifications again
$oldShare = $share;
// Reuse the node we already have
@ -769,24 +762,24 @@ class Manager implements IManager {
try {
$originalShare = $this->getShareById($share->getFullId());
} catch (\UnexpectedValueException $e) {
throw new \InvalidArgumentException('Share does not have a full id');
throw new \InvalidArgumentException($this->l->t('Share does not have a full id'));
}
// We cannot change the share type!
if ($share->getShareType() !== $originalShare->getShareType()) {
throw new \InvalidArgumentException('Cannot change share type');
throw new \InvalidArgumentException($this->l->t('Cannot change share type'));
}
// We can only change the recipient on user shares
if ($share->getSharedWith() !== $originalShare->getSharedWith() &&
$share->getShareType() !== IShare::TYPE_USER) {
throw new \InvalidArgumentException('Can only update recipient on user shares');
throw new \InvalidArgumentException($this->l->t('Can only update recipient on user shares'));
}
// Cannot share with the owner
if ($share->getShareType() === IShare::TYPE_USER &&
$share->getSharedWith() === $share->getShareOwner()) {
throw new \InvalidArgumentException('Cannot share with the share owner');
throw new \InvalidArgumentException($this->l->t('Cannot share with the share owner'));
}
$this->generalCreateChecks($share, true);
@ -795,7 +788,7 @@ class Manager implements IManager {
$this->userCreateChecks($share);
if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
//Verify the expiration date
// Verify the expiration date
$this->validateExpirationDateInternal($share);
$expirationDateUpdated = true;
}
@ -803,7 +796,7 @@ class Manager implements IManager {
$this->groupCreateChecks($share);
if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
//Verify the expiration date
// Verify the expiration date
$this->validateExpirationDateInternal($share);
$expirationDateUpdated = true;
}
@ -821,7 +814,7 @@ class Manager implements IManager {
* Cannot enable the getSendPasswordByTalk if there is no password set
*/
if (empty($plainTextPassword) && $share->getSendPasswordByTalk()) {
throw new \InvalidArgumentException('Cannot enable sending the password by Talk with an empty password');
throw new \InvalidArgumentException($this->l->t('Cannot enable sending the password by Talk with an empty password'));
}
/**
@ -831,10 +824,10 @@ class Manager implements IManager {
*/
if (!$updatedPassword && $share->getShareType() === IShare::TYPE_EMAIL) {
if (!$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) {
throw new \InvalidArgumentException('Cannot enable sending the password by Talk without setting a new password');
throw new \InvalidArgumentException($this->l->t('Cannot enable sending the password by Talk without setting a new password'));
}
if ($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()) {
throw new \InvalidArgumentException('Cannot disable sending the password by Talk without setting a new password');
throw new \InvalidArgumentException($this->l->t('Cannot disable sending the password by Talk without setting a new password'));
}
}
@ -845,7 +838,7 @@ class Manager implements IManager {
}
} elseif ($share->getShareType() === IShare::TYPE_REMOTE || $share->getShareType() === IShare::TYPE_REMOTE_GROUP) {
if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
//Verify the expiration date
// Verify the expiration date
$this->validateExpirationDateInternal($share);
$expirationDateUpdated = true;
}
@ -915,7 +908,7 @@ class Manager implements IManager {
$provider = $this->factory->getProvider($providerId);
if (!($provider instanceof IShareProviderSupportsAccept)) {
throw new \InvalidArgumentException('Share provider does not support accepting');
throw new \InvalidArgumentException($this->l->t('Share provider does not support accepting'));
}
/** @var IShareProvider&IShareProviderSupportsAccept $provider */
$provider->acceptShare($share, $recipientId);
@ -944,7 +937,7 @@ class Manager implements IManager {
// Password updated.
if ($passwordsAreDifferent) {
//Verify the password
// Verify the password
$this->verifyPassword($share->getPassword());
// If a password is set. Hash it!
@ -1028,7 +1021,7 @@ class Manager implements IManager {
try {
$share->getFullId();
} catch (\UnexpectedValueException $e) {
throw new \InvalidArgumentException('Share does not have a full id');
throw new \InvalidArgumentException($this->l->t('Share does not have a full id'));
}
$this->dispatcher->dispatchTyped(new BeforeShareDeletedEvent($share));
@ -1075,21 +1068,21 @@ class Manager implements IManager {
public function moveShare(IShare $share, $recipientId) {
if ($share->getShareType() === IShare::TYPE_LINK
|| $share->getShareType() === IShare::TYPE_EMAIL) {
throw new \InvalidArgumentException('Cannot change target of link share');
throw new \InvalidArgumentException($this->l->t('Cannot change target of link share'));
}
if ($share->getShareType() === IShare::TYPE_USER && $share->getSharedWith() !== $recipientId) {
throw new \InvalidArgumentException('Invalid recipient');
throw new \InvalidArgumentException($this->l->t('Invalid recipient'));
}
if ($share->getShareType() === IShare::TYPE_GROUP) {
$sharedWith = $this->groupManager->get($share->getSharedWith());
if (is_null($sharedWith)) {
throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist');
throw new \InvalidArgumentException($this->l->t('Group "%s" does not exist', [$share->getSharedWith()]));
}
$recipient = $this->userManager->get($recipientId);
if (!$sharedWith->inGroup($recipient)) {
throw new \InvalidArgumentException('Invalid recipient');
throw new \InvalidArgumentException($this->l->t('Invalid recipient'));
}
}
@ -1125,7 +1118,7 @@ class Manager implements IManager {
if ($path !== null &&
!($path instanceof \OCP\Files\File) &&
!($path instanceof \OCP\Files\Folder)) {
throw new \InvalidArgumentException('invalid path');
throw new \InvalidArgumentException($this->l->t('Invalid path'));
}
try {
@ -1149,7 +1142,7 @@ class Manager implements IManager {
try {
$this->checkShare($share);
} catch (ShareNotFound $e) {
//Ignore since this basically means the share is deleted
// Ignore since this basically means the share is deleted
continue;
}

@ -652,7 +652,7 @@ class ManagerTest extends \Test\TestCase {
[$this->createShare(null, IShare::TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
[$this->createShare(null, IShare::TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
[$this->createShare(null, IShare::TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'SharedWith should be empty', true],
[$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'unknown share type', true],
[$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'Unknown share type', true],
[$this->createShare(null, IShare::TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'SharedBy should be set', true],
[$this->createShare(null, IShare::TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'SharedBy should be set', true],