NewUserMailHelper.php 5.5 KB

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