From a27611c0cb73aaa6235165127e75cda7fd3bc654 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Sun, 15 May 2022 19:41:35 +0200 Subject: [PATCH 01/13] Convert basic api auth plugin to services --- plugins/api-authentication/basic/basic.xml | 4 +- .../basic/services/provider.php | 45 +++++++++++++++++++ .../basic/{basic.php => src/Basic.php} | 27 +++++------ 3 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 plugins/api-authentication/basic/services/provider.php rename plugins/api-authentication/basic/{basic.php => src/Basic.php} (82%) diff --git a/plugins/api-authentication/basic/basic.xml b/plugins/api-authentication/basic/basic.xml index 4e3c90c7fb172..18fd042e11243 100644 --- a/plugins/api-authentication/basic/basic.xml +++ b/plugins/api-authentication/basic/basic.xml @@ -9,8 +9,10 @@ www.joomla.org 4.0.0 PLG_API-AUTHENTICATION_BASIC_XML_DESCRIPTION + Joomla\Plugin\ApiAuthentication\Basic - basic.php + services + src language/en-GB/plg_authentication_api_basic.ini diff --git a/plugins/api-authentication/basic/services/provider.php b/plugins/api-authentication/basic/services/provider.php new file mode 100644 index 0000000000000..cf2badc93cf5c --- /dev/null +++ b/plugins/api-authentication/basic/services/provider.php @@ -0,0 +1,45 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\Extension\PluginInterface; +use Joomla\CMS\Plugin\PluginHelper; +use Joomla\Database\DatabaseInterface; +use Joomla\DI\Container; +use Joomla\DI\ServiceProviderInterface; +use Joomla\Event\DispatcherInterface; +use Joomla\Plugin\ApiAuthentication\Basic\Basic; + +return new class implements ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container) + { + $container->set( + PluginInterface::class, + function (Container $container) + { + $dispatcher = $container->get(DispatcherInterface::class); + + $plugin = new Basic($dispatcher, (array) PluginHelper::getPlugin('system', 'cache')); + $plugin->setDatabase($container->get(DatabaseInterface::class)); + + return $plugin; + } + ); + } +}; diff --git a/plugins/api-authentication/basic/basic.php b/plugins/api-authentication/basic/src/Basic.php similarity index 82% rename from plugins/api-authentication/basic/basic.php rename to plugins/api-authentication/basic/src/Basic.php index 44cd2ad57292e..12dc8ec371157 100644 --- a/plugins/api-authentication/basic/basic.php +++ b/plugins/api-authentication/basic/src/Basic.php @@ -7,36 +7,33 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ +namespace Joomla\Plugin\ApiAuthentication\Basic; + defined('_JEXEC') or die; +use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\Authentication\Authentication; -use Joomla\CMS\Language\Text; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\User\User; use Joomla\CMS\User\UserHelper; +use Joomla\Database\DatabaseAwareTrait; /** * Joomla Authentication plugin * * @since 4.0.0 */ -class PlgApiAuthenticationBasic extends CMSPlugin +final class Basic extends CMSPlugin { - /** - * The application object - * - * @var \Joomla\CMS\Application\CMSApplicationInterface - * @since 4.0.0 - */ - protected $app; + use DatabaseAwareTrait; /** * The application object * - * @var \Joomla\Database\DatabaseInterface + * @var CMSApplicationInterface * @since 4.0.0 */ - protected $db; + protected $app; /** * This method should handle any authentication and report back to the subject @@ -59,12 +56,12 @@ public function onUserAuthenticate($credentials, $options, &$response) if ($password === '') { $response->status = Authentication::STATUS_FAILURE; - $response->error_message = Text::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED'); + $response->error_message = $this->app->getLanguage()->_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED'); return; } - $db = $this->db; + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName(['id', 'password'])) ->from($db->quoteName('#__users')) @@ -103,7 +100,7 @@ public function onUserAuthenticate($credentials, $options, &$response) { // Invalid password $response->status = Authentication::STATUS_FAILURE; - $response->error_message = Text::_('JGLOBAL_AUTH_INVALID_PASS'); + $response->error_message = $this->app->getLanguage()->_('JGLOBAL_AUTH_INVALID_PASS'); } } else @@ -114,7 +111,7 @@ public function onUserAuthenticate($credentials, $options, &$response) // Invalid user $response->status = Authentication::STATUS_FAILURE; - $response->error_message = Text::_('JGLOBAL_AUTH_NO_USER'); + $response->error_message = $this->app->getLanguage()->_('JGLOBAL_AUTH_NO_USER'); } } } From fa6e964ed79a90c41ef33521493671144ed290a4 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Sun, 15 May 2022 19:58:29 +0200 Subject: [PATCH 02/13] Load user by factory --- .../basic/services/provider.php | 9 ++++-- .../api-authentication/basic/src/Basic.php | 30 +++++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/plugins/api-authentication/basic/services/provider.php b/plugins/api-authentication/basic/services/provider.php index cf2badc93cf5c..128d44ade70a8 100644 --- a/plugins/api-authentication/basic/services/provider.php +++ b/plugins/api-authentication/basic/services/provider.php @@ -11,6 +11,7 @@ use Joomla\CMS\Extension\PluginInterface; use Joomla\CMS\Plugin\PluginHelper; +use Joomla\CMS\User\UserFactoryInterface; use Joomla\Database\DatabaseInterface; use Joomla\DI\Container; use Joomla\DI\ServiceProviderInterface; @@ -33,9 +34,11 @@ public function register(Container $container) PluginInterface::class, function (Container $container) { - $dispatcher = $container->get(DispatcherInterface::class); - - $plugin = new Basic($dispatcher, (array) PluginHelper::getPlugin('system', 'cache')); + $plugin = new Basic( + $container->get(DispatcherInterface::class), + (array) PluginHelper::getPlugin('system', 'cache'), + $container->get(UserFactoryInterface::class) + ); $plugin->setDatabase($container->get(DatabaseInterface::class)); return $plugin; diff --git a/plugins/api-authentication/basic/src/Basic.php b/plugins/api-authentication/basic/src/Basic.php index 12dc8ec371157..e589beecb12db 100644 --- a/plugins/api-authentication/basic/src/Basic.php +++ b/plugins/api-authentication/basic/src/Basic.php @@ -14,9 +14,10 @@ use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\Authentication\Authentication; use Joomla\CMS\Plugin\CMSPlugin; -use Joomla\CMS\User\User; +use Joomla\CMS\User\UserFactoryInterface; use Joomla\CMS\User\UserHelper; use Joomla\Database\DatabaseAwareTrait; +use Joomla\Event\DispatcherInterface; /** * Joomla Authentication plugin @@ -35,6 +36,31 @@ final class Basic extends CMSPlugin */ protected $app; + /** + * The user factory + * + * @var UserFactoryInterface + * + * @since __DEPLOY_VERSION__ + */ + protected $userFactory; + + /** + * Constructor. + * + * @param DispatcherInterface $dispatcher The dispatcher + * @param array $config An optional associative array of configuration settings + * @param UserFactoryInterface $userFactory The suer factory + * + * @since __DEPLOY_VERSION__ + */ + public function __construct(DispatcherInterface $dispatcher, array $config, UserFactoryInterface $userFactory) + { + parent::__construct($dispatcher, $config); + + $this->userFactory = $userFactory; + } + /** * This method should handle any authentication and report back to the subject * @@ -78,7 +104,7 @@ public function onUserAuthenticate($credentials, $options, &$response) if ($match === true) { // Bring this in line with the rest of the system - $user = User::getInstance($result->id); + $user = $this->userFactory->loadUserById($result->id); $response->email = $user->email; $response->fullname = $user->name; $response->username = $username; From bf974cccd1c64a757596b4168fc5185999b95d53 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Sun, 15 May 2022 19:59:26 +0200 Subject: [PATCH 03/13] spelling --- plugins/api-authentication/basic/src/Basic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/api-authentication/basic/src/Basic.php b/plugins/api-authentication/basic/src/Basic.php index e589beecb12db..23f53d5df80ca 100644 --- a/plugins/api-authentication/basic/src/Basic.php +++ b/plugins/api-authentication/basic/src/Basic.php @@ -50,7 +50,7 @@ final class Basic extends CMSPlugin * * @param DispatcherInterface $dispatcher The dispatcher * @param array $config An optional associative array of configuration settings - * @param UserFactoryInterface $userFactory The suer factory + * @param UserFactoryInterface $userFactory The user factory * * @since __DEPLOY_VERSION__ */ From b56709ba54ac2ccf069c126dd2cd607a9d97f11a Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Sun, 15 May 2022 20:00:08 +0200 Subject: [PATCH 04/13] visibility --- plugins/api-authentication/basic/src/Basic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/api-authentication/basic/src/Basic.php b/plugins/api-authentication/basic/src/Basic.php index 23f53d5df80ca..8e9083bfd6b7f 100644 --- a/plugins/api-authentication/basic/src/Basic.php +++ b/plugins/api-authentication/basic/src/Basic.php @@ -43,7 +43,7 @@ final class Basic extends CMSPlugin * * @since __DEPLOY_VERSION__ */ - protected $userFactory; + private $userFactory; /** * Constructor. From 704cbf03d89f08e319c85ee1dcf096b58dc532f9 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Mon, 16 May 2022 13:27:19 +0200 Subject: [PATCH 05/13] Update plugins/api-authentication/basic/basic.xml Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com> --- plugins/api-authentication/basic/basic.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/api-authentication/basic/basic.xml b/plugins/api-authentication/basic/basic.xml index 18fd042e11243..9edb6d9100572 100644 --- a/plugins/api-authentication/basic/basic.xml +++ b/plugins/api-authentication/basic/basic.xml @@ -11,7 +11,7 @@ PLG_API-AUTHENTICATION_BASIC_XML_DESCRIPTION Joomla\Plugin\ApiAuthentication\Basic - services + services src From 63b9cde9dcf836d851e1bcacb0909785de59ae2f Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 13:46:30 +0200 Subject: [PATCH 06/13] Update plugins/api-authentication/basic/services/provider.php Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com> --- plugins/api-authentication/basic/services/provider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/api-authentication/basic/services/provider.php b/plugins/api-authentication/basic/services/provider.php index 128d44ade70a8..994d13e195f80 100644 --- a/plugins/api-authentication/basic/services/provider.php +++ b/plugins/api-authentication/basic/services/provider.php @@ -36,7 +36,7 @@ function (Container $container) { $plugin = new Basic( $container->get(DispatcherInterface::class), - (array) PluginHelper::getPlugin('system', 'cache'), + (array) PluginHelper::getPlugin('api-authentication', 'basic'), $container->get(UserFactoryInterface::class) ); $plugin->setDatabase($container->get(DatabaseInterface::class)); From 07fb9bad28c3e77a45b3bf802c7442baded80b0a Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 13:55:50 +0200 Subject: [PATCH 07/13] lang files --- plugins/api-authentication/basic/basic.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/api-authentication/basic/basic.xml b/plugins/api-authentication/basic/basic.xml index 18fd042e11243..d113aa8ab235f 100644 --- a/plugins/api-authentication/basic/basic.xml +++ b/plugins/api-authentication/basic/basic.xml @@ -15,7 +15,7 @@ src - language/en-GB/plg_authentication_api_basic.ini - language/en-GB/plg_authentication_api_basic.sys.ini + language/en-GB/plg_authentication-api_basic.ini + language/en-GB/plg_authentication-api_basic.sys.ini From d731b40dcd1de825322f9146c4cae0ddce35ee6c Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 14:08:33 +0200 Subject: [PATCH 08/13] Update plugins/api-authentication/basic/basic.xml Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com> --- plugins/api-authentication/basic/basic.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/api-authentication/basic/basic.xml b/plugins/api-authentication/basic/basic.xml index f101794f20a8d..4bc18acc159aa 100644 --- a/plugins/api-authentication/basic/basic.xml +++ b/plugins/api-authentication/basic/basic.xml @@ -15,7 +15,7 @@ src - language/en-GB/plg_authentication-api_basic.ini - language/en-GB/plg_authentication-api_basic.sys.ini + language/en-GB/plg_api-authentication_basic.ini + language/en-GB/plg_api-authentication_basic.sys.ini From 2b9de71b07a470220b4ba970823a178db61eb123 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 14:09:47 +0200 Subject: [PATCH 09/13] Update plugins/api-authentication/basic/services/provider.php Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com> --- plugins/api-authentication/basic/services/provider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/api-authentication/basic/services/provider.php b/plugins/api-authentication/basic/services/provider.php index 994d13e195f80..0e3c520a632fd 100644 --- a/plugins/api-authentication/basic/services/provider.php +++ b/plugins/api-authentication/basic/services/provider.php @@ -1,7 +1,7 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt From 85ec3fb40dd720442dec075181ef0ab8ea8056d2 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 14:10:16 +0200 Subject: [PATCH 10/13] doc --- plugins/api-authentication/basic/src/Basic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/api-authentication/basic/src/Basic.php b/plugins/api-authentication/basic/src/Basic.php index 8e9083bfd6b7f..3ff7f71f522a2 100644 --- a/plugins/api-authentication/basic/src/Basic.php +++ b/plugins/api-authentication/basic/src/Basic.php @@ -1,7 +1,7 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt From 84bee349af5c516b3b6a01bfb29037415201d147 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 14:23:47 +0200 Subject: [PATCH 11/13] Update plugins/api-authentication/basic/services/provider.php Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com> --- plugins/api-authentication/basic/services/provider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/api-authentication/basic/services/provider.php b/plugins/api-authentication/basic/services/provider.php index 0e3c520a632fd..ec8efccbe2603 100644 --- a/plugins/api-authentication/basic/services/provider.php +++ b/plugins/api-authentication/basic/services/provider.php @@ -18,7 +18,8 @@ use Joomla\Event\DispatcherInterface; use Joomla\Plugin\ApiAuthentication\Basic\Basic; -return new class implements ServiceProviderInterface { +return new class implements ServiceProviderInterface +{ /** * Registers the service provider with a DI container. * From e98a12e4c48014ddb04c59369584a39510614651 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 14:23:56 +0200 Subject: [PATCH 12/13] Update plugins/api-authentication/basic/src/Basic.php Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com> --- plugins/api-authentication/basic/src/Basic.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/api-authentication/basic/src/Basic.php b/plugins/api-authentication/basic/src/Basic.php index 3ff7f71f522a2..18ed21e4b1b8c 100644 --- a/plugins/api-authentication/basic/src/Basic.php +++ b/plugins/api-authentication/basic/src/Basic.php @@ -40,7 +40,6 @@ final class Basic extends CMSPlugin * The user factory * * @var UserFactoryInterface - * * @since __DEPLOY_VERSION__ */ private $userFactory; From f29921b8e26d2c47d31bc92c0508a1abfff7d329 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 17 May 2022 14:25:21 +0200 Subject: [PATCH 13/13] Extension ns --- plugins/api-authentication/basic/services/provider.php | 2 +- plugins/api-authentication/basic/src/{ => Extension}/Basic.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename plugins/api-authentication/basic/src/{ => Extension}/Basic.php (98%) diff --git a/plugins/api-authentication/basic/services/provider.php b/plugins/api-authentication/basic/services/provider.php index 0e3c520a632fd..3dc3144c98eb5 100644 --- a/plugins/api-authentication/basic/services/provider.php +++ b/plugins/api-authentication/basic/services/provider.php @@ -16,7 +16,7 @@ use Joomla\DI\Container; use Joomla\DI\ServiceProviderInterface; use Joomla\Event\DispatcherInterface; -use Joomla\Plugin\ApiAuthentication\Basic\Basic; +use Joomla\Plugin\ApiAuthentication\Basic\Extension\Basic; return new class implements ServiceProviderInterface { /** diff --git a/plugins/api-authentication/basic/src/Basic.php b/plugins/api-authentication/basic/src/Extension/Basic.php similarity index 98% rename from plugins/api-authentication/basic/src/Basic.php rename to plugins/api-authentication/basic/src/Extension/Basic.php index 3ff7f71f522a2..173c7b428fab6 100644 --- a/plugins/api-authentication/basic/src/Basic.php +++ b/plugins/api-authentication/basic/src/Extension/Basic.php @@ -7,7 +7,7 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ -namespace Joomla\Plugin\ApiAuthentication\Basic; +namespace Joomla\Plugin\ApiAuthentication\Basic\Extension; defined('_JEXEC') or die;