diff options
author | skjnldsv <skjnldsv@protonmail.com> | 2024-07-05 09:47:40 +0200 |
---|---|---|
committer | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2024-07-12 20:14:30 +0200 |
commit | d388370c3b4bd50d1ad7668aef9000bd54a8c442 (patch) | |
tree | 90b98683ee1245ff50c233a7cebe0a2130913c26 /apps | |
parent | 443c48aefb70983afefce48652880a1bcaad4d53 (diff) | |
download | nextcloud-server-d388370c3b4bd50d1ad7668aef9000bd54a8c442.tar.gz nextcloud-server-d388370c3b4bd50d1ad7668aef9000bd54a8c442.zip |
feat(files_sharing): implement IShareProviderWithNotification and refactor sendMailNotification
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_sharing/appinfo/routes.php | 5 | ||||
-rw-r--r-- | apps/files_sharing/lib/Controller/ShareAPIController.php | 56 | ||||
-rw-r--r-- | apps/sharebymail/lib/ShareByMailProvider.php | 37 |
3 files changed, 47 insertions, 51 deletions
diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index bd8bfaf38e5..91f8271c143 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -117,6 +117,11 @@ return [ 'verb' => 'DELETE', ], [ + 'name' => 'ShareAPI#sendShareEmail', + 'url' => '/api/v1/shares/{id}/send-email', + 'verb' => 'POST', + ], + [ 'name' => 'ShareAPI#acceptShare', 'url' => '/api/v1/shares/pending/{id}', 'verb' => 'POST', diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index 5a939e88914..5e7e2b9c872 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -13,14 +13,14 @@ use Exception; use OC\Files\FileInfo; use OC\Files\Storage\Wrapper\Wrapper; use OC\Share20\Exception\ProviderException; -use OCA\Files\Helper; use OCA\Files_Sharing\Exceptions\SharingRightsException; use OCA\Files_Sharing\External\Storage; -use OCA\Files_Sharing\ResponseDefinitions; use OCA\Files_Sharing\SharedStorage; +use OCA\Files\Helper; use OCA\ShareByMail\ShareByMailProvider; use OCP\App\IAppManager; -use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\BruteForceProtection; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\OCS\OCSBadRequestException; @@ -51,6 +51,7 @@ use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; use OCP\Share\IProviderFactory; use OCP\Share\IShare; +use OCP\Share\IShareProviderWithNotification; use OCP\UserStatus\IManager as IUserStatusManager; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -2032,46 +2033,29 @@ class ShareAPIController extends OCSController { } } - public function sendShareEmail(int $shareId, $emails = []) { + /** + * @param string $id + * @param string[] $emails a list of emails to send the notification to + * @return void + */ + #[NoAdminRequired] + #[BruteForceProtection(action: 'sendShareEmail')] + public function sendShareEmail(string $id, $emails = []) { try { - $share = $this->shareManager->getShareById($shareId); - - // Only mail and link shares are supported - if ($share->getShareType() !== IShare::TYPE_EMAIL - && $share->getShareType() !== IShare::TYPE_LINK) { - throw new OCSBadRequestException('Only email and link shares are supported'); - } - - // Allow sending the mail again if the share is an email share - if ($share->getShareType() === IShare::TYPE_EMAIL && count($emails) !== 0) { - throw new OCSBadRequestException('Emails should not be provided for email shares'); - } - - // Allow sending a mail if the share is a link share AND a list of emails is provided - if ($share->getShareType() === IShare::TYPE_LINK && count($emails) === 0) { - throw new OCSBadRequestException('Emails should be provided for link shares'); - } - - $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', - ['token' => $share->getToken()]); + $share = $this->getShareById($id); try { - /** @var ShareByMailProvider */ - $provider = $this->factory->getProviderForType(IShare::TYPE_EMAIL); - $provider->sendMailNotification( - $share->getNode()->getName(), - $link, - $share->getSharedBy(), - $share->getSharedWith(), - $share->getExpirationDate(), - $share->getNote() - ); + $provider = $this->factory->getProviderForType($share->getShareType()); + if (!($provider instanceof IShareProviderWithNotification)) { + throw new OCSBadRequestException($this->l->t('No mail notification configured for this share type')); + } + + $provider->sendMailNotification($share); return new JSONResponse(['message' => 'ok']); - } catch (ProviderException $e) { - throw new OCSBadRequestException($this->l->t('Sending mail notification is not enabled')); } catch (Exception $e) { throw new OCSException($this->l->t('Error while sending mail notification')); } + } catch (ShareNotFound $e) { throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); } diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 4b3162c1982..3ff4f3db82e 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -31,7 +31,7 @@ use OCP\Share\Exceptions\GenericShareException; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; -use OCP\Share\IShareProvider; +use OCP\Share\IShareProviderWithNotification; use Psr\Log\LoggerInterface; /** @@ -39,7 +39,7 @@ use Psr\Log\LoggerInterface; * * @package OCA\ShareByMail */ -class ShareByMailProvider implements IShareProvider { +class ShareByMailProvider implements IShareProviderWithNotification { /** * Return the identifier of this provider. * @@ -216,7 +216,7 @@ class ShareByMailProvider implements IShareProvider { */ protected function createMailShare(IShare $share): int { $share->setToken($this->generateToken()); - $shareId = $this->addShareToDB( + return $this->addShareToDB( $share->getNodeId(), $share->getNodeType(), $share->getSharedWith(), @@ -232,7 +232,13 @@ class ShareByMailProvider implements IShareProvider { $share->getExpirationDate(), $share->getNote() ); + } + /** + * @inheritDoc + */ + public function sendMailNotification(IShare $share): bool { + $shareId = $share->getId(); if (!$this->mailer->validateMailAddress($share->getSharedWith())) { $this->removeShareFromTable($shareId); $e = new HintException('Failed to send share by mail. Got an invalid email address: ' . $share->getSharedWith(), @@ -246,7 +252,7 @@ class ShareByMailProvider implements IShareProvider { try { $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); - $this->sendMailNotification( + $this->sendEmail( $share->getNode()->getName(), $link, $share->getSharedBy(), @@ -254,6 +260,7 @@ class ShareByMailProvider implements IShareProvider { $share->getExpirationDate(), $share->getNote() ); + return true; } catch (HintException $hintException) { $this->logger->error('Failed to send share by mail.', [ 'app' => 'sharebymail', @@ -270,14 +277,19 @@ class ShareByMailProvider implements IShareProvider { throw new HintException('Failed to send share by mail', $this->l->t('Failed to send share by email')); } - - return $shareId; + return false; } /** + * @param string $filename file/folder name + * @param string $link link to the file/folder + * @param string $initiator user ID of share sender + * @param string $shareWith email addresses + * @param \DateTime|null $expiration expiration date + * @param string $note note * @throws \Exception If mail couldn't be sent */ - public function sendMailNotification( + public function sendEmail( string $filename, string $link, string $initiator, @@ -348,7 +360,7 @@ class ShareByMailProvider implements IShareProvider { /** * send password to recipient of a mail share */ - protected function sendPassword(IShare $share, string $password): bool { + public function sendPassword(IShare $share, string $password): bool { $filename = $share->getNode()->getName(); $initiator = $share->getSharedBy(); $shareWith = $share->getSharedWith(); @@ -607,18 +619,13 @@ class ShareByMailProvider implements IShareProvider { ->setValue('stime', $qb->createNamedParameter(time())) ->setValue('hide_download', $qb->createNamedParameter((int)$hideDownload, IQueryBuilder::PARAM_INT)) ->setValue('label', $qb->createNamedParameter($label)) - ->setValue('note', $qb->createNamedParameter($note)); + ->setValue('note', $qb->createNamedParameter($note)) + ->setValue('mail_send', $qb->createNamedParameter(1)); if ($expirationTime !== null) { $qb->setValue('expiration', $qb->createNamedParameter($expirationTime, IQueryBuilder::PARAM_DATE)); } - /* - * Added to fix https://github.com/owncloud/core/issues/22215 - * Can be removed once we get rid of ajax/share.php - */ - $qb->setValue('file_target', $qb->createNamedParameter('')); - $qb->executeStatement(); return $qb->getLastInsertId(); } |