Skip to content
Snippets Groups Projects
Commit 2fd69974 authored by João Ventura's avatar João Ventura Committed by Joao Ventura
Browse files

Issue #2925066 by daggerhart, JeroenT, screon, jonas139, jcnventura: Refactor...

Issue #2925066 by daggerhart, JeroenT, screon, jonas139, jcnventura: Refactor implicitly named setup plugin id expectations to an annotation property
parent 864efe18
No related branches found
Tags 8.x-2.0-alpha4
No related merge requests found
......@@ -36,4 +36,11 @@ class TfaLogin extends Plugin {
*/
public $description;
/**
* Plugin ID for user-specific setup plugin for this login plugin.
*
* @var string
*/
public $setupPluginId;
}
......@@ -36,4 +36,11 @@ class TfaSend extends Plugin {
*/
public $description;
/**
* Plugin ID for user-specific setup plugin for this send plugin.
*
* @var string
*/
public $setupPluginId;
}
......@@ -36,4 +36,11 @@ class TfaValidation extends Plugin {
*/
public $description;
/**
* Plugin ID for user-specific setup plugin for this validation plugin.
*
* @var string
*/
public $setupPluginId;
}
......@@ -6,7 +6,10 @@ use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\tfa\TfaDataTrait;
use Drupal\tfa\TfaLoginPluginManager;
use Drupal\tfa\TfaSendPluginManager;
use Drupal\tfa\TfaSetupPluginManager;
use Drupal\tfa\TfaValidationPluginManager;
use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -24,6 +27,27 @@ class BasicOverview extends FormBase {
*/
protected $tfaSetup;
/**
* Validation plugin manager.
*
* @var \Drupal\tfa\TfaValidationPluginManager
*/
protected $tfaValidation;
/**
* Login plugin manager.
*
* @var \Drupal\tfa\TfaLoginPluginManager
*/
protected $tfaLogin;
/**
* Send plugin manager.
*
* @var \Drupal\tfa\TfaSendPluginManager
*/
protected $tfaSend;
/**
* Provides the user data service object.
*
......@@ -38,10 +62,19 @@ class BasicOverview extends FormBase {
* The user data service.
* @param \Drupal\tfa\TfaSetupPluginManager $tfa_setup_manager
* The setup plugin manager.
* @param \Drupal\tfa\TfaValidationPluginManager $tfa_validation_manager
* The validation plugin manager.
* @param \Drupal\tfa\TfaLoginPluginManager $tfa_login_manager
* The login plugin manager.
* @param \Drupal\tfa\TfaSendPluginManager $tfa_send_manager
* The send plugin manager.
*/
public function __construct(UserDataInterface $user_data, TfaSetupPluginManager $tfa_setup_manager) {
public function __construct(UserDataInterface $user_data, TfaSetupPluginManager $tfa_setup_manager, TfaValidationPluginManager $tfa_validation_manager, TfaLoginPluginManager $tfa_login_manager, TfaSendPluginManager $tfa_send_manager) {
$this->userData = $user_data;
$this->tfaSetup = $tfa_setup_manager;
$this->tfaValidation = $tfa_validation_manager;
$this->tfaLogin = $tfa_login_manager;
$this->tfaSend = $tfa_send_manager;
}
/**
......@@ -50,7 +83,10 @@ class BasicOverview extends FormBase {
public static function create(ContainerInterface $container) {
return new static(
$container->get('user.data'),
$container->get('plugin.manager.tfa.setup')
$container->get('plugin.manager.tfa.setup'),
$container->get('plugin.manager.tfa.validation'),
$container->get('plugin.manager.tfa.login'),
$container->get('plugin.manager.tfa.send')
);
}
......@@ -106,23 +142,28 @@ class BasicOverview extends FormBase {
if ($configuration['enabled']) {
$enabled = isset($user_tfa['status'], $user_tfa['data']) && !empty($user_tfa['data']['plugins']) && $user_tfa['status'];
// Validation plugin setup.
$allowed_plugins = $configuration['allowed_validation_plugins'];
$enabled_plugins = isset($user_tfa['data']['plugins']) ? $user_tfa['data']['plugins'] : [];
foreach ($allowed_plugins as $allowed_plugin) {
$output[$allowed_plugin] = $this->tfaPluginSetupFormOverview($allowed_plugin, $user, !empty($enabled_plugins[$allowed_plugin]));
$validation_plugins = $this->tfaValidation->getDefinitions();
foreach ($validation_plugins as $plugin_id => $plugin) {
if (!empty($configuration['allowed_validation_plugins'][$plugin_id])) {
$output[$plugin_id] = $this->tfaPluginSetupFormOverview($plugin, $user, !empty($enabled_plugins[$plugin_id]));
}
}
if ($enabled) {
$login_plugins = $configuration['login_plugins'];
foreach ($login_plugins as $lplugin_id) {
$output[$lplugin_id] = $this->tfaPluginSetupFormOverview($lplugin_id, $user, TRUE);
$login_plugins = $this->tfaLogin->getDefinitions();
foreach ($login_plugins as $plugin_id => $plugin) {
if (!empty($configuration['login_plugins'][$plugin_id])) {
$output[$plugin_id] = $this->tfaPluginSetupFormOverview($plugin, $user, TRUE);
}
}
$send_plugin = $configuration['send_plugins'];
if ($send_plugin) {
$output[$send_plugin] = $this->tfaPluginSetupFormOverview($send_plugin, $user, TRUE);
$send_plugins = $this->tfaSend->getDefinitions();
foreach ($send_plugins as $plugin_id => $plugin) {
if (!empty($configuration['send_plugins'][$plugin_id])) {
$output[$plugin_id] = $this->tfaPluginSetupFormOverview($plugin, $user, TRUE);
}
}
}
}
......@@ -162,8 +203,8 @@ class BasicOverview extends FormBase {
/**
* Get TFA basic setup action links for use on overview page.
*
* @param string $plugin
* The setup plugin.
* @param array $plugin
* Plugin definition.
* @param object $account
* Current user account.
* @param bool $enabled
......@@ -172,15 +213,15 @@ class BasicOverview extends FormBase {
* @return array
* Render array
*/
protected function tfaPluginSetupFormOverview($plugin, $account, $enabled) {
protected function tfaPluginSetupFormOverview(array $plugin, $account, $enabled) {
$params = [
'enabled' => $enabled,
'account' => $account,
'plugin_id' => $plugin,
'plugin_id' => $plugin['id'],
];
try {
return $this->tfaSetup
->createInstance($plugin . '_setup', ['uid' => $account->id()])
->createInstance($plugin['setupPluginId'], ['uid' => $account->id()])
->getOverview($params);
}
catch (\Exception $e) {
......
......@@ -2,12 +2,16 @@
namespace Drupal\tfa\Form;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\tfa\TfaDataTrait;
use Drupal\tfa\TfaLoginPluginManager;
use Drupal\tfa\TfaSendPluginManager;
use Drupal\tfa\TfaSetup;
use Drupal\tfa\TfaValidationPluginManager;
use Drupal\user\Entity\User;
use Drupal\user\UserDataInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -27,6 +31,27 @@ class BasicSetup extends FormBase {
*/
protected $manager;
/**
* The validation plugin manager.
*
* @var \Drupal\tfa\TfaValidationPluginManager
*/
protected $tfaValidation;
/**
* The login plugin manager.
*
* @var \Drupal\tfa\TfaLoginPluginManager
*/
protected $tfaLogin;
/**
* The send plugin manager.
*
* @var \Drupal\tfa\TfaSendPluginManager
*/
protected $tfaSend;
/**
* Provides the user data service object.
*
......@@ -40,7 +65,10 @@ class BasicSetup extends FormBase {
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.tfa.setup'),
$container->get('user.data')
$container->get('user.data'),
$container->get('plugin.manager.tfa.validation'),
$container->get('plugin.manager.tfa.login'),
$container->get('plugin.manager.tfa.send')
);
}
......@@ -51,10 +79,19 @@ class BasicSetup extends FormBase {
* The plugin manager to fetch plugin information.
* @param \Drupal\user\UserDataInterface $user_data
* The user data object to store user information.
* @param \Drupal\tfa\TfaValidationPluginManager $tfa_validation_manager
* The validation plugin manager.
* @param \Drupal\tfa\TfaLoginPluginManager $tfa_login_manager
* The login plugin manager.
* @param \Drupal\tfa\TfaSendPluginManager $tfa_send_manager
* The send plugin manager.
*/
public function __construct(PluginManagerInterface $manager, UserDataInterface $user_data) {
public function __construct(PluginManagerInterface $manager, UserDataInterface $user_data, TfaValidationPluginManager $tfa_validation_manager, TfaLoginPluginManager $tfa_login_manager, TfaSendPluginManager $tfa_send_manager) {
$this->manager = $manager;
$this->userData = $user_data;
$this->tfaValidation = $tfa_validation_manager;
$this->tfaLogin = $tfa_login_manager;
$this->tfaSend = $tfa_send_manager;
}
/**
......@@ -64,6 +101,33 @@ class BasicSetup extends FormBase {
return 'tfa_setup';
}
/**
* Find the correct plugin that is being setup.
*
* @param string $plugin_id
* Plugin ID.
*
* @return array|null
* Plugin definitions.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function findPlugin($plugin_id) {
$plugin = $this->tfaValidation->getDefinition($plugin_id, FALSE);
if (empty($plugin)) {
$plugin = $this->tfaLogin->getDefinition($plugin_id, FALSE);
}
if (empty($plugin)) {
$plugin = $this->tfaSend->getDefinition($plugin_id, FALSE);
}
if (empty($plugin)) {
throw new PluginNotFoundException($plugin_id, sprintf('The "%s" plugin does not exist.', $plugin_id));
}
return $plugin;
}
/**
* {@inheritdoc}
*/
......@@ -127,10 +191,8 @@ class BasicSetup extends FormBase {
// Record methods progressed.
$storage['steps'][] = $method;
$plugin_id = $method . '_setup';
$validation_inst = \Drupal::service('plugin.manager.tfa.setup');
$setup_plugin = $validation_inst->createInstance($plugin_id, ['uid' => $user->id()]);
$plugin = $this->findPlugin($method);
$setup_plugin = $this->manager->createInstance($plugin['setupPluginId'], ['uid' => $account->id()]);
$tfa_setup = new TfaSetup($setup_plugin);
$form = $tfa_setup->getForm($form, $form_state, $reset);
$storage[$method] = $tfa_setup;
......
......@@ -128,19 +128,11 @@ class SettingsForm extends ConfigFormBase {
// Get Send Plugins.
$send_plugins = $this->tfaSend->getDefinitions();
// Get Setup Plugins.
$setup_plugins = $this->tfaSetup->getDefinitions();
// Get Validation Plugins.
$validation_plugins = $this->tfaValidation->getDefinitions();
// Get validation plugin labels.
$validation_plugins_labels = [];
foreach ($validation_plugins as $key => $plugin) {
// Skip this plugin if no setup class is available.
if (!isset($setup_plugins[$key . '_setup'])) {
unset($validation_plugins[$key]);
continue;
}
$validation_plugins_labels[$plugin['id']] = $plugin['label']->render();
}
// Fetching all available encryption profiles.
......
......@@ -18,6 +18,7 @@ use Drupal\user\UserDataInterface;
* id = "tfa_trusted_browser",
* label = @Translation("TFA Trusted Browser"),
* description = @Translation("TFA Trusted Browser Plugin"),
* setupPluginId = "tfa_trusted_browser_setup",
* )
*/
class TfaTrustedBrowser extends TfaBasePlugin implements TfaLoginInterface, TfaValidationInterface {
......
......@@ -22,6 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* id = "tfa_recovery_code",
* label = @Translation("TFA Recovery Code"),
* description = @Translation("TFA Recovery Code Validation Plugin"),
* setupPluginId = "tfa_recovery_code_setup",
* )
*/
class TfaRecoveryCode extends TfaBasePlugin implements TfaValidationInterface, ContainerFactoryPluginInterface {
......
......@@ -125,4 +125,42 @@ class TfaLoginPluginManager extends DefaultPluginManager {
return NULL;
}
/**
* {@inheritdoc}
*
* Provide some backwards compatibility with the old implicit setupPluginId.
* This will give other modules more time to update their plugins.
*
* @deprecated in tfa:8.x-1.0-alpha7 and is removed from tfa:8.x-2.0. Please
* specify the setupPluginId property in the plugin annotation.
* @see https://www.drupal.org/project/tfa/issues/2925066
*/
public function getDefinitions() {
$definitions = parent::getDefinitions();
foreach ($definitions as &$definition) {
if (empty($definition['setupPluginId'])) {
$definition['setupPluginId'] = $definition['id'] . '_setup';
}
}
return $definitions;
}
/**
* {@inheritdoc}
*
* Provide some backwards compatibility with the old implicit setupPluginId.
* This will give other modules more time to update their plugins.
*
* @deprecated in tfa:8.x-1.0-alpha7 and is removed from tfa:8.x-2.0. Please
* specify the setupPluginId property in the plugin annotation.
* @see https://www.drupal.org/project/tfa/issues/2925066
*/
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
$plugin = parent::getDefinition($plugin_id, $exception_on_invalid);
if (is_array($plugin) && empty($plugin['setupPluginId'])) {
$plugin['setupPluginId'] = $plugin_id . '_setup';
}
return $plugin;
}
}
......@@ -28,4 +28,42 @@ class TfaSendPluginManager extends DefaultPluginManager {
$this->setCacheBackend($cache_backend, 'tfa_send');
}
/**
* {@inheritdoc}
*
* Provide some backwards compatibility with the old implicit setupPluginId.
* This will give other modules more time to update their plugins.
*
* @deprecated in tfa:8.x-1.0-alpha7 and is removed from tfa:8.x-2.0. Please
* specify the setupPluginId property in the plugin annotation.
* @see https://www.drupal.org/project/tfa/issues/2925066
*/
public function getDefinitions() {
$definitions = parent::getDefinitions();
foreach ($definitions as &$definition) {
if (empty($definition['setupPluginId'])) {
$definition['setupPluginId'] = $definition['id'] . '_setup';
}
}
return $definitions;
}
/**
* {@inheritdoc}
*
* Provide some backwards compatibility with the old implicit setupPluginId.
* This will give other modules more time to update their plugins.
*
* @deprecated in tfa:8.x-1.0-alpha7 and is removed from tfa:8.x-2.0. Please
* specify the setupPluginId property in the plugin annotation.
* @see https://www.drupal.org/project/tfa/issues/2925066
*/
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
$plugin = parent::getDefinition($plugin_id, $exception_on_invalid);
if (is_array($plugin) && empty($plugin['setupPluginId'])) {
$plugin['setupPluginId'] = $plugin_id . '_setup';
}
return $plugin;
}
}
......@@ -7,8 +7,6 @@ use Drupal\tfa\Plugin\TfaSetupInterface;
/**
* Class TfaSetup.
*
* @todo This class is somewhat useless. Deprecate this later.
*/
class TfaSetup {
/**
......
......@@ -2,6 +2,7 @@
namespace Drupal\tfa;
use Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
......@@ -15,6 +16,7 @@ use Drupal\user\UserDataInterface;
* The validation plugin manager.
*/
class TfaValidationPluginManager extends DefaultPluginManager {
use DiscoveryCachedTrait;
/**
* Provides the user data service object.
......@@ -93,4 +95,42 @@ class TfaValidationPluginManager extends DefaultPluginManager {
return $plugin;
}
/**
* {@inheritdoc}
*
* Provide some backwards compatibility with the old implicit setupPluginId.
* This will give other modules more time to update their plugins.
*
* @deprecated in tfa:8.x-1.0-alpha7 and is removed from tfa:8.x-2.0. Please
* specify the setupPluginId property in the plugin annotation.
* @see https://www.drupal.org/project/tfa/issues/2925066
*/
public function getDefinitions() {
$definitions = parent::getDefinitions();
foreach ($definitions as &$definition) {
if (empty($definition['setupPluginId'])) {
$definition['setupPluginId'] = $definition['id'] . '_setup';
}
}
return $definitions;
}
/**
* {@inheritdoc}
*
* Provide some backwards compatibility with the old implicit setupPluginId.
* This will give other modules more time to update their plugins.
*
* @deprecated in tfa:8.x-1.0-alpha7 and is removed from tfa:8.x-2.0. Please
* specify the setupPluginId property in the plugin annotation.
* @see https://www.drupal.org/project/tfa/issues/2925066
*/
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
$plugin = parent::getDefinition($plugin_id, $exception_on_invalid);
if (is_array($plugin) && empty($plugin['setupPluginId'])) {
$plugin['setupPluginId'] = $plugin_id . '_setup';
}
return $plugin;
}
}
......@@ -15,6 +15,7 @@ use Drupal\tfa\Plugin\TfaValidationInterface;
* id = "tfa_test_plugins_validation",
* label = @Translation("TFA Test Validation Plugin"),
* description = @Translation("TFA Test Validation Plugin"),
* setupPluginId = "tfa_test_plugins_validation_setup",
* )
*/
class TfaTestValidationPlugin extends TfaBasePlugin implements TfaValidationInterface {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment