diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2017-04-07 14:51:05 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2017-04-07 12:03:47 -0500 |
commit | 281ad406e88d36c1492c7aefd8ef28762379f9e3 (patch) | |
tree | 91b81364f1a9244455657b86848025c628fd4089 /settings | |
parent | 1be75e8db8f583476f1cd03498afd608fce6408d (diff) | |
download | nextcloud-server-281ad406e88d36c1492c7aefd8ef28762379f9e3.tar.gz nextcloud-server-281ad406e88d36c1492c7aefd8ef28762379f9e3.zip |
Add support for theming
Add support for theming in generated emails and simplify API
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Diffstat (limited to 'settings')
-rw-r--r-- | settings/Application.php | 23 | ||||
-rw-r--r-- | settings/Controller/UsersController.php | 102 | ||||
-rw-r--r-- | settings/Mailer/NewUserMailHelper.php | 166 |
3 files changed, 196 insertions, 95 deletions
diff --git a/settings/Application.php b/settings/Application.php index 44747c2f681..bf149def94d 100644 --- a/settings/Application.php +++ b/settings/Application.php @@ -35,7 +35,10 @@ use OC\App\AppStore\Fetcher\CategoryFetcher; use OC\AppFramework\Utility\TimeFactory; use OC\Authentication\Token\IProvider; use OC\Server; +use OC\ServerContainer; +use OC\Settings\Mailer\NewUserMailHelper; use OC\Settings\Middleware\SubadminMiddleware; +use OCA\Theming\ThemingDefaults; use OCP\AppFramework\App; use OCP\IContainer; use OCP\Settings\IManager; @@ -75,9 +78,6 @@ class Application extends App { } return $isSubAdmin; }); - $container->registerService('fromMailAddress', function() { - return Util::getDefaultEmailAddress('no-reply'); - }); $container->registerService('userCertificateManager', function(IContainer $c) { return $c->query('ServerContainer')->getCertificateManager(); }, false); @@ -90,6 +90,23 @@ class Application extends App { $container->registerService(IManager::class, function (IContainer $c) { return $c->query('ServerContainer')->getSettingsManager(); }); + + $container->registerService(NewUserMailHelper::class, function (IContainer $c) { + /** @var Server $server */ + $server = $c->query('ServerContainer'); + + return new NewUserMailHelper( + $server->getThemingDefaults(), + $server->getURLGenerator(), + $server->getL10N('settings'), + $server->getMailer(), + $server->getSecureRandom(), + new TimeFactory(), + $server->getConfig(), + $server->getCrypto(), + Util::getDefaultEmailAddress('no-reply') + ); + }); $container->registerService(AppFetcher::class, function (IContainer $c) { /** @var Server $server */ $server = $c->query('ServerContainer'); diff --git a/settings/Controller/UsersController.php b/settings/Controller/UsersController.php index c7b2c2537da..b42d4faa569 100644 --- a/settings/Controller/UsersController.php +++ b/settings/Controller/UsersController.php @@ -33,12 +33,10 @@ namespace OC\Settings\Controller; use OC\Accounts\AccountManager; use OC\AppFramework\Http; use OC\ForbiddenException; -use OC\Mail\EMailTemplate; -use OC\User\User; +use OC\Settings\Mailer\NewUserMailHelper; use OCP\App\IAppManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; @@ -50,9 +48,7 @@ use OCP\IUserManager; use OCP\IUserSession; use OCP\Mail\IMailer; use OCP\IAvatarManager; -use OCP\Security\ICrypto; use OCP\Security\ISecureRandom; -use OCP\AppFramework\Utility\ITimeFactory; /** * @package OC\Settings\Controller @@ -72,14 +68,8 @@ class UsersController extends Controller { private $config; /** @var ILogger */ private $log; - /** @var \OC_Defaults */ - private $defaults; /** @var IMailer */ private $mailer; - /** @var string */ - private $fromMailAddress; - /** @var IURLGenerator */ - private $urlGenerator; /** @var bool contains the state of the encryption app */ private $isEncryptionAppEnabled; /** @var bool contains the state of the admin recovery setting */ @@ -90,11 +80,8 @@ class UsersController extends Controller { private $accountManager; /** @var ISecureRandom */ private $secureRandom; - /** @var ITimeFactory */ - private $timeFactory; - /** @var ICrypto */ - private $crypto; - + /** @var NewUserMailHelper */ + private $newUserMailHelper; /** * @param string $appName @@ -106,16 +93,13 @@ class UsersController extends Controller { * @param bool $isAdmin * @param IL10N $l10n * @param ILogger $log - * @param \OC_Defaults $defaults * @param IMailer $mailer - * @param string $fromMailAddress * @param IURLGenerator $urlGenerator * @param IAppManager $appManager * @param IAvatarManager $avatarManager * @param AccountManager $accountManager * @param ISecureRandom $secureRandom - * @param ITimeFactory $timeFactory - * @param ICrypto $crypto + * @param NewUserMailHelper $newUserMailHelper */ public function __construct($appName, IRequest $request, @@ -126,16 +110,13 @@ class UsersController extends Controller { $isAdmin, IL10N $l10n, ILogger $log, - \OC_Defaults $defaults, IMailer $mailer, - $fromMailAddress, IURLGenerator $urlGenerator, IAppManager $appManager, IAvatarManager $avatarManager, AccountManager $accountManager, ISecureRandom $secureRandom, - ITimeFactory $timeFactory, - ICrypto $crypto) { + NewUserMailHelper $newUserMailHelper) { parent::__construct($appName, $request); $this->userManager = $userManager; $this->groupManager = $groupManager; @@ -144,15 +125,11 @@ class UsersController extends Controller { $this->isAdmin = $isAdmin; $this->l10n = $l10n; $this->log = $log; - $this->defaults = $defaults; $this->mailer = $mailer; - $this->fromMailAddress = $fromMailAddress; - $this->urlGenerator = $urlGenerator; $this->avatarManager = $avatarManager; $this->accountManager = $accountManager; $this->secureRandom = $secureRandom; - $this->timeFactory = $timeFactory; - $this->crypto = $crypto; + $this->newUserMailHelper = $newUserMailHelper; // check for encryption state - TODO see formatUserForIndex $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption'); @@ -380,7 +357,7 @@ class UsersController extends Controller { ); } - $generatedPassword = false; + $generatePasswordResetToken = false; if ($password === '') { if ($email === '') { return new DataResponse( @@ -392,7 +369,7 @@ class UsersController extends Controller { } $password = $this->secureRandom->generate(32); - $generatedPassword = true; + $generatePasswordResetToken = true; } try { @@ -426,68 +403,9 @@ class UsersController extends Controller { */ if($email !== '') { $user->setEMailAddress($email); - - if ($generatedPassword) { - $token = $this->secureRandom->generate( - 21, - ISecureRandom::CHAR_DIGITS . - ISecureRandom::CHAR_LOWER . - ISecureRandom::CHAR_UPPER - ); - $tokenValue = $this->timeFactory->getTime() . ':' . $token; - $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; - $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); - $this->config->setUserValue($username, 'core', 'lostpassword', $encryptedValue); - - $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $username, 'token' => $token]); - } else { - $link = $this->urlGenerator->getAbsoluteURL('/'); - } - - $emailTemplate = new EMailTemplate($this->defaults); - - $emailTemplate->addHeader($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('', 'logo-mail-header.png'))); - - $displayName = $user->getDisplayName(); - if ($displayName === $username) { - $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); - } else { - $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); - } - $emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->defaults->getName()])); - $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$username])); - - if ($generatedPassword) { - $leftButtonText = $this->l10n->t('Set your password'); - } else { - $leftButtonText = $this->l10n->t('Go to %s', [$this->defaults->getName()]); - } - - $emailTemplate->addBodyButtonGroup( - $leftButtonText, - $link, - $this->l10n->t('Install Client'), - 'https://nextcloud.com/install/#install-clients' - ); - - $emailTemplate->addFooter( - $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('', 'logo-mail-footer.png')), - $this->defaults->getName() . ' - ' . $this->defaults->getSlogan() . '<br>' . $this->l10n->t('This is an automatically generated email, please do not reply.') - ); - - $mailContent = $emailTemplate->renderHTML(); - $plainTextMailContent = $emailTemplate->renderText(); - - $subject = $this->l10n->t('Your %s account was created', [$this->defaults->getName()]); - try { - $message = $this->mailer->createMessage(); - $message->setTo([$email => $username]); - $message->setSubject($subject); - $message->setHtmlBody($mailContent); - $message->setPlainBody($plainTextMailContent); - $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]); - $this->mailer->send($message); + $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken); + $this->newUserMailHelper->sendMail($user, $emailTemplate); } catch(\Exception $e) { $this->log->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings')); } diff --git a/settings/Mailer/NewUserMailHelper.php b/settings/Mailer/NewUserMailHelper.php new file mode 100644 index 00000000000..15b912f803a --- /dev/null +++ b/settings/Mailer/NewUserMailHelper.php @@ -0,0 +1,166 @@ +<?php +/** + * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Settings\Mailer; + +use OC\Mail\EMailTemplate; +use OC\Mail\IEMailTemplate; +use OCA\Theming\ThemingDefaults; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\IUser; +use OCP\Mail\IMailer; +use OCP\Security\ICrypto; +use OCP\Security\ISecureRandom; + +class NewUserMailHelper { + /** @var ThemingDefaults */ + private $themingDefaults; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var IL10N */ + private $l10n; + /** @var IMailer */ + private $mailer; + /** @var ISecureRandom */ + private $secureRandom; + /** @var ITimeFactory */ + private $timeFactory; + /** @var IConfig */ + private $config; + /** @var ICrypto */ + private $crypto; + /** @var string */ + private $fromAddress; + + /** + * @param ThemingDefaults $themingDefaults + * @param IURLGenerator $urlGenerator + * @param IL10N $l10n + * @param IMailer $mailer + * @param ISecureRandom $secureRandom + * @param ITimeFactory $timeFactory + * @param IConfig $config + * @param ICrypto $crypto + * @param string $fromAddress + */ + public function __construct(ThemingDefaults $themingDefaults, + IURLGenerator $urlGenerator, + IL10N $l10n, + IMailer $mailer, + ISecureRandom $secureRandom, + ITimeFactory $timeFactory, + IConfig $config, + ICrypto $crypto, + $fromAddress) { + $this->themingDefaults = $themingDefaults; + $this->urlGenerator = $urlGenerator; + $this->l10n = $l10n; + $this->mailer = $mailer; + $this->secureRandom = $secureRandom; + $this->timeFactory = $timeFactory; + $this->config = $config; + $this->crypto = $crypto; + $this->fromAddress = $fromAddress; + } + + /** + * Set the IL10N object + * + * @param IL10N $l10n + */ + public function setL10N(IL10N $l10n) { + $this->l10n = $l10n; + } + + /** + * @param IUser $user + * @param bool $generatePasswordResetToken + * @return EMailTemplate + */ + public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { + if ($generatePasswordResetToken) { + $token = $this->secureRandom->generate( + 21, + ISecureRandom::CHAR_DIGITS . + ISecureRandom::CHAR_LOWER . + ISecureRandom::CHAR_UPPER + ); + $tokenValue = $this->timeFactory->getTime() . ':' . $token; + $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : ''; + $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); + $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]); + } else { + $link = $this->urlGenerator->getAbsoluteURL('/'); + } + + $emailTemplate = new EMailTemplate( + $this->themingDefaults, + $this->urlGenerator, + $this->l10n + ); + $emailTemplate->addHeader(); + $displayName = $user->getDisplayName(); + $userName = $user->getUID(); + if ($displayName === $userName) { + $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); + } else { + $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); + } + $emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); + $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userName])); + if ($generatePasswordResetToken) { + $leftButtonText = $this->l10n->t('Set your password'); + } else { + $leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]); + } + $emailTemplate->addBodyButtonGroup( + $leftButtonText, + $link, + $this->l10n->t('Install Client'), + 'https://nextcloud.com/install/#install-clients' + ); + $emailTemplate->addFooter(); + + return $emailTemplate; + } + + /** + * Sends a welcome mail to $user + * + * @param IUser $user + * @param IEmailTemplate $emailTemplate + * @throws \Exception If mail could not be sent + */ + public function sendMail(IUser $user, + IEMailTemplate $emailTemplate) { + $message = $this->mailer->createMessage(); + $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); + $message->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); + $message->setHtmlBody($emailTemplate->renderHTML()); + $message->setPlainBody($emailTemplate->renderText()); + $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); + $this->mailer->send($message); + } +} |