1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling <[email protected]> |
6
|
|
|
* @author Lukas Reschke <[email protected]> |
7
|
|
|
* @author Morris Jobke <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license GNU AGPL version 3 or any later version |
10
|
|
|
* |
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
14
|
|
|
* License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OC\Settings; |
27
|
|
|
|
28
|
|
|
use OC\Settings\Activity\GroupProvider; |
29
|
|
|
use OC\Settings\Activity\Provider; |
30
|
|
|
use OCP\Activity\IManager as IActivityManager; |
31
|
|
|
use OCP\IConfig; |
32
|
|
|
use OCP\IGroup; |
33
|
|
|
use OCP\IGroupManager; |
34
|
|
|
use OCP\IL10N; |
35
|
|
|
use OCP\IURLGenerator; |
36
|
|
|
use OCP\IUser; |
37
|
|
|
use OCP\IUserManager; |
38
|
|
|
use OCP\IUserSession; |
39
|
|
|
use OCP\L10N\IFactory; |
40
|
|
|
use OCP\Mail\IMailer; |
41
|
|
|
|
42
|
|
|
class Hooks { |
43
|
|
|
|
44
|
|
|
/** @var IActivityManager */ |
45
|
|
|
protected $activityManager; |
46
|
|
|
/** @var IGroupManager|\OC\Group\Manager */ |
47
|
|
|
protected $groupManager; |
48
|
|
|
/** @var IUserManager */ |
49
|
|
|
protected $userManager; |
50
|
|
|
/** @var IUserSession */ |
51
|
|
|
protected $userSession; |
52
|
|
|
/** @var IURLGenerator */ |
53
|
|
|
protected $urlGenerator; |
54
|
|
|
/** @var IMailer */ |
55
|
|
|
protected $mailer; |
56
|
|
|
/** @var IConfig */ |
57
|
|
|
protected $config; |
58
|
|
|
/** @var IFactory */ |
59
|
|
|
protected $languageFactory; |
60
|
|
|
/** @var IL10N */ |
61
|
|
|
protected $l; |
62
|
|
|
|
63
|
|
|
public function __construct(IActivityManager $activityManager, |
64
|
|
|
IGroupManager $groupManager, |
65
|
|
|
IUserManager $userManager, |
66
|
|
|
IUserSession $userSession, |
67
|
|
|
IURLGenerator $urlGenerator, |
68
|
|
|
IMailer $mailer, |
69
|
|
|
IConfig $config, |
70
|
|
|
IFactory $languageFactory, |
71
|
|
|
IL10N $l) { |
72
|
|
|
$this->activityManager = $activityManager; |
73
|
|
|
$this->groupManager = $groupManager; |
74
|
|
|
$this->userManager = $userManager; |
75
|
|
|
$this->userSession = $userSession; |
76
|
|
|
$this->urlGenerator = $urlGenerator; |
77
|
|
|
$this->mailer = $mailer; |
78
|
|
|
$this->config = $config; |
79
|
|
|
$this->languageFactory = $languageFactory; |
80
|
|
|
$this->l = $l; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $uid |
85
|
|
|
* @throws \InvalidArgumentException |
86
|
|
|
* @throws \BadMethodCallException |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function onChangePassword($uid) { |
90
|
|
|
$user = $this->userManager->get($uid); |
91
|
|
|
|
92
|
|
|
if (!$user instanceof IUser || $user->getLastLogin() === 0) { |
93
|
|
|
// User didn't login, so don't create activities and emails. |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$event = $this->activityManager->generateEvent(); |
98
|
|
|
$event->setApp('settings') |
99
|
|
|
->setType('personal_settings') |
100
|
|
|
->setAffectedUser($user->getUID()); |
101
|
|
|
|
102
|
|
|
$instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
103
|
|
|
|
104
|
|
|
$actor = $this->userSession->getUser(); |
105
|
|
|
if ($actor instanceof IUser) { |
106
|
|
|
if ($actor->getUID() !== $user->getUID()) { |
107
|
|
|
$this->l = $this->languageFactory->get( |
108
|
|
|
'settings', |
109
|
|
|
$this->config->getUserValue( |
110
|
|
|
$user->getUID(), 'core', 'lang', |
111
|
|
|
$this->config->getSystemValue('default_language', 'en') |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
116
|
|
|
$event->setAuthor($actor->getUID()) |
117
|
|
|
->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); |
118
|
|
|
} else { |
119
|
|
|
$text = $this->l->t('Your password on %s was changed.', [$instanceUrl]); |
120
|
|
|
$event->setAuthor($actor->getUID()) |
121
|
|
|
->setSubject(Provider::PASSWORD_CHANGED_SELF); |
122
|
|
|
} |
123
|
|
|
} else { |
124
|
|
|
$text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); |
125
|
|
|
$event->setSubject(Provider::PASSWORD_RESET); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->activityManager->publish($event); |
129
|
|
|
|
130
|
|
|
if ($user->getEMailAddress() !== null) { |
131
|
|
|
$template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [ |
132
|
|
|
'displayname' => $user->getDisplayName(), |
133
|
|
|
'emailAddress' => $user->getEMailAddress(), |
134
|
|
|
'instanceUrl' => $instanceUrl, |
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
$template->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
138
|
|
|
$template->addHeader(); |
139
|
|
|
$template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false); |
140
|
|
|
$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
141
|
|
|
$template->addFooter(); |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
$message = $this->mailer->createMessage(); |
145
|
|
|
$message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
146
|
|
|
$message->useTemplate($template); |
147
|
|
|
$this->mailer->send($message); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param IUser $user |
153
|
|
|
* @param string|null $oldMailAddress |
154
|
|
|
* @throws \InvalidArgumentException |
155
|
|
|
* @throws \BadMethodCallException |
156
|
|
|
*/ |
157
|
|
|
public function onChangeEmail(IUser $user, $oldMailAddress) { |
158
|
|
|
|
159
|
|
|
if ($oldMailAddress === $user->getEMailAddress() || |
160
|
|
|
$user->getLastLogin() === 0) { |
161
|
|
|
// Email didn't really change or user didn't login, |
162
|
|
|
// so don't create activities and emails. |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$event = $this->activityManager->generateEvent(); |
167
|
|
|
$event->setApp('settings') |
168
|
|
|
->setType('personal_settings') |
169
|
|
|
->setAffectedUser($user->getUID()); |
170
|
|
|
|
171
|
|
|
$instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
172
|
|
|
|
173
|
|
|
$actor = $this->userSession->getUser(); |
174
|
|
|
if ($actor instanceof IUser) { |
175
|
|
|
$subject = Provider::EMAIL_CHANGED_SELF; |
176
|
|
|
if ($actor->getUID() !== $user->getUID()) { |
177
|
|
|
$this->l = $this->languageFactory->get( |
178
|
|
|
'settings', |
179
|
|
|
$this->config->getUserValue( |
180
|
|
|
$user->getUID(), 'core', 'lang', |
181
|
|
|
$this->config->getSystemValue('default_language', 'en') |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
$subject = Provider::EMAIL_CHANGED; |
185
|
|
|
} |
186
|
|
|
$text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]); |
187
|
|
|
$event->setAuthor($actor->getUID()) |
188
|
|
|
->setSubject($subject); |
189
|
|
|
} else { |
190
|
|
|
$text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); |
191
|
|
|
$event->setSubject(Provider::EMAIL_CHANGED); |
192
|
|
|
} |
193
|
|
|
$this->activityManager->publish($event); |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
if ($oldMailAddress !== null) { |
197
|
|
|
$template = $this->mailer->createEMailTemplate('settings.EmailChanged', [ |
198
|
|
|
'displayname' => $user->getDisplayName(), |
199
|
|
|
'newEMailAddress' => $user->getEMailAddress(), |
200
|
|
|
'oldEMailAddress' => $oldMailAddress, |
201
|
|
|
'instanceUrl' => $instanceUrl, |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
$template->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
205
|
|
|
$template->addHeader(); |
206
|
|
|
$template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false); |
207
|
|
|
$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
208
|
|
|
if ($user->getEMailAddress()) { |
209
|
|
|
$template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()])); |
210
|
|
|
} |
211
|
|
|
$template->addFooter(); |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
$message = $this->mailer->createMessage(); |
215
|
|
|
$message->setTo([$oldMailAddress => $user->getDisplayName()]); |
216
|
|
|
$message->useTemplate($template); |
217
|
|
|
$this->mailer->send($message); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param IGroup $group |
223
|
|
|
* @param IUser $user |
224
|
|
|
* @throws \InvalidArgumentException |
225
|
|
|
* @throws \BadMethodCallException |
226
|
|
|
*/ |
227
|
|
|
public function addUserToGroup(IGroup $group, IUser $user): void { |
228
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin(); |
|
|
|
|
229
|
|
|
$usersToNotify = $subAdminManager->getGroupsSubAdmins($group); |
230
|
|
|
$usersToNotify[] = $user; |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
$event = $this->activityManager->generateEvent(); |
234
|
|
|
$event->setApp('settings') |
235
|
|
|
->setType('group_settings'); |
236
|
|
|
|
237
|
|
|
$actor = $this->userSession->getUser(); |
238
|
|
|
if ($actor instanceof IUser) { |
239
|
|
|
$event->setAuthor($actor->getUID()) |
240
|
|
|
->setSubject(GroupProvider::ADDED_TO_GROUP, [ |
241
|
|
|
'user' => $user->getUID(), |
242
|
|
|
'group' => $group->getGID(), |
243
|
|
|
'actor' => $actor->getUID(), |
244
|
|
|
]); |
245
|
|
|
} else { |
246
|
|
|
$event->setSubject(GroupProvider::ADDED_TO_GROUP, [ |
247
|
|
|
'user' => $user->getUID(), |
248
|
|
|
'group' => $group->getGID(), |
249
|
|
|
]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
foreach ($usersToNotify as $userToNotify) { |
253
|
|
|
$event->setAffectedUser($userToNotify->getUID()); |
254
|
|
|
$this->activityManager->publish($event); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param IGroup $group |
260
|
|
|
* @param IUser $user |
261
|
|
|
* @throws \InvalidArgumentException |
262
|
|
|
* @throws \BadMethodCallException |
263
|
|
|
*/ |
264
|
|
|
public function removeUserFromGroup(IGroup $group, IUser $user): void { |
265
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin(); |
266
|
|
|
$usersToNotify = $subAdminManager->getGroupsSubAdmins($group); |
267
|
|
|
$usersToNotify[] = $user; |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
$event = $this->activityManager->generateEvent(); |
271
|
|
|
$event->setApp('settings') |
272
|
|
|
->setType('group_settings'); |
273
|
|
|
|
274
|
|
|
$actor = $this->userSession->getUser(); |
275
|
|
|
if ($actor instanceof IUser) { |
276
|
|
|
$event->setAuthor($actor->getUID()) |
277
|
|
|
->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ |
278
|
|
|
'user' => $user->getUID(), |
279
|
|
|
'group' => $group->getGID(), |
280
|
|
|
'actor' => $actor->getUID(), |
281
|
|
|
]); |
282
|
|
|
} else { |
283
|
|
|
$event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ |
284
|
|
|
'user' => $user->getUID(), |
285
|
|
|
'group' => $group->getGID(), |
286
|
|
|
]); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
foreach ($usersToNotify as $userToNotify) { |
290
|
|
|
$event->setAffectedUser($userToNotify->getUID()); |
291
|
|
|
$this->activityManager->publish($event); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|