diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2017-08-01 13:27:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-01 13:27:15 +0200 |
commit | c8452803eff9993a3946656def8596657302033c (patch) | |
tree | 1b4179162451ef9061c1158a3d6d7fd1d1c03bd9 /lib | |
parent | e73fdad88c12509fb06778f9224dddf66f1c9910 (diff) | |
parent | a309fa7d6b2b5b83ea266b081f74852a3988c9b2 (diff) | |
download | nextcloud-server-c8452803eff9993a3946656def8596657302033c.tar.gz nextcloud-server-c8452803eff9993a3946656def8596657302033c.zip |
Merge pull request #5897 from nextcloud/add-share-mail-for-user-share
Send an email once a file/folder is shared with a user
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Server.php | 5 | ||||
-rw-r--r-- | lib/private/Share20/Manager.php | 101 |
2 files changed, 104 insertions, 2 deletions
diff --git a/lib/private/Server.php b/lib/private/Server.php index 1f1570f3f46..60a5de97bbf 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -964,7 +964,10 @@ class Server extends ServerContainer implements IServerContainer { $factory, $c->getUserManager(), $c->getLazyRootFolder(), - $c->getEventDispatcher() + $c->getEventDispatcher(), + $c->getMailer(), + $c->getURLGenerator(), + $c->getThemingDefaults() ); return $manager; diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index ca9d8e2843f..f7f090f261f 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -31,6 +31,7 @@ use OC\Cache\CappedMemoryCache; use OC\Files\Mount\MoveableMount; use OC\HintException; use OC\Share20\Exception\ProviderException; +use OCP\Defaults; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\IRootFolder; @@ -40,7 +41,9 @@ use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; use OCP\ILogger; +use OCP\IURLGenerator; use OCP\IUserManager; +use OCP\Mail\IMailer; use OCP\Security\IHasher; use OCP\Security\ISecureRandom; use OCP\Share\Exceptions\GenericShareException; @@ -82,6 +85,12 @@ class Manager implements IManager { private $eventDispatcher; /** @var LegacyHooks */ private $legacyHooks; + /** @var IMailer */ + private $mailer; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var \OC_Defaults */ + private $defaults; /** @@ -98,6 +107,9 @@ class Manager implements IManager { * @param IUserManager $userManager * @param IRootFolder $rootFolder * @param EventDispatcher $eventDispatcher + * @param IMailer $mailer + * @param IURLGenerator $urlGenerator + * @param \OC_Defaults $defaults */ public function __construct( ILogger $logger, @@ -110,7 +122,10 @@ class Manager implements IManager { IProviderFactory $factory, IUserManager $userManager, IRootFolder $rootFolder, - EventDispatcher $eventDispatcher + EventDispatcher $eventDispatcher, + IMailer $mailer, + IURLGenerator $urlGenerator, + \OC_Defaults $defaults ) { $this->logger = $logger; $this->config = $config; @@ -125,6 +140,9 @@ class Manager implements IManager { $this->eventDispatcher = $eventDispatcher; $this->sharingDisabledForUsersCache = new CappedMemoryCache(); $this->legacyHooks = new LegacyHooks($this->eventDispatcher); + $this->mailer = $mailer; + $this->urlGenerator = $urlGenerator; + $this->defaults = $defaults; } /** @@ -664,10 +682,91 @@ class Manager implements IManager { \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { + $user = $this->userManager->get($share->getSharedWith()); + if ($user !== null) { + $emailAddress = $user->getEMailAddress(); + if ($emailAddress !== null && $emailAddress !== '') { + $this->sendMailNotification( + $share->getNode()->getName(), + $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', [ 'fileid' => $share->getNode()->getId() ]), + $share->getSharedBy(), + $emailAddress + ); + $this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); + } else { + $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); + } + } else { + $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); + } + } + return $share; } /** + * @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 address of share receiver + * @throws \Exception If mail couldn't be sent + */ + protected function sendMailNotification($filename, + $link, + $initiator, + $shareWith) { + $initiatorUser = $this->userManager->get($initiator); + $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; + $subject = (string)$this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); + + $message = $this->mailer->createMessage(); + + $emailTemplate = $this->mailer->createEMailTemplate(); + + $emailTemplate->addHeader(); + $emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); + $text = $this->l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); + + $emailTemplate->addBodyText( + $text . ' ' . $this->l->t('Click the button below to open it.'), + $text + ); + $emailTemplate->addBodyButton( + $this->l->t('Open »%s«', [$filename]), + $link + ); + + $message->setTo([$shareWith]); + + // The "From" contains the sharers name + $instanceName = $this->defaults->getName(); + $senderName = $this->l->t( + '%s via %s', + [ + $initiatorDisplayName, + $instanceName + ] + ); + $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); + + // The "Reply-To" is set to the sharer if an mail address is configured + // also the default footer contains a "Do not reply" which needs to be adjusted. + $initiatorEmail = $initiatorUser->getEMailAddress(); + if($initiatorEmail !== null) { + $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); + $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); + } else { + $emailTemplate->addFooter(); + } + + $message->setSubject($subject); + $message->setPlainBody($emailTemplate->renderText()); + $message->setHtmlBody($emailTemplate->renderHtml()); + $this->mailer->send($message); + } + + /** * Update a share * * @param \OCP\Share\IShare $share |