11<?php
22
3+ declare (strict_types=1 );
4+
35/**
46 * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
57 * SPDX-License-Identifier: AGPL-3.0-or-later
911use OCA \Settings \Activity \Provider ;
1012use OCP \Activity \IManager as IActivityManager ;
1113use OCP \Defaults ;
14+ use OCP \EventDispatcher \Event ;
15+ use OCP \EventDispatcher \IEventListener ;
1216use OCP \IConfig ;
1317use OCP \IGroupManager ;
1418use OCP \IURLGenerator ;
1721use OCP \IUserSession ;
1822use OCP \L10N \IFactory ;
1923use OCP \Mail \IMailer ;
24+ use OCP \User \Events \PasswordUpdatedEvent ;
25+ use OCP \User \Events \UserChangedEvent ;
2026
21- class Hooks {
27+ /**
28+ * @template-implements IEventListener<PasswordUpdatedEvent|UserChangedEvent>
29+ */
30+ class Hooks implements IEventListener {
2231
2332 public function __construct (
2433 protected IActivityManager $ activityManager ,
@@ -33,16 +42,19 @@ public function __construct(
3342 ) {
3443 }
3544
36- /**
37- * @param string $uid
38- * @throws \InvalidArgumentException
39- * @throws \BadMethodCallException
40- * @throws \Exception
41- */
42- public function onChangePassword ( $ uid ) {
43- $ user = $ this -> userManager -> get ( $ uid );
45+ public function handle ( Event $ event ): void {
46+ if ( $ event instanceof PasswordUpdatedEvent) {
47+ $ this -> onChangePassword ( $ event );
48+ }
49+ if ( $ event instanceof UserChangedEvent) {
50+ $ this -> onChangeEmail ( $ event );
51+ }
52+ }
4453
45- if (!$ user instanceof IUser || $ user ->getLastLogin () === 0 ) {
54+ public function onChangePassword (PasswordUpdatedEvent $ handle ): void {
55+ $ user = $ handle ->getUser ();
56+
57+ if ($ user ->getLastLogin () === 0 ) {
4658 // User didn't login, so don't create activities and emails.
4759 return ;
4860 }
@@ -89,6 +101,7 @@ public function onChangePassword($uid) {
89101 'displayname ' => $ user ->getDisplayName (),
90102 'emailAddress ' => $ user ->getEMailAddress (),
91103 'instanceUrl ' => $ instanceUrl ,
104+ 'event ' => $ handle ,
92105 ]);
93106
94107 $ template ->setSubject ($ l ->t ('Password for %1$s changed on %2$s ' , [$ user ->getDisplayName (), $ instanceName ]));
@@ -105,13 +118,14 @@ public function onChangePassword($uid) {
105118 }
106119 }
107120
108- /**
109- * @param IUser $user
110- * @param string|null $oldMailAddress
111- * @throws \InvalidArgumentException
112- * @throws \BadMethodCallException
113- */
114- public function onChangeEmail (IUser $ user , $ oldMailAddress ) {
121+ public function onChangeEmail (UserChangedEvent $ handle ): void {
122+ if ($ handle ->getFeature () !== 'eMailAddress ' ) {
123+ return ;
124+ }
125+
126+ $ oldMailAddress = $ handle ->getOldValue ();
127+ $ user = $ handle ->getUser ();
128+
115129 if ($ oldMailAddress === $ user ->getEMailAddress ()
116130 || $ user ->getLastLogin () === 0 ) {
117131 // Email didn't really change or user didn't login,
0 commit comments