NewUserMailHelper.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_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. if ($user->getBackendClassName() !== 'LDAP') {
  131. $emailTemplate->addBodyText($l10n->t('Your username is: %s', [$userId]));
  132. }
  133. if ($generatePasswordResetToken) {
  134. $leftButtonText = $l10n->t('Set your password');
  135. } else {
  136. $leftButtonText = $l10n->t('Go to %s', [$this->themingDefaults->getName()]);
  137. }
  138. $emailTemplate->addBodyButtonGroup(
  139. $leftButtonText,
  140. $link,
  141. $l10n->t('Install Client'),
  142. $this->config->getSystemValue('customclient_desktop', '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. }