NewUserMailHelper.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Mail\IEMailTemplate;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\Defaults;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\Mail\IMailer;
  35. use OCP\Security\ICrypto;
  36. use OCP\Security\ISecureRandom;
  37. class NewUserMailHelper {
  38. /** @var Defaults */
  39. private $themingDefaults;
  40. /** @var IURLGenerator */
  41. private $urlGenerator;
  42. /** @var IL10N */
  43. private $l10n;
  44. /** @var IMailer */
  45. private $mailer;
  46. /** @var ISecureRandom */
  47. private $secureRandom;
  48. /** @var ITimeFactory */
  49. private $timeFactory;
  50. /** @var IConfig */
  51. private $config;
  52. /** @var ICrypto */
  53. private $crypto;
  54. /** @var string */
  55. private $fromAddress;
  56. /**
  57. * @param Defaults $themingDefaults
  58. * @param IURLGenerator $urlGenerator
  59. * @param IL10N $l10n
  60. * @param IMailer $mailer
  61. * @param ISecureRandom $secureRandom
  62. * @param ITimeFactory $timeFactory
  63. * @param IConfig $config
  64. * @param ICrypto $crypto
  65. * @param string $fromAddress
  66. */
  67. public function __construct(Defaults $themingDefaults,
  68. IURLGenerator $urlGenerator,
  69. IL10N $l10n,
  70. IMailer $mailer,
  71. ISecureRandom $secureRandom,
  72. ITimeFactory $timeFactory,
  73. IConfig $config,
  74. ICrypto $crypto,
  75. $fromAddress) {
  76. $this->themingDefaults = $themingDefaults;
  77. $this->urlGenerator = $urlGenerator;
  78. $this->l10n = $l10n;
  79. $this->mailer = $mailer;
  80. $this->secureRandom = $secureRandom;
  81. $this->timeFactory = $timeFactory;
  82. $this->config = $config;
  83. $this->crypto = $crypto;
  84. $this->fromAddress = $fromAddress;
  85. }
  86. /**
  87. * Set the IL10N object
  88. *
  89. * @param IL10N $l10n
  90. */
  91. public function setL10N(IL10N $l10n) {
  92. $this->l10n = $l10n;
  93. }
  94. /**
  95. * @param IUser $user
  96. * @param bool $generatePasswordResetToken
  97. * @return IEMailTemplate
  98. */
  99. public function generateTemplate(IUser $user, $generatePasswordResetToken = false) {
  100. if ($generatePasswordResetToken) {
  101. $token = $this->secureRandom->generate(
  102. 21,
  103. ISecureRandom::CHAR_DIGITS .
  104. ISecureRandom::CHAR_LOWER .
  105. ISecureRandom::CHAR_UPPER
  106. );
  107. $tokenValue = $this->timeFactory->getTime() . ':' . $token;
  108. $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : '';
  109. $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret'));
  110. $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue);
  111. $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]);
  112. } else {
  113. $link = $this->urlGenerator->getAbsoluteURL('/');
  114. }
  115. $displayName = $user->getDisplayName();
  116. $userId = $user->getUID();
  117. $emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [
  118. 'link' => $link,
  119. 'displayname' => $displayName,
  120. 'userid' => $userId,
  121. 'instancename' => $this->themingDefaults->getName(),
  122. 'resetTokenGenerated' => $generatePasswordResetToken,
  123. ]);
  124. $emailTemplate->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()]));
  125. $emailTemplate->addHeader();
  126. if ($displayName === $userId) {
  127. $emailTemplate->addHeading($this->l10n->t('Welcome aboard'));
  128. } else {
  129. $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName]));
  130. }
  131. $emailTemplate->addBodyText($this->l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()]));
  132. $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userId]));
  133. if ($generatePasswordResetToken) {
  134. $leftButtonText = $this->l10n->t('Set your password');
  135. } else {
  136. $leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]);
  137. }
  138. $emailTemplate->addBodyButtonGroup(
  139. $leftButtonText,
  140. $link,
  141. $this->l10n->t('Install Client'),
  142. 'https://nextcloud.com/install/#install-clients'
  143. );
  144. $emailTemplate->addFooter();
  145. return $emailTemplate;
  146. }
  147. /**
  148. * Sends a welcome mail to $user
  149. *
  150. * @param IUser $user
  151. * @param IEmailTemplate $emailTemplate
  152. * @throws \Exception If mail could not be sent
  153. */
  154. public function sendMail(IUser $user,
  155. IEMailTemplate $emailTemplate) {
  156. $message = $this->mailer->createMessage();
  157. $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
  158. $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]);
  159. $message->useTemplate($emailTemplate);
  160. $this->mailer->send($message);
  161. }
  162. }