Skip to content

Commit 8e559f9

Browse files
authored
Converts the authentication plugins to service providers (#39624)
1 parent 174a5cb commit 8e559f9

File tree

10 files changed

+291
-141
lines changed

10 files changed

+291
-141
lines changed

plugins/authentication/cookie/cookie.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
<authorUrl>www.joomla.org</authorUrl>
1010
<version>3.0.0</version>
1111
<description>PLG_AUTHENTICATION_COOKIE_XML_DESCRIPTION</description>
12+
<namespace path="src">Joomla\Plugin\Authentication\Cookie</namespace>
1213
<files>
13-
<filename plugin="cookie">cookie.php</filename>
14+
<folder plugin="cookie">services</folder>
15+
<folder>src</folder>
1416
</files>
1517
<languages>
1618
<language tag="en-GB">language/en-GB/plg_authentication_cookie.ini</language>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Plugin
5+
* @subpackage Authentication.cookie
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\PluginInterface;
14+
use Joomla\CMS\Factory;
15+
use Joomla\CMS\Plugin\PluginHelper;
16+
use Joomla\Database\DatabaseInterface;
17+
use Joomla\DI\Container;
18+
use Joomla\DI\ServiceProviderInterface;
19+
use Joomla\Event\DispatcherInterface;
20+
use Joomla\Plugin\Authentication\Cookie\Extension\Cookie;
21+
22+
return new class implements ServiceProviderInterface
23+
{
24+
/**
25+
* Registers the service provider with a DI container.
26+
*
27+
* @param Container $container The DI container.
28+
*
29+
* @return void
30+
*
31+
* @since __DEPLOY_VERSION__
32+
*/
33+
public function register(Container $container)
34+
{
35+
$container->set(
36+
PluginInterface::class,
37+
function (Container $container) {
38+
$dispatcher = $container->get(DispatcherInterface::class);
39+
40+
$plugin = new Cookie(
41+
$dispatcher,
42+
(array) PluginHelper::getPlugin('authentication', 'cookie')
43+
);
44+
$plugin->setApplication(Factory::getApplication());
45+
$plugin->setDatabase($container->get(DatabaseInterface::class));
46+
47+
return $plugin;
48+
}
49+
);
50+
}
51+
};

plugins/authentication/cookie/cookie.php renamed to plugins/authentication/cookie/src/Extension/Cookie.php

Lines changed: 68 additions & 80 deletions
Large diffs are not rendered by default.

plugins/authentication/joomla/joomla.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
<authorUrl>www.joomla.org</authorUrl>
1010
<version>3.0.0</version>
1111
<description>PLG_AUTHENTICATION_JOOMLA_XML_DESCRIPTION</description>
12+
<namespace path="src">Joomla\Plugin\Authentication\Joomla</namespace>
1213
<files>
13-
<filename plugin="joomla">joomla.php</filename>
14+
<folder plugin="joomla">services</folder>
15+
<folder>src</folder>
1416
</files>
1517
<languages>
1618
<language tag="en-GB">language/en-GB/plg_authentication_joomla.ini</language>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Plugin
5+
* @subpackage Authentication.joomla
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\PluginInterface;
14+
use Joomla\CMS\Factory;
15+
use Joomla\CMS\Plugin\PluginHelper;
16+
use Joomla\Database\DatabaseInterface;
17+
use Joomla\DI\Container;
18+
use Joomla\DI\ServiceProviderInterface;
19+
use Joomla\Event\DispatcherInterface;
20+
use Joomla\Plugin\Authentication\Joomla\Extension\Joomla;
21+
22+
return new class implements ServiceProviderInterface
23+
{
24+
/**
25+
* Registers the service provider with a DI container.
26+
*
27+
* @param Container $container The DI container.
28+
*
29+
* @return void
30+
*
31+
* @since __DEPLOY_VERSION__
32+
*/
33+
public function register(Container $container)
34+
{
35+
$container->set(
36+
PluginInterface::class,
37+
function (Container $container) {
38+
$dispatcher = $container->get(DispatcherInterface::class);
39+
40+
$plugin = new Joomla(
41+
$dispatcher,
42+
(array) PluginHelper::getPlugin('authentication', 'joomla')
43+
);
44+
$plugin->setApplication(Factory::getApplication());
45+
$plugin->setDatabase($container->get(DatabaseInterface::class));
46+
47+
return $plugin;
48+
}
49+
);
50+
}
51+
};

plugins/authentication/joomla/joomla.php renamed to plugins/authentication/joomla/src/Extension/Joomla.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
*
77
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
88
* @license GNU General Public License version 2 or later; see LICENSE.txt
9-
10-
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
119
*/
1210

11+
namespace Joomla\Plugin\Authentication\Joomla\Extension;
12+
1313
use Joomla\CMS\Authentication\Authentication;
14-
use Joomla\CMS\Helper\AuthenticationHelper;
15-
use Joomla\CMS\Language\Text;
1614
use Joomla\CMS\Plugin\CMSPlugin;
17-
use Joomla\CMS\Plugin\PluginHelper;
1815
use Joomla\CMS\User\User;
1916
use Joomla\CMS\User\UserHelper;
17+
use Joomla\Database\DatabaseAwareTrait;
2018

2119
// phpcs:disable PSR1.Files.SideEffects
2220
\defined('_JEXEC') or die;
@@ -27,23 +25,9 @@
2725
*
2826
* @since 1.5
2927
*/
30-
class PlgAuthenticationJoomla extends CMSPlugin
28+
final class Joomla extends CMSPlugin
3129
{
32-
/**
33-
* Application object
34-
*
35-
* @var \Joomla\CMS\Application\CMSApplication
36-
* @since 4.0.0
37-
*/
38-
protected $app;
39-
40-
/**
41-
* Database object
42-
*
43-
* @var \Joomla\Database\DatabaseDriver
44-
* @since 4.0.0
45-
*/
46-
protected $db;
30+
use DatabaseAwareTrait;
4731

4832
/**
4933
* This method should handle any authentication and report back to the subject
@@ -63,12 +47,12 @@ public function onUserAuthenticate($credentials, $options, &$response)
6347
// Joomla does not like blank passwords
6448
if (empty($credentials['password'])) {
6549
$response->status = Authentication::STATUS_FAILURE;
66-
$response->error_message = Text::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
50+
$response->error_message = $this->getApplication()->getLanguage()->_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
6751

6852
return;
6953
}
7054

71-
$db = $this->db;
55+
$db = $this->getDatabase();
7256
$query = $db->getQuery(true)
7357
->select($db->quoteName(['id', 'password']))
7458
->from($db->quoteName('#__users'))
@@ -87,7 +71,7 @@ public function onUserAuthenticate($credentials, $options, &$response)
8771
$response->email = $user->email;
8872
$response->fullname = $user->name;
8973

90-
if ($this->app->isClient('administrator')) {
74+
if ($this->getApplication()->isClient('administrator')) {
9175
$response->language = $user->getParam('admin_language');
9276
} else {
9377
$response->language = $user->getParam('language');
@@ -98,7 +82,7 @@ public function onUserAuthenticate($credentials, $options, &$response)
9882
} else {
9983
// Invalid password
10084
$response->status = Authentication::STATUS_FAILURE;
101-
$response->error_message = Text::_('JGLOBAL_AUTH_INVALID_PASS');
85+
$response->error_message = $this->getApplication()->getLanguage()->_('JGLOBAL_AUTH_INVALID_PASS');
10286
}
10387
} else {
10488
// Let's hash the entered password even if we don't have a matching user for some extra response time
@@ -107,7 +91,7 @@ public function onUserAuthenticate($credentials, $options, &$response)
10791

10892
// Invalid user
10993
$response->status = Authentication::STATUS_FAILURE;
110-
$response->error_message = Text::_('JGLOBAL_AUTH_NO_USER');
94+
$response->error_message = $this->getApplication()->getLanguage()->_('JGLOBAL_AUTH_NO_USER');
11195
}
11296
}
11397
}

plugins/authentication/ldap/ldap.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
<authorUrl>www.joomla.org</authorUrl>
1010
<version>3.0.0</version>
1111
<description>PLG_LDAP_XML_DESCRIPTION</description>
12+
<namespace path="src">Joomla\Plugin\Authentication\Ldap</namespace>
1213
<files>
13-
<filename plugin="ldap">ldap.php</filename>
14+
<folder plugin="ldap">services</folder>
15+
<folder>src</folder>
1416
</files>
1517
<languages>
1618
<language tag="en-GB">language/en-GB/plg_authentication_ldap.ini</language>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Plugin
5+
* @subpackage Authentication.ldap
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\PluginInterface;
14+
use Joomla\CMS\Factory;
15+
use Joomla\CMS\Plugin\PluginHelper;
16+
use Joomla\Database\DatabaseInterface;
17+
use Joomla\DI\Container;
18+
use Joomla\DI\ServiceProviderInterface;
19+
use Joomla\Event\DispatcherInterface;
20+
use Joomla\Plugin\Authentication\Ldap\Extension\Ldap;
21+
22+
return new class implements ServiceProviderInterface
23+
{
24+
/**
25+
* Registers the service provider with a DI container.
26+
*
27+
* @param Container $container The DI container.
28+
*
29+
* @return void
30+
*
31+
* @since __DEPLOY_VERSION__
32+
*/
33+
public function register(Container $container)
34+
{
35+
$container->set(
36+
PluginInterface::class,
37+
function (Container $container) {
38+
$dispatcher = $container->get(DispatcherInterface::class);
39+
40+
$plugin = new Ldap(
41+
$dispatcher,
42+
(array) PluginHelper::getPlugin('authentication', 'ldap')
43+
);
44+
$plugin->setApplication(Factory::getApplication());
45+
$plugin->setDatabase($container->get(DatabaseInterface::class));
46+
47+
return $plugin;
48+
}
49+
);
50+
}
51+
};

0 commit comments

Comments
 (0)