Skip to content

Commit

Permalink
Introduce a UserFactoryAware interface (#40337)
Browse files Browse the repository at this point in the history
* Introduce an UserFactoryAware interface

* 0

* Test

* safe

* trigger deprecated message

* protected

* test

* load user correctly

* fix the deprecated message
  • Loading branch information
laoneo authored Apr 19, 2023
1 parent 2911da0 commit 0e57bdd
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Joomla\CMS\Mail\Exception\MailDisabledException;
use Joomla\CMS\Mail\MailTemplate;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\User\UserFactoryAwareInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper;
use Joomla\Utilities\IpHelper;
use PHPMailer\PHPMailer\Exception as phpMailerException;
Expand All @@ -30,8 +32,10 @@
*
* @since 3.9.0
*/
class ActionlogModel extends BaseDatabaseModel
class ActionlogModel extends BaseDatabaseModel implements UserFactoryAwareInterface
{
use UserFactoryAwareTrait;

/**
* Function to add logs to the database
* This method adds a record to #__action_logs contains (message_language_key, message, date, context, user)
Expand All @@ -45,9 +49,13 @@ class ActionlogModel extends BaseDatabaseModel
*
* @since 3.9.0
*/
public function addLog($messages, $messageLanguageKey, $context, $userId = null)
public function addLog($messages, $messageLanguageKey, $context, $userId = 0)
{
$user = Factory::getUser($userId);
if (!is_numeric($userId)) {
@trigger_error(sprintf('User ID must be an integer in %s.', __METHOD__), E_USER_DEPRECATED);
}

$user = $userId ? $this->getUserFactory()->loadUserById($userId) : $this->getCurrentUser();
$db = $this->getDatabase();
$date = Factory::getDate();
$params = ComponentHelper::getComponent('com_actionlogs')->getParams();
Expand Down
13 changes: 6 additions & 7 deletions administrator/components/com_users/src/View/User/HtmlView.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\User\UserFactoryAwareInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Component\Users\Administrator\Helper\Mfa;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -31,8 +31,10 @@
*
* @since 1.5
*/
class HtmlView extends BaseHtmlView
class HtmlView extends BaseHtmlView implements UserFactoryAwareInterface
{
use UserFactoryAwareTrait;

/**
* The Form object
*
Expand Down Expand Up @@ -114,10 +116,7 @@ public function display($tpl = null)
$this->form->setValue('password', null);
$this->form->setValue('password2', null);

/** @var User $userBeingEdited */
$userBeingEdited = Factory::getContainer()
->get(UserFactoryInterface::class)
->loadUserById($this->item->id);
$userBeingEdited = $this->getUserFactory()->loadUserById($this->item->id);

if ($this->item->id > 0 && (int) $userBeingEdited->id == (int) $this->item->id) {
try {
Expand Down
28 changes: 4 additions & 24 deletions libraries/src/Application/IdentityAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Joomla\CMS\Application;

use Joomla\CMS\User\User;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
Expand All @@ -23,6 +23,8 @@
*/
trait IdentityAware
{
use UserFactoryAwareTrait;

/**
* The application identity object.
*
Expand All @@ -31,14 +33,6 @@ trait IdentityAware
*/
protected $identity;

/**
* UserFactoryInterface
*
* @var UserFactoryInterface
* @since 4.0.0
*/
private $userFactory;

/**
* Get the application identity.
*
Expand All @@ -62,22 +56,8 @@ public function getIdentity()
*/
public function loadIdentity(User $identity = null)
{
$this->identity = $identity ?: $this->userFactory->loadUserById(0);
$this->identity = $identity ?: $this->getUserFactory()->loadUserById(0);

return $this;
}

/**
* Set the user factory to use.
*
* @param UserFactoryInterface $userFactory The user factory to use
*
* @return void
*
* @since 4.0.0
*/
public function setUserFactory(UserFactoryInterface $userFactory)
{
$this->userFactory = $userFactory;
}
}
2 changes: 2 additions & 0 deletions libraries/src/Extension/Service/Provider/MVCFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Joomla\CMS\MVC\Factory\ApiMVCFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Router\SiteRouter;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
Expand Down Expand Up @@ -76,6 +77,7 @@ function (Container $container) {
$factory->setDatabase($container->get(DatabaseInterface::class));
$factory->setSiteRouter($container->get(SiteRouter::class));
$factory->setCacheControllerFactory($container->get(CacheControllerFactoryInterface::class));
$factory->setUserFactory($container->get(UserFactoryInterface::class));

return $factory;
}
Expand Down
30 changes: 29 additions & 1 deletion libraries/src/MVC/Factory/MVCFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Joomla\CMS\MVC\Model\ModelInterface;
use Joomla\CMS\Router\SiteRouterAwareInterface;
use Joomla\CMS\Router\SiteRouterAwareTrait;
use Joomla\CMS\User\UserFactoryAwareInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Database\DatabaseAwareInterface;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\DatabaseInterface;
Expand All @@ -35,13 +37,14 @@
*
* @since 3.10.0
*/
class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface, SiteRouterAwareInterface
class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface, SiteRouterAwareInterface, UserFactoryAwareInterface
{
use FormFactoryAwareTrait;
use DispatcherAwareTrait;
use DatabaseAwareTrait;
use SiteRouterAwareTrait;
use CacheControllerFactoryAwareTrait;
use UserFactoryAwareTrait;

/**
* The namespace to create the objects from.
Expand Down Expand Up @@ -95,6 +98,7 @@ public function createController($name, $prefix, array $config, CMSApplicationIn
$this->setDispatcherOnObject($controller);
$this->setRouterOnObject($controller);
$this->setCacheControllerOnObject($controller);
$this->setUserFactoryOnObject($controller);

return $controller;
}
Expand Down Expand Up @@ -140,6 +144,7 @@ public function createModel($name, $prefix = '', array $config = [])
$this->setDispatcherOnObject($model);
$this->setRouterOnObject($model);
$this->setCacheControllerOnObject($model);
$this->setUserFactoryOnObject($model);

if ($model instanceof DatabaseAwareInterface) {
try {
Expand Down Expand Up @@ -196,6 +201,7 @@ public function createView($name, $prefix = '', $type = '', array $config = [])
$this->setDispatcherOnObject($view);
$this->setRouterOnObject($view);
$this->setCacheControllerOnObject($view);
$this->setUserFactoryOnObject($view);

return $view;
}
Expand Down Expand Up @@ -359,4 +365,26 @@ private function setCacheControllerOnObject($object): void
// Ignore it
}
}

/**
* Sets the internal user factory on the given object.
*
* @param object $object The object
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
private function setUserFactoryOnObject($object): void
{
if (!$object instanceof UserFactoryAwareInterface) {
return;
}

try {
$object->setUserFactory($this->getUserFactory());
} catch (\UnexpectedValueException $e) {
// Ignore it
}
}
}
33 changes: 33 additions & 0 deletions libraries/src/User/UserFactoryAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\User;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Interface to be implemented by classes depending on a user factory.
*
* @since __DEPLOY_VERSION__
*/
interface UserFactoryAwareInterface
{
/**
* Set the user factory to use.
*
* @param UserFactoryInterface $factory The user factory to use.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function setUserFactory(UserFactoryInterface $factory): void;
}
61 changes: 61 additions & 0 deletions libraries/src/User/UserFactoryAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\User;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Defines the trait for a UserFactoryInterface Aware Class.
*
* @since __DEPLOY_VERSION__
*/
trait UserFactoryAwareTrait
{
/**
* UserFactoryInterface
*
* @var UserFactoryInterface
* @since __DEPLOY_VERSION__
*/
private $userFactory;

/**
* Get the UserFactoryInterface.
*
* @return UserFactoryInterface
*
* @since __DEPLOY_VERSION__
* @throws \UnexpectedValueException May be thrown if the UserFactory has not been set.
*/
protected function getUserFactory(): UserFactoryInterface
{
if ($this->userFactory) {
return $this->userFactory;
}

throw new \UnexpectedValueException('UserFactory not set in ' . __CLASS__);
}

/**
* Set the user factory to use.
*
* @param UserFactoryInterface $userFactory The user factory to use.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function setUserFactory(UserFactoryInterface $userFactory): void
{
$this->userFactory = $userFactory;
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Libraries/Cms/User/CurrentUserTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Joomla\Tests\Unit\UnitTestCase;

/**
* Test class for \Joomla\CMS\MVC\Model\BaseDatabaseModel
* Test class for \Joomla\CMS\MVC\Model\BaseDatabaseModelUser\CurrentUserTrait
*
* @package Joomla.UnitTest
* @subpackage MVC
Expand Down
Loading

0 comments on commit 0e57bdd

Please sign in to comment.