summaryrefslogtreecommitdiffstats
path: root/apps/sharebymail/lib
diff options
context:
space:
mode:
authorBjoern Schiessle <bjoern@schiessle.org>2016-10-26 11:04:00 +0200
committerBjoern Schiessle <bjoern@schiessle.org>2016-11-01 19:54:40 +0100
commit31c8c38bd62af35c1578a8342b05580a9bd67466 (patch)
tree71e2eeb82c6b4ae716382c0b4cb744d2bff652df /apps/sharebymail/lib
parent7436e45c91e139f99b72235c8fd577901e1596db (diff)
downloadnextcloud-server-31c8c38bd62af35c1578a8342b05580a9bd67466.tar.gz
nextcloud-server-31c8c38bd62af35c1578a8342b05580a9bd67466.zip
send mail for share-by-mail shares
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
Diffstat (limited to 'apps/sharebymail/lib')
-rw-r--r--apps/sharebymail/lib/ShareByMailProvider.php83
1 files changed, 77 insertions, 6 deletions
diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php
index e432ab139ed..c0c21bdb16c 100644
--- a/apps/sharebymail/lib/ShareByMailProvider.php
+++ b/apps/sharebymail/lib/ShareByMailProvider.php
@@ -21,12 +21,15 @@
namespace OCA\ShareByMail;
+use OC\HintException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\ILogger;
+use OCP\IURLGenerator;
use OCP\IUserManager;
+use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use OC\Share20\Share;
use OCP\Share\IShare;
@@ -57,6 +60,12 @@ class ShareByMailProvider implements IShareProvider {
/** @var IL10N */
private $l;
+ /** @var IMailer */
+ private $mailer;
+
+ /** @var IURLGenerator */
+ private $urlGenerator;
+
/**
* Return the identifier of this provider.
*
@@ -75,6 +84,8 @@ class ShareByMailProvider implements IShareProvider {
* @param IRootFolder $rootFolder
* @param IL10N $l
* @param ILogger $logger
+ * @param IMailer $mailer
+ * @param IURLGenerator $urlGenerator
*/
public function __construct(
IDBConnection $connection,
@@ -82,7 +93,9 @@ class ShareByMailProvider implements IShareProvider {
IUserManager $userManager,
IRootFolder $rootFolder,
IL10N $l,
- ILogger $logger
+ ILogger $logger,
+ IMailer $mailer,
+ IURLGenerator $urlGenerator
) {
$this->dbConnection = $connection;
$this->secureRandom = $secureRandom;
@@ -90,6 +103,8 @@ class ShareByMailProvider implements IShareProvider {
$this->rootFolder = $rootFolder;
$this->l = $l;
$this->logger = $logger;
+ $this->mailer = $mailer;
+ $this->urlGenerator = $urlGenerator;
}
/**
@@ -127,7 +142,7 @@ class ShareByMailProvider implements IShareProvider {
* @throws \Exception
*/
private function createMailShare(IShare $share) {
- $token = $this->generateToken();
+ $share->setToken($this->generateToken());
$shareId = $this->addShareToDB(
$share->getNodeId(),
$share->getNodeType(),
@@ -135,21 +150,77 @@ class ShareByMailProvider implements IShareProvider {
$share->getSharedBy(),
$share->getShareOwner(),
$share->getPermissions(),
- $token
+ $share->getToken()
);
try {
- // TODO: Send email with public link
+ $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
+ ['token' => $share->getToken()]);
+ $this->sendMailNotification($share->getNode()->getName(),
+ $link,
+ $share->getShareOwner(),
+ $share->getSharedBy(), $share->getSharedWith());
+ } catch (HintException $hintException) {
+ $this->logger->error('Failed to send share by mail: ' . $hintException->getMessage());
+ $this->removeShareFromTable($shareId);
+ throw $hintException;
} catch (\Exception $e) {
- $this->logger->error('Failed to notify remote server of federated share, removing share (' . $e->getMessage() . ')');
+ $this->logger->error('Failed to send share by mail: ' . $e->getMessage());
$this->removeShareFromTable($shareId);
- throw $e;
+ throw new HintException('Failed to send share by mail',
+ $this->l->t('Failed to send share by E-mail'));
}
return $shareId;
}
+ private function sendMailNotification($filename, $link, $owner, $initiator, $shareWith) {
+ if ($owner === $initiator) {
+ $subject = (string)$this->l->t('%s shared »%s« with you', array($owner, $filename));
+ } else {
+ $subject = (string)$this->l->t('%s shared »%s« with you on behalf of %s', array($owner, $filename, $initiator));
+ }
+
+ $message = $this->mailer->createMessage();
+ $htmlBody = $this->createMailBody('mail', $filename, $link, $owner, $initiator);
+ $textBody = $this->createMailBody('altmail', $filename, $link, $owner, $initiator);
+ $message->setTo([$shareWith]);
+ $message->setSubject($subject);
+ $message->setBody($textBody, 'text/plain');
+ $message->setHtmlBody($htmlBody);
+ $this->mailer->send($message);
+
+ }
+
+ /**
+ * create mail body
+ *
+ * @param $filename
+ * @param $link
+ * @param $owner
+ * @param $initiator
+ * @return string plain text mail
+ * @throws HintException
+ */
+ protected function createMailBody($template, $filename, $link, $owner, $initiator) {
+
+ $mailBodyTemplate = new \OC_Template('sharebymail', $template, '');
+ $mailBodyTemplate->assign ('filename', $filename);
+ $mailBodyTemplate->assign ('link', $link);
+ $mailBodyTemplate->assign ('owner', $owner);
+ $mailBodyTemplate->assign ('initiator', $initiator);
+ $mailBodyTemplate->assign ('onBehalfOf', $initiator !== $owner);
+ $mailBody = $mailBodyTemplate->fetchPage();
+
+ if (is_string($mailBody)) {
+ return $mailBody;
+ }
+
+ throw new HintException('Failed to create the E-mail',
+ $this->l->t('Failed to create the E-mail'));
+ }
+
/**
* generate share token
*