@@ -34,129 +34,129 @@ |
||
34 | 34 | use OCP\Security\ISecureRandom; |
35 | 35 | |
36 | 36 | class NewUserMailHelper { |
37 | - /** @var Defaults */ |
|
38 | - private $themingDefaults; |
|
39 | - /** @var IURLGenerator */ |
|
40 | - private $urlGenerator; |
|
41 | - /** @var IL10N */ |
|
42 | - private $l10n; |
|
43 | - /** @var IMailer */ |
|
44 | - private $mailer; |
|
45 | - /** @var ISecureRandom */ |
|
46 | - private $secureRandom; |
|
47 | - /** @var ITimeFactory */ |
|
48 | - private $timeFactory; |
|
49 | - /** @var IConfig */ |
|
50 | - private $config; |
|
51 | - /** @var ICrypto */ |
|
52 | - private $crypto; |
|
53 | - /** @var string */ |
|
54 | - private $fromAddress; |
|
37 | + /** @var Defaults */ |
|
38 | + private $themingDefaults; |
|
39 | + /** @var IURLGenerator */ |
|
40 | + private $urlGenerator; |
|
41 | + /** @var IL10N */ |
|
42 | + private $l10n; |
|
43 | + /** @var IMailer */ |
|
44 | + private $mailer; |
|
45 | + /** @var ISecureRandom */ |
|
46 | + private $secureRandom; |
|
47 | + /** @var ITimeFactory */ |
|
48 | + private $timeFactory; |
|
49 | + /** @var IConfig */ |
|
50 | + private $config; |
|
51 | + /** @var ICrypto */ |
|
52 | + private $crypto; |
|
53 | + /** @var string */ |
|
54 | + private $fromAddress; |
|
55 | 55 | |
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 | - } |
|
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 | 86 | |
87 | - /** |
|
88 | - * Set the IL10N object |
|
89 | - * |
|
90 | - * @param IL10N $l10n |
|
91 | - */ |
|
92 | - public function setL10N(IL10N $l10n) { |
|
93 | - $this->l10n = $l10n; |
|
94 | - } |
|
87 | + /** |
|
88 | + * Set the IL10N object |
|
89 | + * |
|
90 | + * @param IL10N $l10n |
|
91 | + */ |
|
92 | + public function setL10N(IL10N $l10n) { |
|
93 | + $this->l10n = $l10n; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @param IUser $user |
|
98 | - * @param bool $generatePasswordResetToken |
|
99 | - * @return EMailTemplate |
|
100 | - */ |
|
101 | - public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { |
|
102 | - if ($generatePasswordResetToken) { |
|
103 | - $token = $this->secureRandom->generate( |
|
104 | - 21, |
|
105 | - ISecureRandom::CHAR_DIGITS . |
|
106 | - ISecureRandom::CHAR_LOWER . |
|
107 | - ISecureRandom::CHAR_UPPER |
|
108 | - ); |
|
109 | - $tokenValue = $this->timeFactory->getTime() . ':' . $token; |
|
110 | - $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
111 | - $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); |
|
112 | - $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
113 | - $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]); |
|
114 | - } else { |
|
115 | - $link = $this->urlGenerator->getAbsoluteURL('/'); |
|
116 | - } |
|
96 | + /** |
|
97 | + * @param IUser $user |
|
98 | + * @param bool $generatePasswordResetToken |
|
99 | + * @return EMailTemplate |
|
100 | + */ |
|
101 | + public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { |
|
102 | + if ($generatePasswordResetToken) { |
|
103 | + $token = $this->secureRandom->generate( |
|
104 | + 21, |
|
105 | + ISecureRandom::CHAR_DIGITS . |
|
106 | + ISecureRandom::CHAR_LOWER . |
|
107 | + ISecureRandom::CHAR_UPPER |
|
108 | + ); |
|
109 | + $tokenValue = $this->timeFactory->getTime() . ':' . $token; |
|
110 | + $mailAddress = (null !== $user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
111 | + $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress . $this->config->getSystemValue('secret')); |
|
112 | + $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
113 | + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $user->getUID(), 'token' => $token]); |
|
114 | + } else { |
|
115 | + $link = $this->urlGenerator->getAbsoluteURL('/'); |
|
116 | + } |
|
117 | 117 | |
118 | - $emailTemplate = $this->mailer->createEMailTemplate(); |
|
119 | - $emailTemplate->addHeader(); |
|
120 | - $displayName = $user->getDisplayName(); |
|
121 | - $userName = $user->getUID(); |
|
122 | - if ($displayName === $userName) { |
|
123 | - $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); |
|
124 | - } else { |
|
125 | - $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); |
|
126 | - } |
|
127 | - $emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); |
|
128 | - $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userName])); |
|
129 | - if ($generatePasswordResetToken) { |
|
130 | - $leftButtonText = $this->l10n->t('Set your password'); |
|
131 | - } else { |
|
132 | - $leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]); |
|
133 | - } |
|
134 | - $emailTemplate->addBodyButtonGroup( |
|
135 | - $leftButtonText, |
|
136 | - $link, |
|
137 | - $this->l10n->t('Install Client'), |
|
138 | - 'https://nextcloud.com/install/#install-clients' |
|
139 | - ); |
|
140 | - $emailTemplate->addFooter(); |
|
118 | + $emailTemplate = $this->mailer->createEMailTemplate(); |
|
119 | + $emailTemplate->addHeader(); |
|
120 | + $displayName = $user->getDisplayName(); |
|
121 | + $userName = $user->getUID(); |
|
122 | + if ($displayName === $userName) { |
|
123 | + $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); |
|
124 | + } else { |
|
125 | + $emailTemplate->addHeading($this->l10n->t('Welcome aboard %s', [$displayName])); |
|
126 | + } |
|
127 | + $emailTemplate->addBodyText($this->l10n->t('You have now an %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); |
|
128 | + $emailTemplate->addBodyText($this->l10n->t('Your username is: %s', [$userName])); |
|
129 | + if ($generatePasswordResetToken) { |
|
130 | + $leftButtonText = $this->l10n->t('Set your password'); |
|
131 | + } else { |
|
132 | + $leftButtonText = $this->l10n->t('Go to %s', [$this->themingDefaults->getName()]); |
|
133 | + } |
|
134 | + $emailTemplate->addBodyButtonGroup( |
|
135 | + $leftButtonText, |
|
136 | + $link, |
|
137 | + $this->l10n->t('Install Client'), |
|
138 | + 'https://nextcloud.com/install/#install-clients' |
|
139 | + ); |
|
140 | + $emailTemplate->addFooter(); |
|
141 | 141 | |
142 | - return $emailTemplate; |
|
143 | - } |
|
142 | + return $emailTemplate; |
|
143 | + } |
|
144 | 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->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); |
|
157 | - $message->setHtmlBody($emailTemplate->renderHtml()); |
|
158 | - $message->setPlainBody($emailTemplate->renderText()); |
|
159 | - $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); |
|
160 | - $this->mailer->send($message); |
|
161 | - } |
|
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->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); |
|
157 | + $message->setHtmlBody($emailTemplate->renderHtml()); |
|
158 | + $message->setPlainBody($emailTemplate->renderText()); |
|
159 | + $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); |
|
160 | + $this->mailer->send($message); |
|
161 | + } |
|
162 | 162 | } |