1
0

NewUserMailHelper.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author zulan <git@zulan.net>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OCA\Settings\Mailer;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Defaults;
  33. use OCP\IConfig;
  34. use OCP\IURLGenerator;
  35. use OCP\IUser;
  36. use OCP\L10N\IFactory;
  37. use OCP\Mail\IEMailTemplate;
  38. use OCP\Mail\IMailer;
  39. use OCP\Security\ICrypto;
  40. use OCP\Security\ISecureRandom;
  41. class NewUserMailHelper {
  42. /** @var Defaults */
  43. private $themingDefaults;
  44. /** @var IURLGenerator */
  45. private $urlGenerator;
  46. /** @var IFactory */
  47. private $l10nFactory;
  48. /** @var IMailer */
  49. private $mailer;
  50. /** @var ISecureRandom */
  51. private $secureRandom;
  52. /** @var ITimeFactory */
  53. private $timeFactory;
  54. /** @var IConfig */
  55. private $config;
  56. /** @var ICrypto */
  57. private $crypto;
  58. /** @var string */
  59. private $fromAddress;
  60. /**
  61. * @param Defaults $themingDefaults
  62. * @param IURLGenerator $urlGenerator
  63. * @param IFactory $l10nFactory
  64. * @param IMailer $mailer
  65. * @param ISecureRandom $secureRandom
  66. * @param ITimeFactory $timeFactory
  67. * @param IConfig $config
  68. * @param ICrypto $crypto
  69. * @param string $fromAddress
  70. */
  71. public function __construct(Defaults $themingDefaults,
  72. IURLGenerator $urlGenerator,
  73. IFactory $l10nFactory,
  74. IMailer $mailer,
  75. ISecureRandom $secureRandom,
  76. ITimeFactory $timeFactory,
  77. IConfig $config,
  78. ICrypto $crypto,
  79. $fromAddress) {
  80. $this->themingDefaults = $themingDefaults;
  81. $this->urlGenerator = $urlGenerator;
  82. $this->l10nFactory = $l10nFactory;
  83. $this->mailer = $mailer;
  84. $this->secureRandom = $secureRandom;
  85. $this->timeFactory = $timeFactory;
  86. $this->config = $config;
  87. $this->crypto = $crypto;
  88. $this->fromAddress = $fromAddress;
  89. }
  90. /**
  91. * @param IUser $user
  92. * @param bool $generatePasswordResetToken
  93. * @return IEMailTemplate
  94. */
  95. public function generateTemplate(IUser $user, $generatePasswordResetToken = false) {
  96. $userId = $user->getUID();
  97. $lang = $this->l10nFactory->getUserLanguage($user);
  98. $l10n = $this->l10nFactory->get('settings', $lang);
  99. if ($generatePasswordResetToken) {
  100. $token = $this->secureRandom->generate(
  101. 21,
  102. ISecureRandom::CHAR_ALPHANUMERIC
  103. );
  104. $tokenValue = $this->timeFactory->getTime() . ':' . $token;
  105. $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : '';
  106. $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret'));
  107. $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue);
  108. $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]);
  109. } else {
  110. $link = $this->urlGenerator->getAbsoluteURL('/');
  111. }
  112. $displayName = $user->getDisplayName();
  113. $emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [
  114. 'link' => $link,
  115. 'displayname' => $displayName,
  116. 'userid' => $userId,
  117. 'instancename' => $this->themingDefaults->getName(),
  118. 'resetTokenGenerated' => $generatePasswordResetToken,
  119. ]);
  120. $emailTemplate->setSubject($l10n->t('Your %s account was created', [$this->themingDefaults->getName()]));
  121. $emailTemplate->addHeader();
  122. if ($displayName === $userId) {
  123. $emailTemplate->addHeading($l10n->t('Welcome aboard'));
  124. } else {
  125. $emailTemplate->addHeading($l10n->t('Welcome aboard %s', [$displayName]));
  126. }
  127. $emailTemplate->addBodyText($l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()]));
  128. if ($user->getBackendClassName() !== 'LDAP') {
  129. $emailTemplate->addBodyText($l10n->t('Your username is: %s', [$userId]));
  130. }
  131. if ($generatePasswordResetToken) {
  132. $leftButtonText = $l10n->t('Set your password');
  133. } else {
  134. $leftButtonText = $l10n->t('Go to %s', [$this->themingDefaults->getName()]);
  135. }
  136. $clientDownload = $this->config->getSystemValue('customclient_desktop', 'https://nextcloud.com/install/#install-clients');
  137. if ($clientDownload === '') {
  138. $emailTemplate->addBodyButton(
  139. $leftButtonText,
  140. $link
  141. );
  142. } else {
  143. $emailTemplate->addBodyButtonGroup(
  144. $leftButtonText,
  145. $link,
  146. $l10n->t('Install Client'),
  147. $clientDownload
  148. );
  149. }
  150. $emailTemplate->addFooter('', $lang);
  151. return $emailTemplate;
  152. }
  153. /**
  154. * Sends a welcome mail to $user
  155. *
  156. * @param IUser $user
  157. * @param IEmailTemplate $emailTemplate
  158. * @throws \Exception If mail could not be sent
  159. */
  160. public function sendMail(IUser $user,
  161. IEMailTemplate $emailTemplate): void {
  162. // Be sure to never try to send to an empty e-mail
  163. $email = $user->getEMailAddress();
  164. if ($email === null) {
  165. return;
  166. }
  167. $message = $this->mailer->createMessage();
  168. $message->setTo([$email => $user->getDisplayName()]);
  169. $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]);
  170. $message->useTemplate($emailTemplate);
  171. $this->mailer->send($message);
  172. }
  173. }