Skip to content

Commit

Permalink
[4][com_actionlog] - log event reset password (joomla#39435)
Browse files Browse the repository at this point in the history
* Update Joomla.php

* event

* Update Joomla.php

* cs

* onUserAfterResetRequest

* onUserAfterResetRequest

* onUserBeforeResetRequest, onUserBeforeResetComplete

---------
  • Loading branch information
alikon authored Mar 7, 2023
1 parent 9ef7ff4 commit 081afed
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 17 deletions.
39 changes: 36 additions & 3 deletions components/com_users/src/Model/ResetModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Joomla\Component\Users\Site\Model;

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Event\AbstractEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\Text;
Expand Down Expand Up @@ -194,6 +195,14 @@ public function processResetComplete($data)
// Get the user object.
$user = User::getInstance($userId);

$event = AbstractEvent::create(
'onUserBeforeResetComplete',
[
'subject' => $user,
]
);
$app->getDispatcher()->dispatch($event->getName(), $event);

// Check for a user and that the tokens match.
if (empty($user) || $user->activation !== $token) {
$this->setError(Text::_('COM_USERS_USER_NOT_FOUND'));
Expand Down Expand Up @@ -236,6 +245,14 @@ public function processResetComplete($data)
$app->setUserState('com_users.reset.token', null);
$app->setUserState('com_users.reset.user', null);

$event = AbstractEvent::create(
'onUserAfterResetComplete',
[
'subject' => $user,
]
);
$app->getDispatcher()->dispatch($event->getName(), $event);

return true;
}

Expand Down Expand Up @@ -430,6 +447,14 @@ public function processResetRequest($data)

$user->activation = $hashedToken;

$event = AbstractEvent::create(
'onUserBeforeResetRequest',
[
'subject' => $user,
]
);
$app->getDispatcher()->dispatch($event->getName(), $event);

// Save the user to the database.
if (!$user->save(true)) {
return new \Exception(Text::sprintf('COM_USERS_USER_SAVE_FAILED', $user->getError()), 500);
Expand Down Expand Up @@ -459,7 +484,7 @@ public function processResetRequest($data)

$return = false;
} catch (\RuntimeException $exception) {
Factory::getApplication()->enqueueMessage(Text::_($exception->errorMessage()), 'warning');
$app->enqueueMessage(Text::_($exception->errorMessage()), 'warning');

$return = false;
}
Expand All @@ -468,9 +493,17 @@ public function processResetRequest($data)
// Check for an error.
if ($return !== true) {
return new \Exception(Text::_('COM_USERS_MAIL_FAILED'), 500);
} else {
return true;
}

$event = AbstractEvent::create(
'onUserAfterResetRequest',
[
'subject' => $user,
]
);
$app->getDispatcher()->dispatch($event->getName(), $event);

return true;
}

/**
Expand Down
90 changes: 76 additions & 14 deletions plugins/actionlog/joomla/src/Extension/Joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,32 +574,28 @@ public function onExtensionAfterDelete($context, $table): void
public function onUserAfterSave($user, $isnew, $success, $msg): void
{
$context = $this->getApplication()->input->get('option');
$task = $this->getApplication()->input->post->get('task');
$task = $this->getApplication()->input->get('task');

if (!$this->checkLoggable($context)) {
return;
}

if ($task === 'request') {
return;
}

if ($task === 'complete') {
return;
}

$jUser = Factory::getUser();

if (!$jUser->id) {
$messageLanguageKey = 'PLG_ACTIONLOG_JOOMLA_USER_REGISTERED';
$action = 'register';

// Reset request
if ($task === 'reset.request') {
$messageLanguageKey = 'PLG_ACTIONLOG_JOOMLA_USER_RESET_REQUEST';
$action = 'resetrequest';
}

// Reset complete
if ($task === 'reset.complete') {
$messageLanguageKey = 'PLG_ACTIONLOG_JOOMLA_USER_RESET_COMPLETE';
$action = 'resetcomplete';
}

// Registration Activation
if ($task === 'registration.activate') {
if ($task === 'activate') {
$messageLanguageKey = 'PLG_ACTIONLOG_JOOMLA_USER_REGISTRATION_ACTIVATE';
$action = 'activaterequest';
}
Expand Down Expand Up @@ -1127,4 +1123,70 @@ private function getActionLogParams($context): ?stdClass

return $component->getMVCFactory()->createModel('ActionlogConfig', 'Administrator')->getLogContentTypeParams($context);
}

/**
* On after Reset password request
*
* Method is called after user request to reset their password.
*
* @param array $user Holds the user data.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onUserAfterResetRequest($user)
{
$context = $this->getApplication()->input->get('option');

if (!$this->checkLoggable($context)) {
return;
}

$message = array(
'action' => 'reset',
'type' => 'PLG_ACTIONLOG_JOOMLA_TYPE_USER',
'id' => $user->id,
'title' => $user->name,
'itemlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
'userid' => $user->id,
'username' => $user->name,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
);

$this->addLog(array($message), 'PLG_ACTIONLOG_JOOMLA_USER_RESET_REQUEST', $context, $user->id);
}

/**
* On after Completed reset request
*
* Method is called after user complete the reset of their password.
*
* @param array $user Holds the user data.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onUserAfterResetComplete($user)
{
$context = $this->getApplication()->input->get('option');

if (!$this->checkLoggable($context)) {
return;
}

$message = array(
'action' => 'complete',
'type' => 'PLG_ACTIONLOG_JOOMLA_TYPE_USER',
'id' => $user->id,
'title' => $user->name,
'itemlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
'userid' => $user->id,
'username' => $user->name,
'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
);

$this->addLog(array($message), 'PLG_ACTIONLOG_JOOMLA_USER_RESET_COMPLETE', $context, $user->id);
}
}

0 comments on commit 081afed

Please sign in to comment.