Skip to content

Commit 55b56ec

Browse files
authored
[5.3] [Events] Use event classes for User plugins (#43427)
* Content Events: ContactCreator * User Events: joomla * User Events: profile * User Events: terms * User Events: token
1 parent 8ea8a08 commit 55b56ec

File tree

5 files changed

+233
-125
lines changed

5 files changed

+233
-125
lines changed

plugins/user/contactcreator/src/Extension/ContactCreator.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
namespace Joomla\Plugin\User\ContactCreator\Extension;
1212

13+
use Joomla\CMS\Event\User\AfterSaveEvent;
1314
use Joomla\CMS\Plugin\CMSPlugin;
1415
use Joomla\Component\Contact\Administrator\Table\ContactTable;
16+
use Joomla\Event\SubscriberInterface;
1517
use Joomla\String\StringHelper;
1618

1719
// phpcs:disable PSR1.Files.SideEffects
@@ -25,24 +27,39 @@
2527
*
2628
* @since 1.6
2729
*/
28-
final class ContactCreator extends CMSPlugin
30+
final class ContactCreator extends CMSPlugin implements SubscriberInterface
2931
{
32+
/**
33+
* Returns an array of events this subscriber will listen to.
34+
*
35+
* @return array
36+
*
37+
* @since __DEPLOY_VERSION__
38+
*/
39+
public static function getSubscribedEvents(): array
40+
{
41+
return [
42+
'onUserAfterSave' => 'onUserAfterSave',
43+
];
44+
}
45+
3046
/**
3147
* Utility method to act on a user after it has been saved.
3248
*
3349
* This method creates a contact for the saved user
3450
*
35-
* @param array $user Holds the new user data.
36-
* @param boolean $isnew True if a new user is stored.
37-
* @param boolean $success True if user was successfully stored in the database.
38-
* @param string $msg Message.
51+
* @param AfterSaveEvent $event The event instance.
3952
*
4053
* @return void
4154
*
4255
* @since 1.6
4356
*/
44-
public function onUserAfterSave($user, $isnew, $success, $msg): void
57+
public function onUserAfterSave(AfterSaveEvent $event): void
4558
{
59+
$user = $event->getUser();
60+
$isnew = $event->getIsNew();
61+
$success = $event->getSavingResult();
62+
4663
// If the user wasn't stored we don't resync
4764
if (!$success) {
4865
return;

plugins/user/joomla/src/Extension/Joomla.php

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
namespace Joomla\Plugin\User\Joomla\Extension;
1212

1313
use 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;
1420
use Joomla\CMS\Factory;
1521
use Joomla\CMS\Language\LanguageFactoryInterface;
1622
use Joomla\CMS\Log\Log;
@@ -23,6 +29,7 @@
2329
use Joomla\Database\DatabaseAwareTrait;
2430
use Joomla\Database\Exception\ExecutionFailureException;
2531
use Joomla\Database\ParameterType;
32+
use Joomla\Event\SubscriberInterface;
2633
use Joomla\Registry\Registry;
2734

2835
// phpcs:disable PSR1.Files.SideEffects
@@ -34,23 +41,44 @@
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

Comments
 (0)