2017-04-07 15:51:05 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Leon Klingele <leon@struktur.de>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
*
|
2017-04-07 15:51:05 +03:00
|
|
|
* @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;
|
|
|
|
|
2018-09-24 23:54:57 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2017-04-11 18:35:59 +03:00
|
|
|
use OCP\Mail\IEMailTemplate;
|
2017-04-07 15:51:05 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
2017-04-07 15:51:05 +03:00
|
|
|
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 {
|
2017-04-07 23:42:43 +03:00
|
|
|
/** @var Defaults */
|
2017-04-07 15:51:05 +03:00
|
|
|
private $themingDefaults;
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
2018-09-24 23:54:57 +03:00
|
|
|
/** @var IFactory */
|
|
|
|
private $l10nFactory;
|
2017-04-07 15:51:05 +03:00
|
|
|
/** @var IMailer */
|
|
|
|
private $mailer;
|
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $secureRandom;
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var ICrypto */
|
|
|
|
private $crypto;
|
|
|
|
/** @var string */
|
|
|
|
private $fromAddress;
|
|
|
|
|
|
|
|
/**
|
2017-04-07 23:42:43 +03:00
|
|
|
* @param Defaults $themingDefaults
|
2017-04-07 15:51:05 +03:00
|
|
|
* @param IURLGenerator $urlGenerator
|
2018-09-24 23:54:57 +03:00
|
|
|
* @param IFactory $l10nFactory
|
2017-04-07 15:51:05 +03:00
|
|
|
* @param IMailer $mailer
|
|
|
|
* @param ISecureRandom $secureRandom
|
|
|
|
* @param ITimeFactory $timeFactory
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param ICrypto $crypto
|
|
|
|
* @param string $fromAddress
|
|
|
|
*/
|
2017-04-07 23:42:43 +03:00
|
|
|
public function __construct(Defaults $themingDefaults,
|
2017-04-07 15:51:05 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
2018-09-24 23:54:57 +03:00
|
|
|
IFactory $l10nFactory,
|
2017-04-07 15:51:05 +03:00
|
|
|
IMailer $mailer,
|
|
|
|
ISecureRandom $secureRandom,
|
|
|
|
ITimeFactory $timeFactory,
|
|
|
|
IConfig $config,
|
|
|
|
ICrypto $crypto,
|
|
|
|
$fromAddress) {
|
|
|
|
$this->themingDefaults = $themingDefaults;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2018-09-24 23:54:57 +03:00
|
|
|
$this->l10nFactory = $l10nFactory;
|
2017-04-07 15:51:05 +03:00
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->secureRandom = $secureRandom;
|
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->crypto = $crypto;
|
|
|
|
$this->fromAddress = $fromAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param bool $generatePasswordResetToken
|
2017-09-04 15:46:05 +03:00
|
|
|
* @return IEMailTemplate
|
2017-04-07 15:51:05 +03:00
|
|
|
*/
|
|
|
|
public function generateTemplate(IUser $user, $generatePasswordResetToken = false) {
|
2018-09-24 23:54:57 +03:00
|
|
|
$userId = $user->getUID();
|
|
|
|
$lang = $this->config->getUserValue($userId, 'core', 'lang', 'en');
|
|
|
|
if (!$this->l10nFactory->languageExists('settings', $lang)) {
|
|
|
|
$lang = 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
$l10n = $this->l10nFactory->get('settings', $lang);
|
|
|
|
|
2017-04-07 15:51:05 +03:00
|
|
|
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('/');
|
|
|
|
}
|
2017-09-04 15:46:05 +03:00
|
|
|
$displayName = $user->getDisplayName();
|
2017-04-07 15:51:05 +03:00
|
|
|
|
2017-09-04 16:07:19 +03:00
|
|
|
$emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [
|
2017-09-04 15:46:05 +03:00
|
|
|
'link' => $link,
|
|
|
|
'displayname' => $displayName,
|
|
|
|
'userid' => $userId,
|
|
|
|
'instancename' => $this->themingDefaults->getName(),
|
|
|
|
'resetTokenGenerated' => $generatePasswordResetToken,
|
|
|
|
]);
|
|
|
|
|
2018-09-24 23:54:57 +03:00
|
|
|
$emailTemplate->setSubject($l10n->t('Your %s account was created', [$this->themingDefaults->getName()]));
|
2017-04-07 15:51:05 +03:00
|
|
|
$emailTemplate->addHeader();
|
2017-09-01 16:04:17 +03:00
|
|
|
if ($displayName === $userId) {
|
2018-09-24 23:54:57 +03:00
|
|
|
$emailTemplate->addHeading($l10n->t('Welcome aboard'));
|
2017-04-07 15:51:05 +03:00
|
|
|
} else {
|
2018-09-24 23:54:57 +03:00
|
|
|
$emailTemplate->addHeading($l10n->t('Welcome aboard %s', [$displayName]));
|
2017-04-07 15:51:05 +03:00
|
|
|
}
|
2018-09-24 23:54:57 +03:00
|
|
|
$emailTemplate->addBodyText($l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()]));
|
2019-07-26 11:48:34 +03:00
|
|
|
if($user->getBackendClassName() !== 'LDAP') {
|
|
|
|
$emailTemplate->addBodyText($l10n->t('Your username is: %s', [$userId]));
|
|
|
|
}
|
2017-04-07 15:51:05 +03:00
|
|
|
if ($generatePasswordResetToken) {
|
2018-09-24 23:54:57 +03:00
|
|
|
$leftButtonText = $l10n->t('Set your password');
|
2017-04-07 15:51:05 +03:00
|
|
|
} else {
|
2018-09-24 23:54:57 +03:00
|
|
|
$leftButtonText = $l10n->t('Go to %s', [$this->themingDefaults->getName()]);
|
2017-04-07 15:51:05 +03:00
|
|
|
}
|
|
|
|
$emailTemplate->addBodyButtonGroup(
|
|
|
|
$leftButtonText,
|
|
|
|
$link,
|
2018-09-24 23:54:57 +03:00
|
|
|
$l10n->t('Install Client'),
|
2019-08-27 09:09:25 +03:00
|
|
|
$this->config->getSystemValue('customclient_desktop', 'https://nextcloud.com/install/#install-clients')
|
2017-04-07 15:51:05 +03:00
|
|
|
);
|
|
|
|
$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->setFrom([$this->fromAddress => $this->themingDefaults->getName()]);
|
2017-09-15 12:01:21 +03:00
|
|
|
$message->useTemplate($emailTemplate);
|
2017-04-07 15:51:05 +03:00
|
|
|
$this->mailer->send($message);
|
|
|
|
}
|
|
|
|
}
|