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