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