diff --git a/libraries/src/Application/IdentityAware.php b/libraries/src/Application/IdentityAware.php index 1fe220a0a0f44..7281b5833e348 100644 --- a/libraries/src/Application/IdentityAware.php +++ b/libraries/src/Application/IdentityAware.php @@ -11,6 +11,7 @@ defined('JPATH_PLATFORM') or die; use Joomla\CMS\User\User; +use Joomla\CMS\User\UserFactoryInterface; /** * Trait for application classes which are identity (user) aware @@ -27,6 +28,14 @@ trait IdentityAware */ protected $identity; + /** + * UserFactoryInterface + * + * @var UserFactoryInterface + * @since 4.0.0 + */ + private $userFactory; + /** * Get the application identity. * @@ -50,8 +59,22 @@ public function getIdentity() */ public function loadIdentity(User $identity = null) { - $this->identity = $identity ?: User::getInstance(); + $this->identity = $identity ?: $this->userFactory->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; + } } diff --git a/libraries/src/Factory.php b/libraries/src/Factory.php index 0f355629ea649..0ca96aa4c1ee5 100644 --- a/libraries/src/Factory.php +++ b/libraries/src/Factory.php @@ -300,11 +300,21 @@ public static function getDocument() * * @return User object * - * @see User - * @since 1.7.0 + * @see User + * @since 1.7.0 + * @deprecated 5.0 Load the user service from the dependency injection container or via $app->getIdentity() */ public static function getUser($id = null) { + @trigger_error( + sprintf( + '%1$s() is deprecated. Load the user from the dependency injection container or via %2$s::getApplication()->getIdentity().', + __METHOD__, + __CLASS__ + ), + E_USER_DEPRECATED + ); + $instance = self::getApplication()->getSession()->get('user'); if (is_null($id)) @@ -560,7 +570,8 @@ protected static function createContainer(): Container ->registerServiceProvider(new \Joomla\CMS\Service\Provider\Session) ->registerServiceProvider(new \Joomla\CMS\Service\Provider\Toolbar) ->registerServiceProvider(new \Joomla\CMS\Service\Provider\WebAssetRegistry) - ->registerServiceProvider(new \Joomla\CMS\Service\Provider\ApiRouter); + ->registerServiceProvider(new \Joomla\CMS\Service\Provider\ApiRouter) + ->registerServiceProvider(new \Joomla\CMS\Service\Provider\User); return $container; } diff --git a/libraries/src/Service/Provider/Application.php b/libraries/src/Service/Provider/Application.php index 842b5dcec417e..b68d4cbe9880c 100644 --- a/libraries/src/Service/Provider/Application.php +++ b/libraries/src/Service/Provider/Application.php @@ -20,6 +20,7 @@ use Joomla\CMS\Console\SessionGcCommand; use Joomla\CMS\Console\SessionMetadataGcCommand; use Joomla\CMS\Factory; +use Joomla\CMS\User\UserFactoryInterface; use Joomla\Console\Application as BaseConsoleApplication; use Joomla\Console\Loader\LoaderInterface; use Joomla\DI\Container; @@ -62,6 +63,7 @@ function (Container $container) $app->setDispatcher($container->get(DispatcherInterface::class)); $app->setLogger($container->get(LoggerInterface::class)); $app->setSession($container->get(SessionInterface::class)); + $app->setUserFactory($container->get(UserFactoryInterface::class)); return $app; }, @@ -84,6 +86,7 @@ function (Container $container) $app->setDispatcher($container->get(DispatcherInterface::class)); $app->setLogger($container->get(LoggerInterface::class)); $app->setSession($container->get(SessionInterface::class)); + $app->setUserFactory($container->get(UserFactoryInterface::class)); return $app; }, @@ -108,6 +111,7 @@ function (Container $container) $app->setCommandLoader($container->get(LoaderInterface::class)); $app->setLogger($container->get(LoggerInterface::class)); $app->setSession($container->get(SessionInterface::class)); + $app->setUserFactory($container->get(UserFactoryInterface::class)); return $app; }, diff --git a/libraries/src/Service/Provider/User.php b/libraries/src/Service/Provider/User.php new file mode 100644 index 0000000000000..c172a734a92d6 --- /dev/null +++ b/libraries/src/Service/Provider/User.php @@ -0,0 +1,48 @@ +alias('user.factory', UserFactoryInterface::class) + ->alias(UserFactory::class, UserFactoryInterface::class) + ->share( + UserFactoryInterface::class, + function (Container $container) + { + return new UserFactory($container->get(DatabaseInterface::class)); + }, + true + ); + } +} diff --git a/libraries/src/User/User.php b/libraries/src/User/User.php index 0d7ae5ac473ad..e4992ae99ddec 100644 --- a/libraries/src/User/User.php +++ b/libraries/src/User/User.php @@ -251,18 +251,24 @@ public function __construct($identifier = 0) * * @return User The User object. * - * @since 1.7.0 + * @since 1.7.0 + * @deprecated 5.0 Load the user service from the dependency injection container or via $app->getIdentity() */ public static function getInstance($identifier = 0) { + @trigger_error( + sprintf( + '%1$s() is deprecated. Load the user from the dependency injection container or via %2$s::getApplication()->getIdentity().', + __METHOD__, + __CLASS__ + ), + E_USER_DEPRECATED + ); + // Find the user id if (!is_numeric($identifier)) { - if (!$id = UserHelper::getUserId($identifier)) - { - // If the $identifier doesn't match with any id, just return an empty User. - return new static; - } + return Factory::getContainer()->get(UserFactoryInterface::class)->loadUserByUsername($identifier); } else { @@ -273,13 +279,13 @@ public static function getInstance($identifier = 0) // Note: don't cache this user because it'll have a new ID on save! if ($id === 0) { - return new static; + return Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById($id); } // Check if the user ID is already cached. if (empty(self::$instances[$id])) { - self::$instances[$id] = new static($id); + self::$instances[$id] = Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById($id); } return self::$instances[$id]; diff --git a/libraries/src/User/UserFactory.php b/libraries/src/User/UserFactory.php new file mode 100644 index 0000000000000..6cb9014a3d880 --- /dev/null +++ b/libraries/src/User/UserFactory.php @@ -0,0 +1,74 @@ +db = $db; + } + + /** + * Method to get an instance of a user for the given id. + * + * @param int $id The id + * + * @return User + * + * @since __DEPLOY_VERSION__ + */ + public function loadUserById(int $id): User + { + return new User($id); + } + + /** + * Method to get an instance of a user for the given username. + * + * @param string $username The username + * + * @return User + * + * @since __DEPLOY_VERSION__ + */ + public function loadUserByUsername(string $username): User + { + // Initialise some variables + $query = $this->db->getQuery(true) + ->select($this->db->quoteName('id')) + ->from($this->db->quoteName('#__users')) + ->where($this->db->quoteName('username') . ' = ' . $this->db->quote($username)); + $query->setLimit(1, 0); + $this->db->setQuery($query); + + return $this->loadUserById((int) $this->db->loadResult()); + } +} diff --git a/libraries/src/User/UserFactoryInterface.php b/libraries/src/User/UserFactoryInterface.php new file mode 100644 index 0000000000000..c06f1b1a8c993 --- /dev/null +++ b/libraries/src/User/UserFactoryInterface.php @@ -0,0 +1,41 @@ +