1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Joas Schilling <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OC\Settings\Activity; |
26
|
|
|
|
27
|
|
|
use OCP\Activity\IEvent; |
28
|
|
|
use OCP\Activity\IManager; |
29
|
|
|
use OCP\Activity\IProvider; |
30
|
|
|
use OCP\IL10N; |
31
|
|
|
use OCP\IURLGenerator; |
32
|
|
|
use OCP\IUser; |
33
|
|
|
use OCP\IUserManager; |
34
|
|
|
use OCP\L10N\IFactory; |
35
|
|
|
|
36
|
|
|
class Provider implements IProvider { |
37
|
|
|
|
38
|
|
|
public const PASSWORD_CHANGED_BY = 'password_changed_by'; |
39
|
|
|
public const PASSWORD_CHANGED_SELF = 'password_changed_self'; |
40
|
|
|
public const PASSWORD_RESET = 'password_changed'; |
41
|
|
|
public const EMAIL_CHANGED_BY = 'email_changed_by'; |
42
|
|
|
public const EMAIL_CHANGED_SELF = 'email_changed_self'; |
43
|
|
|
public const EMAIL_CHANGED = 'email_changed'; |
44
|
|
|
public const APP_TOKEN_CREATED = 'app_token_created'; |
45
|
|
|
public const APP_TOKEN_UPDATED = 'app_token_updated'; |
46
|
|
|
public const APP_TOKEN_DELETED = 'app_token_deleted'; |
47
|
|
|
|
48
|
|
|
/** @var IFactory */ |
49
|
|
|
protected $languageFactory; |
50
|
|
|
|
51
|
|
|
/** @var IL10N */ |
52
|
|
|
protected $l; |
53
|
|
|
|
54
|
|
|
/** @var IURLGenerator */ |
55
|
|
|
protected $url; |
56
|
|
|
|
57
|
|
|
/** @var IUserManager */ |
58
|
|
|
protected $userManager; |
59
|
|
|
|
60
|
|
|
/** @var IManager */ |
61
|
|
|
private $activityManager; |
62
|
|
|
|
63
|
|
|
/** @var string[] cached displayNames - key is the UID and value the displayname */ |
64
|
|
|
protected $displayNames = []; |
65
|
|
|
|
66
|
|
|
public function __construct(IFactory $languageFactory, |
67
|
|
|
IURLGenerator $url, |
68
|
|
|
IUserManager $userManager, |
69
|
|
|
IManager $activityManager) { |
70
|
|
|
$this->languageFactory = $languageFactory; |
71
|
|
|
$this->url = $url; |
72
|
|
|
$this->userManager = $userManager; |
73
|
|
|
$this->activityManager = $activityManager; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $language |
78
|
|
|
* @param IEvent $event |
79
|
|
|
* @param IEvent|null $previousEvent |
80
|
|
|
* @return IEvent |
81
|
|
|
* @throws \InvalidArgumentException |
82
|
|
|
* @since 11.0.0 |
83
|
|
|
*/ |
84
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent { |
85
|
|
|
if ($event->getApp() !== 'settings') { |
86
|
|
|
throw new \InvalidArgumentException('Unknown app'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->l = $this->languageFactory->get('settings', $language); |
90
|
|
|
|
91
|
|
|
if ($this->activityManager->getRequirePNG()) { |
92
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png'))); |
93
|
|
|
} else { |
94
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg'))); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($event->getSubject() === self::PASSWORD_CHANGED_BY) { |
98
|
|
|
$subject = $this->l->t('{actor} changed your password'); |
99
|
|
|
} else if ($event->getSubject() === self::PASSWORD_CHANGED_SELF) { |
100
|
|
|
$subject = $this->l->t('You changed your password'); |
101
|
|
|
} else if ($event->getSubject() === self::PASSWORD_RESET) { |
102
|
|
|
$subject = $this->l->t('Your password was reset by an administrator'); |
103
|
|
|
|
104
|
|
|
} else if ($event->getSubject() === self::EMAIL_CHANGED_BY) { |
105
|
|
|
$subject = $this->l->t('{actor} changed your email address'); |
106
|
|
|
} else if ($event->getSubject() === self::EMAIL_CHANGED_SELF) { |
107
|
|
|
$subject = $this->l->t('You changed your email address'); |
108
|
|
|
} else if ($event->getSubject() === self::EMAIL_CHANGED) { |
109
|
|
|
$subject = $this->l->t('Your email address was changed by an administrator'); |
110
|
|
|
|
111
|
|
|
} else if ($event->getSubject() === self::APP_TOKEN_CREATED) { |
112
|
|
|
$subject = $this->l->t('You created app password "{token}"'); |
113
|
|
|
} else if ($event->getSubject() === self::APP_TOKEN_UPDATED) { |
114
|
|
|
$subject = $this->l->t('You updated app password "{token}"'); |
115
|
|
|
} else if ($event->getSubject() === self::APP_TOKEN_DELETED) { |
116
|
|
|
$subject = $this->l->t('You deleted app password "{token}"'); |
117
|
|
|
|
118
|
|
|
} else { |
119
|
|
|
throw new \InvalidArgumentException('Unknown subject'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$parsedParameters = $this->getParameters($event); |
123
|
|
|
$this->setSubjects($event, $subject, $parsedParameters); |
124
|
|
|
|
125
|
|
|
return $event; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param IEvent $event |
130
|
|
|
* @return array |
131
|
|
|
* @throws \InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
protected function getParameters(IEvent $event): array { |
134
|
|
|
$subject = $event->getSubject(); |
135
|
|
|
$parameters = $event->getSubjectParameters(); |
136
|
|
|
|
137
|
|
|
switch ($subject) { |
138
|
|
|
case self::PASSWORD_CHANGED_SELF: |
139
|
|
|
case self::PASSWORD_RESET: |
140
|
|
|
case self::EMAIL_CHANGED_SELF: |
141
|
|
|
case self::EMAIL_CHANGED: |
142
|
|
|
return []; |
143
|
|
|
case self::PASSWORD_CHANGED_BY: |
144
|
|
|
case self::EMAIL_CHANGED_BY: |
145
|
|
|
return [ |
146
|
|
|
'actor' => $this->generateUserParameter($parameters[0]), |
147
|
|
|
]; |
148
|
|
|
case self::APP_TOKEN_CREATED: |
149
|
|
|
case self::APP_TOKEN_UPDATED: |
150
|
|
|
case self::APP_TOKEN_DELETED: |
151
|
|
|
return [ |
152
|
|
|
'token' => [ |
153
|
|
|
'type' => 'highlight', |
154
|
|
|
'id' => $event->getObjectId(), |
155
|
|
|
'name' => $parameters[0], |
156
|
|
|
] |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
throw new \InvalidArgumentException('Unknown subject'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param IEvent $event |
165
|
|
|
* @param string $subject |
166
|
|
|
* @param array $parameters |
167
|
|
|
* @throws \InvalidArgumentException |
168
|
|
|
*/ |
169
|
|
|
protected function setSubjects(IEvent $event, string $subject, array $parameters): void { |
170
|
|
|
$placeholders = $replacements = []; |
171
|
|
|
foreach ($parameters as $placeholder => $parameter) { |
172
|
|
|
$placeholders[] = '{' . $placeholder . '}'; |
173
|
|
|
$replacements[] = $parameter['name']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
177
|
|
|
->setRichSubject($subject, $parameters); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
protected function generateUserParameter(string $uid): array { |
181
|
|
|
if (!isset($this->displayNames[$uid])) { |
182
|
|
|
$this->displayNames[$uid] = $this->getDisplayName($uid); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return [ |
186
|
|
|
'type' => 'user', |
187
|
|
|
'id' => $uid, |
188
|
|
|
'name' => $this->displayNames[$uid], |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function getDisplayName(string $uid): string { |
193
|
|
|
$user = $this->userManager->get($uid); |
194
|
|
|
if ($user instanceof IUser) { |
195
|
|
|
return $user->getDisplayName(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $uid; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|