NewUserMailHelper.php 5.9 KB

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