Passed
Push — master ( aced24...a307d8 )
by Roeland
12:44 queued 11s
created
apps/settings/lib/Hooks.php 1 patch
Indentation   +258 added lines, -258 removed lines patch added patch discarded remove patch
@@ -43,262 +43,262 @@
 block discarded – undo
43 43
 
44 44
 class Hooks {
45 45
 
46
-	/** @var IActivityManager */
47
-	protected $activityManager;
48
-	/** @var IGroupManager|\OC\Group\Manager */
49
-	protected $groupManager;
50
-	/** @var IUserManager */
51
-	protected $userManager;
52
-	/** @var IUserSession */
53
-	protected $userSession;
54
-	/** @var IURLGenerator */
55
-	protected $urlGenerator;
56
-	/** @var IMailer */
57
-	protected $mailer;
58
-	/** @var IConfig */
59
-	protected $config;
60
-	/** @var IFactory */
61
-	protected $languageFactory;
62
-	/** @var IL10N */
63
-	protected $l;
64
-
65
-	public function __construct(IActivityManager $activityManager,
66
-								IGroupManager $groupManager,
67
-								IUserManager $userManager,
68
-								IUserSession $userSession,
69
-								IURLGenerator $urlGenerator,
70
-								IMailer $mailer,
71
-								IConfig $config,
72
-								IFactory $languageFactory,
73
-								IL10N $l) {
74
-		$this->activityManager = $activityManager;
75
-		$this->groupManager = $groupManager;
76
-		$this->userManager = $userManager;
77
-		$this->userSession = $userSession;
78
-		$this->urlGenerator = $urlGenerator;
79
-		$this->mailer = $mailer;
80
-		$this->config = $config;
81
-		$this->languageFactory = $languageFactory;
82
-		$this->l = $l;
83
-	}
84
-
85
-	/**
86
-	 * @param string $uid
87
-	 * @throws \InvalidArgumentException
88
-	 * @throws \BadMethodCallException
89
-	 * @throws \Exception
90
-	 */
91
-	public function onChangePassword($uid) {
92
-		$user = $this->userManager->get($uid);
93
-
94
-		if (!$user instanceof IUser || $user->getLastLogin() === 0) {
95
-			// User didn't login, so don't create activities and emails.
96
-			return;
97
-		}
98
-
99
-		$event = $this->activityManager->generateEvent();
100
-		$event->setApp('settings')
101
-			->setType('personal_settings')
102
-			->setAffectedUser($user->getUID());
103
-
104
-		$instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
105
-
106
-		$actor = $this->userSession->getUser();
107
-		if ($actor instanceof IUser) {
108
-			if ($actor->getUID() !== $user->getUID()) {
109
-				// Admin changed the password through the user panel
110
-				$this->l = $this->languageFactory->get(
111
-					'settings',
112
-					$this->config->getUserValue(
113
-						$user->getUID(), 'core', 'lang',
114
-						$this->config->getSystemValue('default_language', 'en')
115
-					)
116
-				);
117
-
118
-				$text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
119
-				$event->setAuthor($actor->getUID())
120
-					->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
121
-			} else {
122
-				// User changed their password themselves through settings
123
-				$text = $this->l->t('Your password on %s was changed.', [$instanceUrl]);
124
-				$event->setAuthor($actor->getUID())
125
-					->setSubject(Provider::PASSWORD_CHANGED_SELF);
126
-			}
127
-		} else {
128
-			if (PHP_SAPI === 'cli') {
129
-				// Admin used occ to reset the password
130
-				$text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
131
-				$event->setSubject(Provider::PASSWORD_RESET);
132
-			} else {
133
-				// User reset their password from Lost page
134
-				$text = $this->l->t('Your password on %s was reset.', [$instanceUrl]);
135
-				$event->setSubject(Provider::PASSWORD_RESET_SELF);
136
-			}
137
-		}
138
-
139
-		$this->activityManager->publish($event);
140
-
141
-		if ($user->getEMailAddress() !== null) {
142
-			$template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [
143
-				'displayname' => $user->getDisplayName(),
144
-				'emailAddress' => $user->getEMailAddress(),
145
-				'instanceUrl' => $instanceUrl,
146
-			]);
147
-
148
-			$template->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
149
-			$template->addHeader();
150
-			$template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false);
151
-			$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
152
-			$template->addFooter();
153
-
154
-
155
-			$message = $this->mailer->createMessage();
156
-			$message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
157
-			$message->useTemplate($template);
158
-			$this->mailer->send($message);
159
-		}
160
-	}
161
-
162
-	/**
163
-	 * @param IUser $user
164
-	 * @param string|null $oldMailAddress
165
-	 * @throws \InvalidArgumentException
166
-	 * @throws \BadMethodCallException
167
-	 */
168
-	public function onChangeEmail(IUser $user, $oldMailAddress) {
169
-		if ($oldMailAddress === $user->getEMailAddress() ||
170
-			$user->getLastLogin() === 0) {
171
-			// Email didn't really change or user didn't login,
172
-			// so don't create activities and emails.
173
-			return;
174
-		}
175
-
176
-		$event = $this->activityManager->generateEvent();
177
-		$event->setApp('settings')
178
-			->setType('personal_settings')
179
-			->setAffectedUser($user->getUID());
180
-
181
-		$instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
182
-
183
-		$actor = $this->userSession->getUser();
184
-		if ($actor instanceof IUser) {
185
-			$subject = Provider::EMAIL_CHANGED_SELF;
186
-			if ($actor->getUID() !== $user->getUID()) {
187
-				$this->l = $this->languageFactory->get(
188
-					'settings',
189
-					$this->config->getUserValue(
190
-						$user->getUID(), 'core', 'lang',
191
-						$this->config->getSystemValue('default_language', 'en')
192
-					)
193
-				);
194
-				$subject = Provider::EMAIL_CHANGED;
195
-			}
196
-			$text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
197
-			$event->setAuthor($actor->getUID())
198
-				->setSubject($subject);
199
-		} else {
200
-			$text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
201
-			$event->setSubject(Provider::EMAIL_CHANGED);
202
-		}
203
-		$this->activityManager->publish($event);
204
-
205
-
206
-		if ($oldMailAddress !== null) {
207
-			$template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
208
-				'displayname' => $user->getDisplayName(),
209
-				'newEMailAddress' => $user->getEMailAddress(),
210
-				'oldEMailAddress' => $oldMailAddress,
211
-				'instanceUrl' => $instanceUrl,
212
-			]);
213
-
214
-			$template->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
215
-			$template->addHeader();
216
-			$template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
217
-			$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
218
-			if ($user->getEMailAddress()) {
219
-				$template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
220
-			}
221
-			$template->addFooter();
222
-
223
-
224
-			$message = $this->mailer->createMessage();
225
-			$message->setTo([$oldMailAddress => $user->getDisplayName()]);
226
-			$message->useTemplate($template);
227
-			$this->mailer->send($message);
228
-		}
229
-	}
230
-
231
-	/**
232
-	 * @param IGroup $group
233
-	 * @param IUser $user
234
-	 * @throws \InvalidArgumentException
235
-	 * @throws \BadMethodCallException
236
-	 */
237
-	public function addUserToGroup(IGroup $group, IUser $user): void {
238
-		$subAdminManager = $this->groupManager->getSubAdmin();
239
-		$usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
240
-		$usersToNotify[] = $user;
241
-
242
-
243
-		$event = $this->activityManager->generateEvent();
244
-		$event->setApp('settings')
245
-			->setType('group_settings');
246
-
247
-		$actor = $this->userSession->getUser();
248
-		if ($actor instanceof IUser) {
249
-			$event->setAuthor($actor->getUID())
250
-				->setSubject(GroupProvider::ADDED_TO_GROUP, [
251
-					'user' => $user->getUID(),
252
-					'group' => $group->getGID(),
253
-					'actor' => $actor->getUID(),
254
-				]);
255
-		} else {
256
-			$event->setSubject(GroupProvider::ADDED_TO_GROUP, [
257
-				'user' => $user->getUID(),
258
-				'group' => $group->getGID(),
259
-			]);
260
-		}
261
-
262
-		foreach ($usersToNotify as $userToNotify) {
263
-			$event->setAffectedUser($userToNotify->getUID());
264
-			$this->activityManager->publish($event);
265
-		}
266
-	}
267
-
268
-	/**
269
-	 * @param IGroup $group
270
-	 * @param IUser $user
271
-	 * @throws \InvalidArgumentException
272
-	 * @throws \BadMethodCallException
273
-	 */
274
-	public function removeUserFromGroup(IGroup $group, IUser $user): void {
275
-		$subAdminManager = $this->groupManager->getSubAdmin();
276
-		$usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
277
-		$usersToNotify[] = $user;
278
-
279
-
280
-		$event = $this->activityManager->generateEvent();
281
-		$event->setApp('settings')
282
-			->setType('group_settings');
283
-
284
-		$actor = $this->userSession->getUser();
285
-		if ($actor instanceof IUser) {
286
-			$event->setAuthor($actor->getUID())
287
-				->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
288
-					'user' => $user->getUID(),
289
-					'group' => $group->getGID(),
290
-					'actor' => $actor->getUID(),
291
-				]);
292
-		} else {
293
-			$event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
294
-				'user' => $user->getUID(),
295
-				'group' => $group->getGID(),
296
-			]);
297
-		}
298
-
299
-		foreach ($usersToNotify as $userToNotify) {
300
-			$event->setAffectedUser($userToNotify->getUID());
301
-			$this->activityManager->publish($event);
302
-		}
303
-	}
46
+    /** @var IActivityManager */
47
+    protected $activityManager;
48
+    /** @var IGroupManager|\OC\Group\Manager */
49
+    protected $groupManager;
50
+    /** @var IUserManager */
51
+    protected $userManager;
52
+    /** @var IUserSession */
53
+    protected $userSession;
54
+    /** @var IURLGenerator */
55
+    protected $urlGenerator;
56
+    /** @var IMailer */
57
+    protected $mailer;
58
+    /** @var IConfig */
59
+    protected $config;
60
+    /** @var IFactory */
61
+    protected $languageFactory;
62
+    /** @var IL10N */
63
+    protected $l;
64
+
65
+    public function __construct(IActivityManager $activityManager,
66
+                                IGroupManager $groupManager,
67
+                                IUserManager $userManager,
68
+                                IUserSession $userSession,
69
+                                IURLGenerator $urlGenerator,
70
+                                IMailer $mailer,
71
+                                IConfig $config,
72
+                                IFactory $languageFactory,
73
+                                IL10N $l) {
74
+        $this->activityManager = $activityManager;
75
+        $this->groupManager = $groupManager;
76
+        $this->userManager = $userManager;
77
+        $this->userSession = $userSession;
78
+        $this->urlGenerator = $urlGenerator;
79
+        $this->mailer = $mailer;
80
+        $this->config = $config;
81
+        $this->languageFactory = $languageFactory;
82
+        $this->l = $l;
83
+    }
84
+
85
+    /**
86
+     * @param string $uid
87
+     * @throws \InvalidArgumentException
88
+     * @throws \BadMethodCallException
89
+     * @throws \Exception
90
+     */
91
+    public function onChangePassword($uid) {
92
+        $user = $this->userManager->get($uid);
93
+
94
+        if (!$user instanceof IUser || $user->getLastLogin() === 0) {
95
+            // User didn't login, so don't create activities and emails.
96
+            return;
97
+        }
98
+
99
+        $event = $this->activityManager->generateEvent();
100
+        $event->setApp('settings')
101
+            ->setType('personal_settings')
102
+            ->setAffectedUser($user->getUID());
103
+
104
+        $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
105
+
106
+        $actor = $this->userSession->getUser();
107
+        if ($actor instanceof IUser) {
108
+            if ($actor->getUID() !== $user->getUID()) {
109
+                // Admin changed the password through the user panel
110
+                $this->l = $this->languageFactory->get(
111
+                    'settings',
112
+                    $this->config->getUserValue(
113
+                        $user->getUID(), 'core', 'lang',
114
+                        $this->config->getSystemValue('default_language', 'en')
115
+                    )
116
+                );
117
+
118
+                $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
119
+                $event->setAuthor($actor->getUID())
120
+                    ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
121
+            } else {
122
+                // User changed their password themselves through settings
123
+                $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]);
124
+                $event->setAuthor($actor->getUID())
125
+                    ->setSubject(Provider::PASSWORD_CHANGED_SELF);
126
+            }
127
+        } else {
128
+            if (PHP_SAPI === 'cli') {
129
+                // Admin used occ to reset the password
130
+                $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
131
+                $event->setSubject(Provider::PASSWORD_RESET);
132
+            } else {
133
+                // User reset their password from Lost page
134
+                $text = $this->l->t('Your password on %s was reset.', [$instanceUrl]);
135
+                $event->setSubject(Provider::PASSWORD_RESET_SELF);
136
+            }
137
+        }
138
+
139
+        $this->activityManager->publish($event);
140
+
141
+        if ($user->getEMailAddress() !== null) {
142
+            $template = $this->mailer->createEMailTemplate('settings.PasswordChanged', [
143
+                'displayname' => $user->getDisplayName(),
144
+                'emailAddress' => $user->getEMailAddress(),
145
+                'instanceUrl' => $instanceUrl,
146
+            ]);
147
+
148
+            $template->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
149
+            $template->addHeader();
150
+            $template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false);
151
+            $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
152
+            $template->addFooter();
153
+
154
+
155
+            $message = $this->mailer->createMessage();
156
+            $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
157
+            $message->useTemplate($template);
158
+            $this->mailer->send($message);
159
+        }
160
+    }
161
+
162
+    /**
163
+     * @param IUser $user
164
+     * @param string|null $oldMailAddress
165
+     * @throws \InvalidArgumentException
166
+     * @throws \BadMethodCallException
167
+     */
168
+    public function onChangeEmail(IUser $user, $oldMailAddress) {
169
+        if ($oldMailAddress === $user->getEMailAddress() ||
170
+            $user->getLastLogin() === 0) {
171
+            // Email didn't really change or user didn't login,
172
+            // so don't create activities and emails.
173
+            return;
174
+        }
175
+
176
+        $event = $this->activityManager->generateEvent();
177
+        $event->setApp('settings')
178
+            ->setType('personal_settings')
179
+            ->setAffectedUser($user->getUID());
180
+
181
+        $instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
182
+
183
+        $actor = $this->userSession->getUser();
184
+        if ($actor instanceof IUser) {
185
+            $subject = Provider::EMAIL_CHANGED_SELF;
186
+            if ($actor->getUID() !== $user->getUID()) {
187
+                $this->l = $this->languageFactory->get(
188
+                    'settings',
189
+                    $this->config->getUserValue(
190
+                        $user->getUID(), 'core', 'lang',
191
+                        $this->config->getSystemValue('default_language', 'en')
192
+                    )
193
+                );
194
+                $subject = Provider::EMAIL_CHANGED;
195
+            }
196
+            $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
197
+            $event->setAuthor($actor->getUID())
198
+                ->setSubject($subject);
199
+        } else {
200
+            $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
201
+            $event->setSubject(Provider::EMAIL_CHANGED);
202
+        }
203
+        $this->activityManager->publish($event);
204
+
205
+
206
+        if ($oldMailAddress !== null) {
207
+            $template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
208
+                'displayname' => $user->getDisplayName(),
209
+                'newEMailAddress' => $user->getEMailAddress(),
210
+                'oldEMailAddress' => $oldMailAddress,
211
+                'instanceUrl' => $instanceUrl,
212
+            ]);
213
+
214
+            $template->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
215
+            $template->addHeader();
216
+            $template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
217
+            $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
218
+            if ($user->getEMailAddress()) {
219
+                $template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
220
+            }
221
+            $template->addFooter();
222
+
223
+
224
+            $message = $this->mailer->createMessage();
225
+            $message->setTo([$oldMailAddress => $user->getDisplayName()]);
226
+            $message->useTemplate($template);
227
+            $this->mailer->send($message);
228
+        }
229
+    }
230
+
231
+    /**
232
+     * @param IGroup $group
233
+     * @param IUser $user
234
+     * @throws \InvalidArgumentException
235
+     * @throws \BadMethodCallException
236
+     */
237
+    public function addUserToGroup(IGroup $group, IUser $user): void {
238
+        $subAdminManager = $this->groupManager->getSubAdmin();
239
+        $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
240
+        $usersToNotify[] = $user;
241
+
242
+
243
+        $event = $this->activityManager->generateEvent();
244
+        $event->setApp('settings')
245
+            ->setType('group_settings');
246
+
247
+        $actor = $this->userSession->getUser();
248
+        if ($actor instanceof IUser) {
249
+            $event->setAuthor($actor->getUID())
250
+                ->setSubject(GroupProvider::ADDED_TO_GROUP, [
251
+                    'user' => $user->getUID(),
252
+                    'group' => $group->getGID(),
253
+                    'actor' => $actor->getUID(),
254
+                ]);
255
+        } else {
256
+            $event->setSubject(GroupProvider::ADDED_TO_GROUP, [
257
+                'user' => $user->getUID(),
258
+                'group' => $group->getGID(),
259
+            ]);
260
+        }
261
+
262
+        foreach ($usersToNotify as $userToNotify) {
263
+            $event->setAffectedUser($userToNotify->getUID());
264
+            $this->activityManager->publish($event);
265
+        }
266
+    }
267
+
268
+    /**
269
+     * @param IGroup $group
270
+     * @param IUser $user
271
+     * @throws \InvalidArgumentException
272
+     * @throws \BadMethodCallException
273
+     */
274
+    public function removeUserFromGroup(IGroup $group, IUser $user): void {
275
+        $subAdminManager = $this->groupManager->getSubAdmin();
276
+        $usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
277
+        $usersToNotify[] = $user;
278
+
279
+
280
+        $event = $this->activityManager->generateEvent();
281
+        $event->setApp('settings')
282
+            ->setType('group_settings');
283
+
284
+        $actor = $this->userSession->getUser();
285
+        if ($actor instanceof IUser) {
286
+            $event->setAuthor($actor->getUID())
287
+                ->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
288
+                    'user' => $user->getUID(),
289
+                    'group' => $group->getGID(),
290
+                    'actor' => $actor->getUID(),
291
+                ]);
292
+        } else {
293
+            $event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [
294
+                'user' => $user->getUID(),
295
+                'group' => $group->getGID(),
296
+            ]);
297
+        }
298
+
299
+        foreach ($usersToNotify as $userToNotify) {
300
+            $event->setAffectedUser($userToNotify->getUID());
301
+            $this->activityManager->publish($event);
302
+        }
303
+    }
304 304
 }
Please login to merge, or discard this patch.
apps/settings/lib/Activity/Provider.php 1 patch
Indentation   +183 added lines, -183 removed lines patch added patch discarded remove patch
@@ -38,187 +38,187 @@
 block discarded – undo
38 38
 use OCP\L10N\IFactory;
39 39
 
40 40
 class Provider implements IProvider {
41
-	public const PASSWORD_CHANGED_BY = 'password_changed_by';
42
-	public const PASSWORD_CHANGED_SELF = 'password_changed_self';
43
-	public const PASSWORD_RESET = 'password_changed';
44
-	public const PASSWORD_RESET_SELF = 'password_reset_self';
45
-	public const EMAIL_CHANGED_BY = 'email_changed_by';
46
-	public const EMAIL_CHANGED_SELF = 'email_changed_self';
47
-	public const EMAIL_CHANGED = 'email_changed';
48
-	public const APP_TOKEN_CREATED = 'app_token_created';
49
-	public const APP_TOKEN_DELETED = 'app_token_deleted';
50
-	public const APP_TOKEN_RENAMED = 'app_token_renamed';
51
-	public const APP_TOKEN_FILESYSTEM_GRANTED = 'app_token_filesystem_granted';
52
-	public const APP_TOKEN_FILESYSTEM_REVOKED = 'app_token_filesystem_revoked';
53
-
54
-	/** @var IFactory */
55
-	protected $languageFactory;
56
-
57
-	/** @var IL10N */
58
-	protected $l;
59
-
60
-	/** @var IURLGenerator */
61
-	protected $url;
62
-
63
-	/** @var IUserManager */
64
-	protected $userManager;
65
-
66
-	/** @var IManager */
67
-	private $activityManager;
68
-
69
-	/** @var string[] cached displayNames - key is the UID and value the displayname */
70
-	protected $displayNames = [];
71
-
72
-	public function __construct(IFactory $languageFactory,
73
-								IURLGenerator $url,
74
-								IUserManager $userManager,
75
-								IManager $activityManager) {
76
-		$this->languageFactory = $languageFactory;
77
-		$this->url = $url;
78
-		$this->userManager = $userManager;
79
-		$this->activityManager = $activityManager;
80
-	}
81
-
82
-	/**
83
-	 * @param string $language
84
-	 * @param IEvent $event
85
-	 * @param IEvent|null $previousEvent
86
-	 * @return IEvent
87
-	 * @throws \InvalidArgumentException
88
-	 * @since 11.0.0
89
-	 */
90
-	public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
91
-		if ($event->getApp() !== 'settings') {
92
-			throw new \InvalidArgumentException('Unknown app');
93
-		}
94
-
95
-		$this->l = $this->languageFactory->get('settings', $language);
96
-
97
-		if ($this->activityManager->getRequirePNG()) {
98
-			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png')));
99
-		} else {
100
-			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg')));
101
-		}
102
-
103
-		if ($event->getSubject() === self::PASSWORD_CHANGED_BY) {
104
-			$subject = $this->l->t('{actor} changed your password');
105
-		} elseif ($event->getSubject() === self::PASSWORD_CHANGED_SELF) {
106
-			$subject = $this->l->t('You changed your password');
107
-		} elseif ($event->getSubject() === self::PASSWORD_RESET) {
108
-			$subject = $this->l->t('Your password was reset by an administrator');
109
-		} elseif ($event->getSubject() === self::PASSWORD_RESET_SELF) {
110
-			$subject = $this->l->t('Your password was reset');
111
-		} elseif ($event->getSubject() === self::EMAIL_CHANGED_BY) {
112
-			$subject = $this->l->t('{actor} changed your email address');
113
-		} elseif ($event->getSubject() === self::EMAIL_CHANGED_SELF) {
114
-			$subject = $this->l->t('You changed your email address');
115
-		} elseif ($event->getSubject() === self::EMAIL_CHANGED) {
116
-			$subject = $this->l->t('Your email address was changed by an administrator');
117
-		} elseif ($event->getSubject() === self::APP_TOKEN_CREATED) {
118
-			$subject = $this->l->t('You created app password "{token}"');
119
-		} elseif ($event->getSubject() === self::APP_TOKEN_DELETED) {
120
-			$subject = $this->l->t('You deleted app password "{token}"');
121
-		} elseif ($event->getSubject() === self::APP_TOKEN_RENAMED) {
122
-			$subject = $this->l->t('You renamed app password "{token}" to "{newToken}"');
123
-		} elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_GRANTED) {
124
-			$subject = $this->l->t('You granted filesystem access to app password "{token}"');
125
-		} elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_REVOKED) {
126
-			$subject = $this->l->t('You revoked filesystem access from app password "{token}"');
127
-		} else {
128
-			throw new \InvalidArgumentException('Unknown subject');
129
-		}
130
-
131
-		$parsedParameters = $this->getParameters($event);
132
-		$this->setSubjects($event, $subject, $parsedParameters);
133
-
134
-		return $event;
135
-	}
136
-
137
-	/**
138
-	 * @param IEvent $event
139
-	 * @return array
140
-	 * @throws \InvalidArgumentException
141
-	 */
142
-	protected function getParameters(IEvent $event): array {
143
-		$subject = $event->getSubject();
144
-		$parameters = $event->getSubjectParameters();
145
-
146
-		switch ($subject) {
147
-			case self::PASSWORD_CHANGED_SELF:
148
-			case self::PASSWORD_RESET:
149
-			case self::PASSWORD_RESET_SELF:
150
-			case self::EMAIL_CHANGED_SELF:
151
-			case self::EMAIL_CHANGED:
152
-				return [];
153
-			case self::PASSWORD_CHANGED_BY:
154
-			case self::EMAIL_CHANGED_BY:
155
-				return [
156
-					'actor' => $this->generateUserParameter($parameters[0]),
157
-				];
158
-			case self::APP_TOKEN_CREATED:
159
-			case self::APP_TOKEN_DELETED:
160
-			case self::APP_TOKEN_FILESYSTEM_GRANTED:
161
-			case self::APP_TOKEN_FILESYSTEM_REVOKED:
162
-				return [
163
-					'token' => [
164
-						'type' => 'highlight',
165
-						'id' => $event->getObjectId(),
166
-						'name' => $parameters['name'],
167
-					]
168
-				];
169
-			case self::APP_TOKEN_RENAMED:
170
-				return [
171
-					'token' => [
172
-						'type' => 'highlight',
173
-						'id' => $event->getObjectId(),
174
-						'name' => $parameters['name'],
175
-					],
176
-					'newToken' => [
177
-						'type' => 'highlight',
178
-						'id' => $event->getObjectId(),
179
-						'name' => $parameters['newName'],
180
-					]
181
-				];
182
-		}
183
-
184
-		throw new \InvalidArgumentException('Unknown subject');
185
-	}
186
-
187
-	/**
188
-	 * @param IEvent $event
189
-	 * @param string $subject
190
-	 * @param array $parameters
191
-	 * @throws \InvalidArgumentException
192
-	 */
193
-	protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
194
-		$placeholders = $replacements = [];
195
-		foreach ($parameters as $placeholder => $parameter) {
196
-			$placeholders[] = '{' . $placeholder . '}';
197
-			$replacements[] = $parameter['name'];
198
-		}
199
-
200
-		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
201
-			->setRichSubject($subject, $parameters);
202
-	}
203
-
204
-	protected function generateUserParameter(string $uid): array {
205
-		if (!isset($this->displayNames[$uid])) {
206
-			$this->displayNames[$uid] = $this->getDisplayName($uid);
207
-		}
208
-
209
-		return [
210
-			'type' => 'user',
211
-			'id' => $uid,
212
-			'name' => $this->displayNames[$uid],
213
-		];
214
-	}
215
-
216
-	protected function getDisplayName(string $uid): string {
217
-		$user = $this->userManager->get($uid);
218
-		if ($user instanceof IUser) {
219
-			return $user->getDisplayName();
220
-		}
221
-
222
-		return $uid;
223
-	}
41
+    public const PASSWORD_CHANGED_BY = 'password_changed_by';
42
+    public const PASSWORD_CHANGED_SELF = 'password_changed_self';
43
+    public const PASSWORD_RESET = 'password_changed';
44
+    public const PASSWORD_RESET_SELF = 'password_reset_self';
45
+    public const EMAIL_CHANGED_BY = 'email_changed_by';
46
+    public const EMAIL_CHANGED_SELF = 'email_changed_self';
47
+    public const EMAIL_CHANGED = 'email_changed';
48
+    public const APP_TOKEN_CREATED = 'app_token_created';
49
+    public const APP_TOKEN_DELETED = 'app_token_deleted';
50
+    public const APP_TOKEN_RENAMED = 'app_token_renamed';
51
+    public const APP_TOKEN_FILESYSTEM_GRANTED = 'app_token_filesystem_granted';
52
+    public const APP_TOKEN_FILESYSTEM_REVOKED = 'app_token_filesystem_revoked';
53
+
54
+    /** @var IFactory */
55
+    protected $languageFactory;
56
+
57
+    /** @var IL10N */
58
+    protected $l;
59
+
60
+    /** @var IURLGenerator */
61
+    protected $url;
62
+
63
+    /** @var IUserManager */
64
+    protected $userManager;
65
+
66
+    /** @var IManager */
67
+    private $activityManager;
68
+
69
+    /** @var string[] cached displayNames - key is the UID and value the displayname */
70
+    protected $displayNames = [];
71
+
72
+    public function __construct(IFactory $languageFactory,
73
+                                IURLGenerator $url,
74
+                                IUserManager $userManager,
75
+                                IManager $activityManager) {
76
+        $this->languageFactory = $languageFactory;
77
+        $this->url = $url;
78
+        $this->userManager = $userManager;
79
+        $this->activityManager = $activityManager;
80
+    }
81
+
82
+    /**
83
+     * @param string $language
84
+     * @param IEvent $event
85
+     * @param IEvent|null $previousEvent
86
+     * @return IEvent
87
+     * @throws \InvalidArgumentException
88
+     * @since 11.0.0
89
+     */
90
+    public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
91
+        if ($event->getApp() !== 'settings') {
92
+            throw new \InvalidArgumentException('Unknown app');
93
+        }
94
+
95
+        $this->l = $this->languageFactory->get('settings', $language);
96
+
97
+        if ($this->activityManager->getRequirePNG()) {
98
+            $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png')));
99
+        } else {
100
+            $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg')));
101
+        }
102
+
103
+        if ($event->getSubject() === self::PASSWORD_CHANGED_BY) {
104
+            $subject = $this->l->t('{actor} changed your password');
105
+        } elseif ($event->getSubject() === self::PASSWORD_CHANGED_SELF) {
106
+            $subject = $this->l->t('You changed your password');
107
+        } elseif ($event->getSubject() === self::PASSWORD_RESET) {
108
+            $subject = $this->l->t('Your password was reset by an administrator');
109
+        } elseif ($event->getSubject() === self::PASSWORD_RESET_SELF) {
110
+            $subject = $this->l->t('Your password was reset');
111
+        } elseif ($event->getSubject() === self::EMAIL_CHANGED_BY) {
112
+            $subject = $this->l->t('{actor} changed your email address');
113
+        } elseif ($event->getSubject() === self::EMAIL_CHANGED_SELF) {
114
+            $subject = $this->l->t('You changed your email address');
115
+        } elseif ($event->getSubject() === self::EMAIL_CHANGED) {
116
+            $subject = $this->l->t('Your email address was changed by an administrator');
117
+        } elseif ($event->getSubject() === self::APP_TOKEN_CREATED) {
118
+            $subject = $this->l->t('You created app password "{token}"');
119
+        } elseif ($event->getSubject() === self::APP_TOKEN_DELETED) {
120
+            $subject = $this->l->t('You deleted app password "{token}"');
121
+        } elseif ($event->getSubject() === self::APP_TOKEN_RENAMED) {
122
+            $subject = $this->l->t('You renamed app password "{token}" to "{newToken}"');
123
+        } elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_GRANTED) {
124
+            $subject = $this->l->t('You granted filesystem access to app password "{token}"');
125
+        } elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_REVOKED) {
126
+            $subject = $this->l->t('You revoked filesystem access from app password "{token}"');
127
+        } else {
128
+            throw new \InvalidArgumentException('Unknown subject');
129
+        }
130
+
131
+        $parsedParameters = $this->getParameters($event);
132
+        $this->setSubjects($event, $subject, $parsedParameters);
133
+
134
+        return $event;
135
+    }
136
+
137
+    /**
138
+     * @param IEvent $event
139
+     * @return array
140
+     * @throws \InvalidArgumentException
141
+     */
142
+    protected function getParameters(IEvent $event): array {
143
+        $subject = $event->getSubject();
144
+        $parameters = $event->getSubjectParameters();
145
+
146
+        switch ($subject) {
147
+            case self::PASSWORD_CHANGED_SELF:
148
+            case self::PASSWORD_RESET:
149
+            case self::PASSWORD_RESET_SELF:
150
+            case self::EMAIL_CHANGED_SELF:
151
+            case self::EMAIL_CHANGED:
152
+                return [];
153
+            case self::PASSWORD_CHANGED_BY:
154
+            case self::EMAIL_CHANGED_BY:
155
+                return [
156
+                    'actor' => $this->generateUserParameter($parameters[0]),
157
+                ];
158
+            case self::APP_TOKEN_CREATED:
159
+            case self::APP_TOKEN_DELETED:
160
+            case self::APP_TOKEN_FILESYSTEM_GRANTED:
161
+            case self::APP_TOKEN_FILESYSTEM_REVOKED:
162
+                return [
163
+                    'token' => [
164
+                        'type' => 'highlight',
165
+                        'id' => $event->getObjectId(),
166
+                        'name' => $parameters['name'],
167
+                    ]
168
+                ];
169
+            case self::APP_TOKEN_RENAMED:
170
+                return [
171
+                    'token' => [
172
+                        'type' => 'highlight',
173
+                        'id' => $event->getObjectId(),
174
+                        'name' => $parameters['name'],
175
+                    ],
176
+                    'newToken' => [
177
+                        'type' => 'highlight',
178
+                        'id' => $event->getObjectId(),
179
+                        'name' => $parameters['newName'],
180
+                    ]
181
+                ];
182
+        }
183
+
184
+        throw new \InvalidArgumentException('Unknown subject');
185
+    }
186
+
187
+    /**
188
+     * @param IEvent $event
189
+     * @param string $subject
190
+     * @param array $parameters
191
+     * @throws \InvalidArgumentException
192
+     */
193
+    protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
194
+        $placeholders = $replacements = [];
195
+        foreach ($parameters as $placeholder => $parameter) {
196
+            $placeholders[] = '{' . $placeholder . '}';
197
+            $replacements[] = $parameter['name'];
198
+        }
199
+
200
+        $event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
201
+            ->setRichSubject($subject, $parameters);
202
+    }
203
+
204
+    protected function generateUserParameter(string $uid): array {
205
+        if (!isset($this->displayNames[$uid])) {
206
+            $this->displayNames[$uid] = $this->getDisplayName($uid);
207
+        }
208
+
209
+        return [
210
+            'type' => 'user',
211
+            'id' => $uid,
212
+            'name' => $this->displayNames[$uid],
213
+        ];
214
+    }
215
+
216
+    protected function getDisplayName(string $uid): string {
217
+        $user = $this->userManager->get($uid);
218
+        if ($user instanceof IUser) {
219
+            return $user->getDisplayName();
220
+        }
221
+
222
+        return $uid;
223
+    }
224 224
 }
Please login to merge, or discard this patch.