Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions plugins/user/contactcreator/src/Extension/ContactCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

namespace Joomla\Plugin\User\ContactCreator\Extension;

use Joomla\CMS\Event\User\AfterSaveEvent;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Contact\Administrator\Table\ContactTable;
use Joomla\Event\SubscriberInterface;
use Joomla\String\StringHelper;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -25,24 +27,39 @@
*
* @since 1.6
*/
final class ContactCreator extends CMSPlugin
final class ContactCreator extends CMSPlugin implements SubscriberInterface
{
/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onUserAfterSave' => 'onUserAfterSave',
];
}

/**
* Utility method to act on a user after it has been saved.
*
* This method creates a contact for the saved user
*
* @param array $user Holds the new user data.
* @param boolean $isnew True if a new user is stored.
* @param boolean $success True if user was successfully stored in the database.
* @param string $msg Message.
* @param AfterSaveEvent $event The event instance.
*
* @return void
*
* @since 1.6
*/
public function onUserAfterSave($user, $isnew, $success, $msg): void
public function onUserAfterSave(AfterSaveEvent $event): void
{
$user = $event->getUser();
$isnew = $event->getIsNew();
$success = $event->getSavingResult();

// If the user wasn't stored we don't resync
if (!$success) {
return;
Expand Down
99 changes: 64 additions & 35 deletions plugins/user/joomla/src/Extension/Joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
namespace Joomla\Plugin\User\Joomla\Extension;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\Model\PrepareFormEvent;
use Joomla\CMS\Event\User\AfterDeleteEvent;
use Joomla\CMS\Event\User\AfterLoginEvent;
use Joomla\CMS\Event\User\AfterSaveEvent;
use Joomla\CMS\Event\User\LoginEvent;
use Joomla\CMS\Event\User\LogoutEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\LanguageFactoryInterface;
use Joomla\CMS\Log\Log;
Expand All @@ -23,6 +29,7 @@
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\Exception\ExecutionFailureException;
use Joomla\Database\ParameterType;
use Joomla\Event\SubscriberInterface;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -34,23 +41,44 @@
*
* @since 1.5
*/
final class Joomla extends CMSPlugin
final class Joomla extends CMSPlugin implements SubscriberInterface
{
use DatabaseAwareTrait;
use UserFactoryAwareTrait;

/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onContentPrepareForm' => 'onContentPrepareForm',
'onUserAfterDelete' => 'onUserAfterDelete',
'onUserAfterSave' => 'onUserAfterSave',
'onUserLogin' => 'onUserLogin',
'onUserLogout' => 'onUserLogout',
'onUserAfterLogin' => 'onUserAfterLogin',
];
}

/**
* Set as required the passwords fields when mail to user is set to No
*
* @param \Joomla\CMS\Form\Form $form The form to be altered.
* @param mixed $data The associated data for the form.
* @param PrepareFormEvent $event The event instance.
*
* @return boolean
* @return void
*
* @since 4.0.0
*/
public function onContentPrepareForm($form, $data)
public function onContentPrepareForm(PrepareFormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();

// Check we are manipulating a valid user form before modifying it.
$name = $form->getName();

Expand All @@ -71,25 +99,24 @@ public function onContentPrepareForm($form, $data)
$form->setFieldAttribute('password2', 'required', 'true');
}
}

return true;
}

/**
* Remove all sessions for the user name
*
* Method is called after user data is deleted from the database
*
* @param array $user Holds the user data
* @param boolean $success True if user was successfully stored in the database
* @param string $msg Message
* @param AfterDeleteEvent $event The event instance.
*
* @return void
*
* @since 1.6
*/
public function onUserAfterDelete($user, $success, $msg): void
public function onUserAfterDelete(AfterDeleteEvent $event): void
{
$user = $event->getUser();
$success = $event->getDeletingResult();

if (!$success) {
return;
}
Expand Down Expand Up @@ -147,17 +174,17 @@ public function onUserAfterDelete($user, $success, $msg): void
*
* This method sends a registration email to new users created in the backend.
*
* @param array $user Holds the new user data.
* @param boolean $isnew True if a new user is stored.
* @param boolean $success True if user was successfully stored in the database.
* @param string $msg Message.
* @param AfterSaveEvent $event The event instance.
*
* @return void
*
* @since 1.6
*/
public function onUserAfterSave($user, $isnew, $success, $msg): void
public function onUserAfterSave(AfterSaveEvent $event): void
{
$user = $event->getUser();
$isnew = $event->getIsNew();

$mail_to_user = $this->params->get('mail_to_user', 1);

if (!$isnew || !$mail_to_user) {
Expand Down Expand Up @@ -249,27 +276,30 @@ public function onUserAfterSave($user, $isnew, $success, $msg): void
/**
* This method should handle any login logic and report back to the subject
*
* @param array $user Holds the user data
* @param array $options Array holding options (remember, autoregister, group)
* @param LoginEvent $event The event instance.
*
* @return boolean True on success
* @return void
*
* @since 1.5
*/
public function onUserLogin($user, $options = [])
public function onUserLogin(LoginEvent $event)
{
$user = $event->getAuthenticationResponse();
$options = $event->getOptions();
$instance = $this->getUser($user, $options);

// If getUser returned an error, then pass it back.
if ($instance instanceof \Exception) {
return false;
$event->addResult(false);
return;
}

// If the user is blocked, redirect with an error
if ($instance->block == 1) {
$this->getApplication()->enqueueMessage($this->getApplication()->getLanguage()->_('JERROR_NOLOGIN_BLOCKED'), 'warning');
$event->addResult(false);

return false;
return;
}

// Authorise the user based on the group information
Expand All @@ -282,8 +312,9 @@ public function onUserLogin($user, $options = [])

if (!$result) {
$this->getApplication()->enqueueMessage($this->getApplication()->getLanguage()->_('JERROR_LOGIN_DENIED'), 'warning');
$event->addResult(false);

return false;
return;
}

// Mark the user as logged in
Expand Down Expand Up @@ -337,30 +368,30 @@ public function onUserLogin($user, $options = [])
true
);
}

return true;
}

/**
* This method should handle any logout logic and report back to the subject
*
* @param array $user Holds the user data.
* @param array $options Array holding options (client, ...).
* @param LogoutEvent $event The event instance.
*
* @return boolean True on success
* @return void
*
* @since 1.5
*/
public function onUserLogout($user, $options = [])
public function onUserLogout(LogoutEvent $event)
{
$user = $event->getParameters();
$options = $event->getOptions();

$my = $this->getApplication()->getIdentity();
$session = Factory::getSession();

$userid = (int) $user['id'];

// Make sure we're a valid user first
if ($user['id'] === 0 && !$my->get('tmp_user')) {
return true;
return;
}

$sharedSessions = $this->getApplication()->get('shared_session', '0');
Expand All @@ -386,8 +417,6 @@ public function onUserLogout($user, $options = [])
if ($this->getApplication()->isClient('site')) {
$this->getApplication()->getInput()->cookie->set('joomla_user_state', '', 1, $this->getApplication()->get('cookie_path', '/'), $this->getApplication()->get('cookie_domain', ''));
}

return true;
}

/**
Expand All @@ -399,18 +428,18 @@ public function onUserLogout($user, $options = [])
* logging in, something which would cause the Captive page to appear in the frontend and redirect you to the public
* frontend homepage after successfully passing the Two Step verification process.
*
* @param array $options Passed by Joomla. user: a User object; responseType: string, authentication response type.
* @param AfterLoginEvent $event The event instance.
*
* @return void
* @since 4.2.0
*/
public function onUserAfterLogin(array $options): void
public function onUserAfterLogin(AfterLoginEvent $event): void
{
if (!($this->getApplication()->isClient('administrator')) && !($this->getApplication()->isClient('site'))) {
return;
}

$this->disableMfaOnSilentLogin($options);
$this->disableMfaOnSilentLogin($event->getOptions());
}

/**
Expand Down
Loading