1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling <[email protected]> |
6
|
|
|
* @author Leon Klingele <[email protected]> |
7
|
|
|
* @author Lukas Reschke <[email protected]> |
8
|
|
|
* @author Morris Jobke <[email protected]> |
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
|
|
|
|
27
|
|
|
namespace OC\Settings\Mailer; |
28
|
|
|
|
29
|
|
|
use OCP\L10N\IFactory; |
30
|
|
|
use OCP\Mail\IEMailTemplate; |
31
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
32
|
|
|
use OCP\Defaults; |
33
|
|
|
use OCP\IConfig; |
34
|
|
|
use OCP\IL10N; |
35
|
|
|
use OCP\IURLGenerator; |
36
|
|
|
use OCP\IUser; |
37
|
|
|
use OCP\Mail\IMailer; |
38
|
|
|
use OCP\Security\ICrypto; |
39
|
|
|
use OCP\Security\ISecureRandom; |
40
|
|
|
|
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
|
|
|
/** |
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
|
|
View Code Duplication |
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
|
|
|
/** |
93
|
|
|
* @param IUser $user |
94
|
|
|
* @param bool $generatePasswordResetToken |
95
|
|
|
* @return IEMailTemplate |
96
|
|
|
*/ |
97
|
|
|
public function generateTemplate(IUser $user, $generatePasswordResetToken = false) { |
98
|
|
|
$userId = $user->getUID(); |
99
|
|
|
$lang = $this->config->getUserValue($userId, 'core', 'lang', 'en'); |
100
|
|
|
if (!$this->l10nFactory->languageExists('settings', $lang)) { |
101
|
|
|
$lang = 'en'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$l10n = $this->l10nFactory->get('settings', $lang); |
105
|
|
|
|
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
|
|
|
|
123
|
|
|
$emailTemplate = $this->mailer->createEMailTemplate('settings.Welcome', [ |
124
|
|
|
'link' => $link, |
125
|
|
|
'displayname' => $displayName, |
126
|
|
|
'userid' => $userId, |
127
|
|
|
'instancename' => $this->themingDefaults->getName(), |
128
|
|
|
'resetTokenGenerated' => $generatePasswordResetToken, |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$emailTemplate->setSubject($l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); |
132
|
|
|
$emailTemplate->addHeader(); |
133
|
|
|
if ($displayName === $userId) { |
134
|
|
|
$emailTemplate->addHeading($l10n->t('Welcome aboard')); |
135
|
|
|
} else { |
136
|
|
|
$emailTemplate->addHeading($l10n->t('Welcome aboard %s', [$displayName])); |
137
|
|
|
} |
138
|
|
|
$emailTemplate->addBodyText($l10n->t('Welcome to your %s account, you can add, protect, and share your data.', [$this->themingDefaults->getName()])); |
139
|
|
|
$emailTemplate->addBodyText($l10n->t('Your username is: %s', [$userId])); |
140
|
|
|
if ($generatePasswordResetToken) { |
141
|
|
|
$leftButtonText = $l10n->t('Set your password'); |
142
|
|
|
} else { |
143
|
|
|
$leftButtonText = $l10n->t('Go to %s', [$this->themingDefaults->getName()]); |
144
|
|
|
} |
145
|
|
|
$emailTemplate->addBodyButtonGroup( |
146
|
|
|
$leftButtonText, |
147
|
|
|
$link, |
148
|
|
|
$l10n->t('Install Client'), |
149
|
|
|
'https://nextcloud.com/install/#install-clients' |
150
|
|
|
); |
151
|
|
|
$emailTemplate->addFooter(); |
152
|
|
|
|
153
|
|
|
return $emailTemplate; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Sends a welcome mail to $user |
158
|
|
|
* |
159
|
|
|
* @param IUser $user |
160
|
|
|
* @param IEmailTemplate $emailTemplate |
161
|
|
|
* @throws \Exception If mail could not be sent |
162
|
|
|
*/ |
163
|
|
|
public function sendMail(IUser $user, |
164
|
|
|
IEMailTemplate $emailTemplate) { |
165
|
|
|
$message = $this->mailer->createMessage(); |
166
|
|
|
$message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
167
|
|
|
$message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); |
168
|
|
|
$message->useTemplate($emailTemplate); |
169
|
|
|
$this->mailer->send($message); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|