1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Arthur Schiwon <[email protected]> |
6
|
|
|
* @author Christoph Wurst <[email protected]> |
7
|
|
|
* @author Joas Schilling <[email protected]> |
8
|
|
|
* @author Morris Jobke <[email protected]> |
9
|
|
|
* @author Thomas Citharel <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @license GNU AGPL version 3 or any later version |
12
|
|
|
* |
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU Affero General Public License as |
15
|
|
|
* published by the Free Software Foundation, either version 3 of the |
16
|
|
|
* License, or (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* This program is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21
|
|
|
* GNU Affero General Public License for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Affero General Public License |
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace OCA\Settings; |
29
|
|
|
|
30
|
|
|
use OCA\Settings\Activity\Provider; |
31
|
|
|
use OCP\Activity\IManager as IActivityManager; |
32
|
|
|
use OCP\IConfig; |
33
|
|
|
use OCP\IGroupManager; |
34
|
|
|
use OCP\IURLGenerator; |
35
|
|
|
use OCP\IUser; |
36
|
|
|
use OCP\IUserManager; |
37
|
|
|
use OCP\IUserSession; |
38
|
|
|
use OCP\L10N\IFactory; |
39
|
|
|
use OCP\Mail\IMailer; |
40
|
|
|
|
41
|
|
|
class Hooks { |
42
|
|
|
|
43
|
|
|
/** @var IActivityManager */ |
44
|
|
|
protected $activityManager; |
45
|
|
|
/** @var IGroupManager|\OC\Group\Manager */ |
46
|
|
|
protected $groupManager; |
47
|
|
|
/** @var IUserManager */ |
48
|
|
|
protected $userManager; |
49
|
|
|
/** @var IUserSession */ |
50
|
|
|
protected $userSession; |
51
|
|
|
/** @var IURLGenerator */ |
52
|
|
|
protected $urlGenerator; |
53
|
|
|
/** @var IMailer */ |
54
|
|
|
protected $mailer; |
55
|
|
|
/** @var IConfig */ |
56
|
|
|
protected $config; |
57
|
|
|
/** @var IFactory */ |
58
|
|
|
protected $languageFactory; |
59
|
|
|
|
60
|
|
|
public function __construct(IActivityManager $activityManager, |
61
|
|
|
IGroupManager $groupManager, |
62
|
|
|
IUserManager $userManager, |
63
|
|
|
IUserSession $userSession, |
64
|
|
|
IURLGenerator $urlGenerator, |
65
|
|
|
IMailer $mailer, |
66
|
|
|
IConfig $config, |
67
|
|
|
IFactory $languageFactory) { |
68
|
|
|
$this->activityManager = $activityManager; |
69
|
|
|
$this->groupManager = $groupManager; |
70
|
|
|
$this->userManager = $userManager; |
71
|
|
|
$this->userSession = $userSession; |
72
|
|
|
$this->urlGenerator = $urlGenerator; |
73
|
|
|
$this->mailer = $mailer; |
74
|
|
|
$this->config = $config; |
75
|
|
|
$this->languageFactory = $languageFactory; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $uid |
80
|
|
|
* @throws \InvalidArgumentException |
81
|
|
|
* @throws \BadMethodCallException |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
|
public function onChangePassword($uid) { |
85
|
|
|
$user = $this->userManager->get($uid); |
86
|
|
|
|
87
|
|
|
if (!$user instanceof IUser || $user->getLastLogin() === 0) { |
88
|
|
|
// User didn't login, so don't create activities and emails. |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$event = $this->activityManager->generateEvent(); |
93
|
|
|
$event->setApp('settings') |
94
|
|
|
->setType('personal_settings') |
95
|
|
|
->setAffectedUser($user->getUID()); |
96
|
|
|
|
97
|
|
|
$instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
98
|
|
|
$language = $this->languageFactory->getUserLanguage($user); |
99
|
|
|
$l = $this->languageFactory->get('settings', $language); |
100
|
|
|
|
101
|
|
|
$actor = $this->userSession->getUser(); |
102
|
|
|
if ($actor instanceof IUser) { |
103
|
|
|
if ($actor->getUID() !== $user->getUID()) { |
104
|
|
|
// Admin changed the password through the user panel |
105
|
|
|
$text = $l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
106
|
|
|
$event->setAuthor($actor->getUID()) |
107
|
|
|
->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); |
108
|
|
|
} else { |
109
|
|
|
// User changed their password themselves through settings |
110
|
|
|
$text = $l->t('Your password on %s was changed.', [$instanceUrl]); |
111
|
|
|
$event->setAuthor($actor->getUID()) |
112
|
|
|
->setSubject(Provider::PASSWORD_CHANGED_SELF); |
113
|
|
|
} |
114
|
|
|
} else { |
115
|
|
|
if (\OC::$CLI) { |
116
|
|
|
// Admin used occ to reset the password |
117
|
|
|
$text = $l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); |
118
|
|
|
$event->setSubject(Provider::PASSWORD_RESET); |
119
|
|
|
} else { |
120
|
|
|
// User reset their password from Lost page |
121
|
|
|
$text = $l->t('Your password on %s was reset.', [$instanceUrl]); |
122
|
|
|
$event->setSubject(Provider::PASSWORD_RESET_SELF); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->activityManager->publish($event); |
127
|
|
|
|
128
|
|
|
if ($user->getEMailAddress() !== null) { |
129
|
|
|
$template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [ |
130
|
|
|
'displayname' => $user->getDisplayName(), |
131
|
|
|
'emailAddress' => $user->getEMailAddress(), |
132
|
|
|
'instanceUrl' => $instanceUrl, |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
$template->setSubject($l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
136
|
|
|
$template->addHeader(); |
137
|
|
|
$template->addHeading($l->t('Password changed for %s', [$user->getDisplayName()]), false); |
138
|
|
|
$template->addBodyText($text . ' ' . $l->t('If you did not request this, please contact an administrator.')); |
139
|
|
|
$template->addFooter(); |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
$message = $this->mailer->createMessage(); |
143
|
|
|
$message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
144
|
|
|
$message->useTemplate($template); |
145
|
|
|
$this->mailer->send($message); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param IUser $user |
151
|
|
|
* @param string|null $oldMailAddress |
152
|
|
|
* @throws \InvalidArgumentException |
153
|
|
|
* @throws \BadMethodCallException |
154
|
|
|
*/ |
155
|
|
|
public function onChangeEmail(IUser $user, $oldMailAddress) { |
156
|
|
|
if ($oldMailAddress === $user->getEMailAddress() || |
157
|
|
|
$user->getLastLogin() === 0) { |
158
|
|
|
// Email didn't really change or user didn't login, |
159
|
|
|
// so don't create activities and emails. |
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$event = $this->activityManager->generateEvent(); |
164
|
|
|
$event->setApp('settings') |
165
|
|
|
->setType('personal_settings') |
166
|
|
|
->setAffectedUser($user->getUID()); |
167
|
|
|
|
168
|
|
|
$instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
169
|
|
|
$language = $this->languageFactory->getUserLanguage($user); |
170
|
|
|
$l = $this->languageFactory->get('settings', $language); |
171
|
|
|
|
172
|
|
|
$actor = $this->userSession->getUser(); |
173
|
|
|
if ($actor instanceof IUser) { |
174
|
|
|
$subject = Provider::EMAIL_CHANGED_SELF; |
175
|
|
|
if ($actor->getUID() !== $user->getUID()) { |
176
|
|
|
$subject = Provider::EMAIL_CHANGED; |
177
|
|
|
} |
178
|
|
|
$text = $l->t('Your email address on %s was changed.', [$instanceUrl]); |
179
|
|
|
$event->setAuthor($actor->getUID()) |
180
|
|
|
->setSubject($subject); |
181
|
|
|
} else { |
182
|
|
|
if ($this->config->getAppValue('settings', 'disable_activity.email_address_changed_by_admin', 'no') === 'yes') { |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
$text = $l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); |
186
|
|
|
$event->setSubject(Provider::EMAIL_CHANGED); |
187
|
|
|
} |
188
|
|
|
$this->activityManager->publish($event); |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
if ($oldMailAddress !== null) { |
192
|
|
|
$template = $this->mailer->createEMailTemplate('settings.EmailChanged', [ |
193
|
|
|
'displayname' => $user->getDisplayName(), |
194
|
|
|
'newEMailAddress' => $user->getEMailAddress(), |
195
|
|
|
'oldEMailAddress' => $oldMailAddress, |
196
|
|
|
'instanceUrl' => $instanceUrl, |
197
|
|
|
]); |
198
|
|
|
|
199
|
|
|
$template->setSubject($l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
200
|
|
|
$template->addHeader(); |
201
|
|
|
$template->addHeading($l->t('Email address changed for %s', [$user->getDisplayName()]), false); |
202
|
|
|
$template->addBodyText($text . ' ' . $l->t('If you did not request this, please contact an administrator.')); |
203
|
|
|
if ($user->getEMailAddress()) { |
204
|
|
|
$template->addBodyText($l->t('The new email address is %s', [$user->getEMailAddress()])); |
205
|
|
|
} |
206
|
|
|
$template->addFooter(); |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
$message = $this->mailer->createMessage(); |
210
|
|
|
$message->setTo([$oldMailAddress => $user->getDisplayName()]); |
211
|
|
|
$message->useTemplate($template); |
212
|
|
|
$this->mailer->send($message); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|