1111namespace Joomla \Plugin \User \Joomla \Extension ;
1212
1313use Joomla \CMS \Component \ComponentHelper ;
14+ use Joomla \CMS \Event \Model \PrepareFormEvent ;
15+ use Joomla \CMS \Event \User \AfterDeleteEvent ;
16+ use Joomla \CMS \Event \User \AfterLoginEvent ;
17+ use Joomla \CMS \Event \User \AfterSaveEvent ;
18+ use Joomla \CMS \Event \User \LoginEvent ;
19+ use Joomla \CMS \Event \User \LogoutEvent ;
1420use Joomla \CMS \Factory ;
1521use Joomla \CMS \Language \LanguageFactoryInterface ;
1622use Joomla \CMS \Log \Log ;
2329use Joomla \Database \DatabaseAwareTrait ;
2430use Joomla \Database \Exception \ExecutionFailureException ;
2531use Joomla \Database \ParameterType ;
32+ use Joomla \Event \SubscriberInterface ;
2633use Joomla \Registry \Registry ;
2734
2835// phpcs:disable PSR1.Files.SideEffects
3441 *
3542 * @since 1.5
3643 */
37- final class Joomla extends CMSPlugin
44+ final class Joomla extends CMSPlugin implements SubscriberInterface
3845{
3946 use DatabaseAwareTrait;
4047 use UserFactoryAwareTrait;
4148
49+ /**
50+ * Returns an array of events this subscriber will listen to.
51+ *
52+ * @return array
53+ *
54+ * @since __DEPLOY_VERSION__
55+ */
56+ public static function getSubscribedEvents (): array
57+ {
58+ return [
59+ 'onContentPrepareForm ' => 'onContentPrepareForm ' ,
60+ 'onUserAfterDelete ' => 'onUserAfterDelete ' ,
61+ 'onUserAfterSave ' => 'onUserAfterSave ' ,
62+ 'onUserLogin ' => 'onUserLogin ' ,
63+ 'onUserLogout ' => 'onUserLogout ' ,
64+ 'onUserAfterLogin ' => 'onUserAfterLogin ' ,
65+ ];
66+ }
67+
4268 /**
4369 * Set as required the passwords fields when mail to user is set to No
4470 *
45- * @param \Joomla\CMS\Form\Form $form The form to be altered.
46- * @param mixed $data The associated data for the form.
71+ * @param PrepareFormEvent $event The event instance.
4772 *
48- * @return boolean
73+ * @return void
4974 *
5075 * @since 4.0.0
5176 */
52- public function onContentPrepareForm ($ form , $ data )
77+ public function onContentPrepareForm (PrepareFormEvent $ event )
5378 {
79+ $ form = $ event ->getForm ();
80+ $ data = $ event ->getData ();
81+
5482 // Check we are manipulating a valid user form before modifying it.
5583 $ name = $ form ->getName ();
5684
@@ -71,25 +99,24 @@ public function onContentPrepareForm($form, $data)
7199 $ form ->setFieldAttribute ('password2 ' , 'required ' , 'true ' );
72100 }
73101 }
74-
75- return true ;
76102 }
77103
78104 /**
79105 * Remove all sessions for the user name
80106 *
81107 * Method is called after user data is deleted from the database
82108 *
83- * @param array $user Holds the user data
84- * @param boolean $success True if user was successfully stored in the database
85- * @param string $msg Message
109+ * @param AfterDeleteEvent $event The event instance.
86110 *
87111 * @return void
88112 *
89113 * @since 1.6
90114 */
91- public function onUserAfterDelete ($ user , $ success , $ msg ): void
115+ public function onUserAfterDelete (AfterDeleteEvent $ event ): void
92116 {
117+ $ user = $ event ->getUser ();
118+ $ success = $ event ->getDeletingResult ();
119+
93120 if (!$ success ) {
94121 return ;
95122 }
@@ -147,17 +174,17 @@ public function onUserAfterDelete($user, $success, $msg): void
147174 *
148175 * This method sends a registration email to new users created in the backend.
149176 *
150- * @param array $user Holds the new user data.
151- * @param boolean $isnew True if a new user is stored.
152- * @param boolean $success True if user was successfully stored in the database.
153- * @param string $msg Message.
177+ * @param AfterSaveEvent $event The event instance.
154178 *
155179 * @return void
156180 *
157181 * @since 1.6
158182 */
159- public function onUserAfterSave ($ user , $ isnew , $ success , $ msg ): void
183+ public function onUserAfterSave (AfterSaveEvent $ event ): void
160184 {
185+ $ user = $ event ->getUser ();
186+ $ isnew = $ event ->getIsNew ();
187+
161188 $ mail_to_user = $ this ->params ->get ('mail_to_user ' , 1 );
162189
163190 if (!$ isnew || !$ mail_to_user ) {
@@ -249,27 +276,30 @@ public function onUserAfterSave($user, $isnew, $success, $msg): void
249276 /**
250277 * This method should handle any login logic and report back to the subject
251278 *
252- * @param array $user Holds the user data
253- * @param array $options Array holding options (remember, autoregister, group)
279+ * @param LoginEvent $event The event instance.
254280 *
255- * @return boolean True on success
281+ * @return void
256282 *
257283 * @since 1.5
258284 */
259- public function onUserLogin ($ user , $ options = [] )
285+ public function onUserLogin (LoginEvent $ event )
260286 {
287+ $ user = $ event ->getAuthenticationResponse ();
288+ $ options = $ event ->getOptions ();
261289 $ instance = $ this ->getUser ($ user , $ options );
262290
263291 // If getUser returned an error, then pass it back.
264292 if ($ instance instanceof \Exception) {
265- return false ;
293+ $ event ->addResult (false );
294+ return ;
266295 }
267296
268297 // If the user is blocked, redirect with an error
269298 if ($ instance ->block == 1 ) {
270299 $ this ->getApplication ()->enqueueMessage ($ this ->getApplication ()->getLanguage ()->_ ('JERROR_NOLOGIN_BLOCKED ' ), 'warning ' );
300+ $ event ->addResult (false );
271301
272- return false ;
302+ return ;
273303 }
274304
275305 // Authorise the user based on the group information
@@ -282,8 +312,9 @@ public function onUserLogin($user, $options = [])
282312
283313 if (!$ result ) {
284314 $ this ->getApplication ()->enqueueMessage ($ this ->getApplication ()->getLanguage ()->_ ('JERROR_LOGIN_DENIED ' ), 'warning ' );
315+ $ event ->addResult (false );
285316
286- return false ;
317+ return ;
287318 }
288319
289320 // Mark the user as logged in
@@ -337,30 +368,30 @@ public function onUserLogin($user, $options = [])
337368 true
338369 );
339370 }
340-
341- return true ;
342371 }
343372
344373 /**
345374 * This method should handle any logout logic and report back to the subject
346375 *
347- * @param array $user Holds the user data.
348- * @param array $options Array holding options (client, ...).
376+ * @param LogoutEvent $event The event instance.
349377 *
350- * @return boolean True on success
378+ * @return void
351379 *
352380 * @since 1.5
353381 */
354- public function onUserLogout ($ user , $ options = [] )
382+ public function onUserLogout (LogoutEvent $ event )
355383 {
384+ $ user = $ event ->getParameters ();
385+ $ options = $ event ->getOptions ();
386+
356387 $ my = $ this ->getApplication ()->getIdentity ();
357388 $ session = Factory::getSession ();
358389
359390 $ userid = (int ) $ user ['id ' ];
360391
361392 // Make sure we're a valid user first
362393 if ($ user ['id ' ] === 0 && !$ my ->get ('tmp_user ' )) {
363- return true ;
394+ return ;
364395 }
365396
366397 $ sharedSessions = $ this ->getApplication ()->get ('shared_session ' , '0 ' );
@@ -386,8 +417,6 @@ public function onUserLogout($user, $options = [])
386417 if ($ this ->getApplication ()->isClient ('site ' )) {
387418 $ this ->getApplication ()->getInput ()->cookie ->set ('joomla_user_state ' , '' , 1 , $ this ->getApplication ()->get ('cookie_path ' , '/ ' ), $ this ->getApplication ()->get ('cookie_domain ' , '' ));
388419 }
389-
390- return true ;
391420 }
392421
393422 /**
@@ -399,18 +428,18 @@ public function onUserLogout($user, $options = [])
399428 * logging in, something which would cause the Captive page to appear in the frontend and redirect you to the public
400429 * frontend homepage after successfully passing the Two Step verification process.
401430 *
402- * @param array $options Passed by Joomla. user: a User object; responseType: string, authentication response type .
431+ * @param AfterLoginEvent $event The event instance .
403432 *
404433 * @return void
405434 * @since 4.2.0
406435 */
407- public function onUserAfterLogin (array $ options ): void
436+ public function onUserAfterLogin (AfterLoginEvent $ event ): void
408437 {
409438 if (!($ this ->getApplication ()->isClient ('administrator ' )) && !($ this ->getApplication ()->isClient ('site ' ))) {
410439 return ;
411440 }
412441
413- $ this ->disableMfaOnSilentLogin ($ options );
442+ $ this ->disableMfaOnSilentLogin ($ event -> getOptions () );
414443 }
415444
416445 /**
0 commit comments