Merge pull request #5897 from nextcloud/add-share-mail-for-user-share
Send an email once a file/folder is shared with a user
This commit is contained in:
commit
c8452803ef
|
@ -92,7 +92,7 @@ class RemoteShare implements ISetting {
|
||||||
* @since 11.0.0
|
* @since 11.0.0
|
||||||
*/
|
*/
|
||||||
public function isDefaultEnabledMail() {
|
public function isDefaultEnabledMail() {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ class Shared implements ISetting {
|
||||||
* @since 11.0.0
|
* @since 11.0.0
|
||||||
*/
|
*/
|
||||||
public function isDefaultEnabledMail() {
|
public function isDefaultEnabledMail() {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -964,7 +964,10 @@ class Server extends ServerContainer implements IServerContainer {
|
||||||
$factory,
|
$factory,
|
||||||
$c->getUserManager(),
|
$c->getUserManager(),
|
||||||
$c->getLazyRootFolder(),
|
$c->getLazyRootFolder(),
|
||||||
$c->getEventDispatcher()
|
$c->getEventDispatcher(),
|
||||||
|
$c->getMailer(),
|
||||||
|
$c->getURLGenerator(),
|
||||||
|
$c->getThemingDefaults()
|
||||||
);
|
);
|
||||||
|
|
||||||
return $manager;
|
return $manager;
|
||||||
|
|
|
@ -31,6 +31,7 @@ use OC\Cache\CappedMemoryCache;
|
||||||
use OC\Files\Mount\MoveableMount;
|
use OC\Files\Mount\MoveableMount;
|
||||||
use OC\HintException;
|
use OC\HintException;
|
||||||
use OC\Share20\Exception\ProviderException;
|
use OC\Share20\Exception\ProviderException;
|
||||||
|
use OCP\Defaults;
|
||||||
use OCP\Files\File;
|
use OCP\Files\File;
|
||||||
use OCP\Files\Folder;
|
use OCP\Files\Folder;
|
||||||
use OCP\Files\IRootFolder;
|
use OCP\Files\IRootFolder;
|
||||||
|
@ -40,7 +41,9 @@ use OCP\IConfig;
|
||||||
use OCP\IGroupManager;
|
use OCP\IGroupManager;
|
||||||
use OCP\IL10N;
|
use OCP\IL10N;
|
||||||
use OCP\ILogger;
|
use OCP\ILogger;
|
||||||
|
use OCP\IURLGenerator;
|
||||||
use OCP\IUserManager;
|
use OCP\IUserManager;
|
||||||
|
use OCP\Mail\IMailer;
|
||||||
use OCP\Security\IHasher;
|
use OCP\Security\IHasher;
|
||||||
use OCP\Security\ISecureRandom;
|
use OCP\Security\ISecureRandom;
|
||||||
use OCP\Share\Exceptions\GenericShareException;
|
use OCP\Share\Exceptions\GenericShareException;
|
||||||
|
@ -82,6 +85,12 @@ class Manager implements IManager {
|
||||||
private $eventDispatcher;
|
private $eventDispatcher;
|
||||||
/** @var LegacyHooks */
|
/** @var LegacyHooks */
|
||||||
private $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 IUserManager $userManager
|
||||||
* @param IRootFolder $rootFolder
|
* @param IRootFolder $rootFolder
|
||||||
* @param EventDispatcher $eventDispatcher
|
* @param EventDispatcher $eventDispatcher
|
||||||
|
* @param IMailer $mailer
|
||||||
|
* @param IURLGenerator $urlGenerator
|
||||||
|
* @param \OC_Defaults $defaults
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ILogger $logger,
|
ILogger $logger,
|
||||||
|
@ -110,7 +122,10 @@ class Manager implements IManager {
|
||||||
IProviderFactory $factory,
|
IProviderFactory $factory,
|
||||||
IUserManager $userManager,
|
IUserManager $userManager,
|
||||||
IRootFolder $rootFolder,
|
IRootFolder $rootFolder,
|
||||||
EventDispatcher $eventDispatcher
|
EventDispatcher $eventDispatcher,
|
||||||
|
IMailer $mailer,
|
||||||
|
IURLGenerator $urlGenerator,
|
||||||
|
\OC_Defaults $defaults
|
||||||
) {
|
) {
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
@ -125,6 +140,9 @@ class Manager implements IManager {
|
||||||
$this->eventDispatcher = $eventDispatcher;
|
$this->eventDispatcher = $eventDispatcher;
|
||||||
$this->sharingDisabledForUsersCache = new CappedMemoryCache();
|
$this->sharingDisabledForUsersCache = new CappedMemoryCache();
|
||||||
$this->legacyHooks = new LegacyHooks($this->eventDispatcher);
|
$this->legacyHooks = new LegacyHooks($this->eventDispatcher);
|
||||||
|
$this->mailer = $mailer;
|
||||||
|
$this->urlGenerator = $urlGenerator;
|
||||||
|
$this->defaults = $defaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -664,9 +682,90 @@ class Manager implements IManager {
|
||||||
|
|
||||||
\OC_Hook::emit('OCP\Share', 'post_shared', $postHookData);
|
\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;
|
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
|
* Update a share
|
||||||
*
|
*
|
||||||
|
|
|
@ -23,6 +23,7 @@ namespace Test\Share20;
|
||||||
use OC\Files\Mount\MoveableMount;
|
use OC\Files\Mount\MoveableMount;
|
||||||
use OC\HintException;
|
use OC\HintException;
|
||||||
use OC\Share20\DefaultShareProvider;
|
use OC\Share20\DefaultShareProvider;
|
||||||
|
use OCP\Defaults;
|
||||||
use OCP\Files\File;
|
use OCP\Files\File;
|
||||||
use OCP\Files\Folder;
|
use OCP\Files\Folder;
|
||||||
use OCP\Files\IRootFolder;
|
use OCP\Files\IRootFolder;
|
||||||
|
@ -31,8 +32,10 @@ use OCP\Files\Node;
|
||||||
use OCP\Files\Storage;
|
use OCP\Files\Storage;
|
||||||
use OCP\IGroup;
|
use OCP\IGroup;
|
||||||
use OCP\IServerContainer;
|
use OCP\IServerContainer;
|
||||||
|
use OCP\IURLGenerator;
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
use OCP\IUserManager;
|
use OCP\IUserManager;
|
||||||
|
use OCP\Mail\IMailer;
|
||||||
use OCP\Share\Exceptions\ShareNotFound;
|
use OCP\Share\Exceptions\ShareNotFound;
|
||||||
use OCP\Share\IProviderFactory;
|
use OCP\Share\IProviderFactory;
|
||||||
use OCP\Share\IShare;
|
use OCP\Share\IShare;
|
||||||
|
@ -85,6 +88,12 @@ class ManagerTest extends \Test\TestCase {
|
||||||
protected $rootFolder;
|
protected $rootFolder;
|
||||||
/** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
|
/** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
|
||||||
protected $eventDispatcher;
|
protected $eventDispatcher;
|
||||||
|
/** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $mailer;
|
||||||
|
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $urlGenerator;
|
||||||
|
/** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $defaults;
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
|
|
||||||
|
@ -97,6 +106,9 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$this->userManager = $this->createMock(IUserManager::class);
|
$this->userManager = $this->createMock(IUserManager::class);
|
||||||
$this->rootFolder = $this->createMock(IRootFolder::class);
|
$this->rootFolder = $this->createMock(IRootFolder::class);
|
||||||
$this->eventDispatcher = $this->createMock(EventDispatcher::class);
|
$this->eventDispatcher = $this->createMock(EventDispatcher::class);
|
||||||
|
$this->mailer = $this->createMock(IMailer::class);
|
||||||
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
||||||
|
$this->defaults = $this->createMock(\OC_Defaults::class);
|
||||||
|
|
||||||
$this->l = $this->createMock(IL10N::class);
|
$this->l = $this->createMock(IL10N::class);
|
||||||
$this->l->method('t')
|
$this->l->method('t')
|
||||||
|
@ -117,7 +129,10 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$this->factory,
|
$this->factory,
|
||||||
$this->userManager,
|
$this->userManager,
|
||||||
$this->rootFolder,
|
$this->rootFolder,
|
||||||
$this->eventDispatcher
|
$this->eventDispatcher,
|
||||||
|
$this->mailer,
|
||||||
|
$this->urlGenerator,
|
||||||
|
$this->defaults
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->defaultProvider = $this->createMock(DefaultShareProvider::class);
|
$this->defaultProvider = $this->createMock(DefaultShareProvider::class);
|
||||||
|
@ -141,7 +156,10 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$this->factory,
|
$this->factory,
|
||||||
$this->userManager,
|
$this->userManager,
|
||||||
$this->rootFolder,
|
$this->rootFolder,
|
||||||
$this->eventDispatcher
|
$this->eventDispatcher,
|
||||||
|
$this->mailer,
|
||||||
|
$this->urlGenerator,
|
||||||
|
$this->defaults
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2108,7 +2126,10 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$factory,
|
$factory,
|
||||||
$this->userManager,
|
$this->userManager,
|
||||||
$this->rootFolder,
|
$this->rootFolder,
|
||||||
$this->eventDispatcher
|
$this->eventDispatcher,
|
||||||
|
$this->mailer,
|
||||||
|
$this->urlGenerator,
|
||||||
|
$this->defaults
|
||||||
);
|
);
|
||||||
|
|
||||||
$share = $this->createMock(IShare::class);
|
$share = $this->createMock(IShare::class);
|
||||||
|
@ -2147,7 +2168,10 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$factory,
|
$factory,
|
||||||
$this->userManager,
|
$this->userManager,
|
||||||
$this->rootFolder,
|
$this->rootFolder,
|
||||||
$this->eventDispatcher
|
$this->eventDispatcher,
|
||||||
|
$this->mailer,
|
||||||
|
$this->urlGenerator,
|
||||||
|
$this->defaults
|
||||||
);
|
);
|
||||||
|
|
||||||
$share = $this->createMock(IShare::class);
|
$share = $this->createMock(IShare::class);
|
||||||
|
@ -2795,7 +2819,10 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$factory,
|
$factory,
|
||||||
$this->userManager,
|
$this->userManager,
|
||||||
$this->rootFolder,
|
$this->rootFolder,
|
||||||
$this->eventDispatcher
|
$this->eventDispatcher,
|
||||||
|
$this->mailer,
|
||||||
|
$this->urlGenerator,
|
||||||
|
$this->defaults
|
||||||
);
|
);
|
||||||
$this->assertSame($expected,
|
$this->assertSame($expected,
|
||||||
$manager->shareProviderExists($shareType)
|
$manager->shareProviderExists($shareType)
|
||||||
|
@ -2823,7 +2850,10 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$factory,
|
$factory,
|
||||||
$this->userManager,
|
$this->userManager,
|
||||||
$this->rootFolder,
|
$this->rootFolder,
|
||||||
$this->eventDispatcher
|
$this->eventDispatcher,
|
||||||
|
$this->mailer,
|
||||||
|
$this->urlGenerator,
|
||||||
|
$this->defaults
|
||||||
);
|
);
|
||||||
|
|
||||||
$factory->setProvider($this->defaultProvider);
|
$factory->setProvider($this->defaultProvider);
|
||||||
|
@ -2882,7 +2912,10 @@ class ManagerTest extends \Test\TestCase {
|
||||||
$factory,
|
$factory,
|
||||||
$this->userManager,
|
$this->userManager,
|
||||||
$this->rootFolder,
|
$this->rootFolder,
|
||||||
$this->eventDispatcher
|
$this->eventDispatcher,
|
||||||
|
$this->mailer,
|
||||||
|
$this->urlGenerator,
|
||||||
|
$this->defaults
|
||||||
);
|
);
|
||||||
|
|
||||||
$factory->setProvider($this->defaultProvider);
|
$factory->setProvider($this->defaultProvider);
|
||||||
|
|
Loading…
Reference in New Issue