Skip to content
Snippets Groups Projects
Commit 3120a988 authored by Matthew Slater's avatar Matthew Slater
Browse files

drupal9 ready; improved plugin interface

parent 8aa3d013
No related branches found
No related tags found
No related merge requests found
name: Alternative Login ID & Display Name name: Alternative Login ID & Display Name
description: Provides options for populating the username field, for logging on, and for displaying the username. description: Provides options for populating the username field, for logging on, and for displaying the username.
project: alt_login project: alt_login
core_version_requirement: ^8 core_version_requirement: ^8 || ^9
type: module type: module
configure: alt_login.admin configure: alt_login.admin
...@@ -135,8 +135,8 @@ function alt_login_login_name_element_validate(&$element, $form_state) { ...@@ -135,8 +135,8 @@ function alt_login_login_name_element_validate(&$element, $form_state) {
function alt_login_convert_alias($alias) { function alt_login_convert_alias($alias) {
foreach (alt_login_active_plugins() as $plugin) { foreach (alt_login_active_plugins() as $plugin) {
if ($plugin->applies($alias)) { if ($plugin->applies($alias)) {
if ($name = $plugin->getUsernameFromAlias($alias)) { if ($user = $plugin->getUserFromAlias($alias)) {
return $name; return $user->getAccountName();
} }
} }
} }
......
...@@ -18,7 +18,7 @@ interface AltLoginMethodInterface { ...@@ -18,7 +18,7 @@ interface AltLoginMethodInterface {
* @return string | null * @return string | null
* If another account has the same alias, flag the error, on this fieldname. * If another account has the same alias, flag the error, on this fieldname.
*/ */
function dedupeAlias(UserInterface $user); function dedupeAlias(UserInterface $user) :string;
/** /**
* Check wheteher the given string is likely to be an alias generated by this * Check wheteher the given string is likely to be an alias generated by this
...@@ -28,17 +28,16 @@ interface AltLoginMethodInterface { ...@@ -28,17 +28,16 @@ interface AltLoginMethodInterface {
* *
* @return bool * @return bool
*/ */
function applies($alias); function applies($alias) : bool;
/** /**
* When validating a login, find which user it applies to. * When validating a login, find which user it applies to.
* *
* @param string $name * @param string $alias
* *
* @return string | NULL * @return UserInterface | NULL
* The real username or Null if the alias doesn't correspond to a user.
*/ */
function getUsernameFromAlias($alias); function getUserFromAlias($alias) : UserInterface;
/** /**
* Get the alias for the given user. * Get the alias for the given user.
...@@ -47,7 +46,7 @@ interface AltLoginMethodInterface { ...@@ -47,7 +46,7 @@ interface AltLoginMethodInterface {
* *
* @return string * @return string
*/ */
function getAlias(UserInterface $user); function getAlias(UserInterface $user) : string ;
/** /**
......
...@@ -6,7 +6,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; ...@@ -6,7 +6,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase; use Drupal\Core\DependencyInjection\ServiceProviderBase;
/** /**
* Modifies the language manager service. * Modifies Drupal's services
*/ */
class AltLoginServiceProvider extends ServiceProviderBase { class AltLoginServiceProvider extends ServiceProviderBase {
......
...@@ -9,6 +9,7 @@ use Drupal\Core\Database\Connection; ...@@ -9,6 +9,7 @@ use Drupal\Core\Database\Connection;
use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\Query\Sql\Condition; use Drupal\Core\Entity\Query\Sql\Condition;
use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Entity\Query\QueryFactory;
...@@ -44,20 +45,20 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte ...@@ -44,20 +45,20 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte
private $entityFieldManager; private $entityFieldManager;
/** /**
* @var QueryFactory * @var EntityTypeManagerInterface
*/ */
private $entityQueryFactory; private $entityTypeManager;
/** /**
* @param EntityFieldManagerInterface $entity_field_manager * @param EntityFieldManagerInterface $entity_field_manager
* @param Connection $database * @param Connection $database
* @param QueryFactory $query_factory * @param EntityTypeManagerInterface $entity_type_manager
* @param MessengerInterface $messenger * @param MessengerInterface $messenger
*/ */
function __construct(EntityFieldManagerInterface $entity_field_manager, Connection $database, QueryFactory $query_factory, MessengerInterface $messenger) { function __construct(EntityFieldManagerInterface $entity_field_manager, Connection $database, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
$this->entityFieldManager = $entity_field_manager; $this->entityFieldManager = $entity_field_manager;
$this->database = $database; $this->database = $database;
$this->entityQueryFactory = $query_factory; $this->entityTypeManager = $entity_type_manager;
$this->messenger = $messenger; $this->messenger = $messenger;
} }
...@@ -73,7 +74,7 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte ...@@ -73,7 +74,7 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte
return new static ( return new static (
$container->get('entity_field.manager'), $container->get('entity_field.manager'),
$container->get('database'), $container->get('database'),
$container->get('entity.query'), $container->get('entity_type.manager'),
$container->get('messenger') $container->get('messenger')
); );
} }
...@@ -82,7 +83,7 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte ...@@ -82,7 +83,7 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function dedupeAlias(UserInterface $user) { function dedupeAlias(UserInterface $user) :string{
$alias = $this->getAlias($user); $alias = $this->getAlias($user);
if (empty($alias)) { if (empty($alias)) {
$this->messenger->addWarning($this->t('Neither given name nor family name provided in address field.')); $this->messenger->addWarning($this->t('Neither given name nor family name provided in address field.'));
...@@ -97,27 +98,24 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte ...@@ -97,27 +98,24 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function applies($alias) { function applies($alias) : bool {
// Pretty much any string could be valid // Pretty much any string could be valid
return TRUE; return TRUE;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*
* Its tricky to use entityQuery if the given and family names are not exactly
* one word each.
*/ */
function getUsernameFromAlias($alias) { function getUserFromAlias($alias) : UserInterface{
if ($uids = $this->getUids($alias)) { if ($uids = $this->getUids($alias)) {
return User::load(reset($uids))->getUsername(); return User::load(reset($uids));
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function getAlias(UserInterface $user){ function getAlias(UserInterface $user) : string {
$field_name = $this->fieldName(); $field_name = $this->fieldName();
return implode(' ', [$user->{$field_name}->given_name, $user->{$field_name}->family_name]); return implode(' ', [$user->{$field_name}->given_name, $user->{$field_name}->family_name]);
} }
...@@ -163,7 +161,7 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte ...@@ -163,7 +161,7 @@ class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInte
$or_group->condition($fname.'.family_name', $first, 'STARTS_WITH'); $or_group->condition($fname.'.family_name', $first, 'STARTS_WITH');
} }
else { else {
$and_group = $this->entityQueryFactory->get('user')->andConditionGroup(); $and_group = $this->entityTypeManager->getStorage('user')->getQuery()->andConditionGroup();
$and_group->condition($fname.'.given_name', substr($first, 0, 4), 'STARTS_WITH'); $and_group->condition($fname.'.given_name', substr($first, 0, 4), 'STARTS_WITH');
$and_group->condition($fname.'.family_name', $last, 'STARTS_WITH'); $and_group->condition($fname.'.family_name', $last, 'STARTS_WITH');
$or_group->condition($and_group); $or_group->condition($and_group);
......
...@@ -25,11 +25,13 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor ...@@ -25,11 +25,13 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor
* @var EmailValidatorInterface * @var EmailValidatorInterface
*/ */
private $emailValidator; private $emailValidator;
/** /**
* @var EntityTypeManagerInterface * @var EntityTypeManagerInterface
*/ */
private $entityTypeManager; private $entityTypeManager;
/** /**
* @param EntityTypeManagerInterface $entity_type_manager * @param EntityTypeManagerInterface $entity_type_manager
* @param EmailValidatorInterface $email_validator * @param EmailValidatorInterface $email_validator
...@@ -40,12 +42,11 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor ...@@ -40,12 +42,11 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor
} }
/** /**
*
* @param ContainerInterface $container * @param ContainerInterface $container
* @param array $configuration * @param array $configuration
* @param string $plugin_id * @param type $plugin_id
* @param array $plugin_definition * @param type $plugin_definition
* @return static * @return \static
*/ */
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static ( return new static (
...@@ -57,7 +58,7 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor ...@@ -57,7 +58,7 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function applies($alias) { function applies($alias) : bool{
return $this->emailValidator->isValid($alias); return $this->emailValidator->isValid($alias);
} }
...@@ -65,17 +66,17 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor ...@@ -65,17 +66,17 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function getUsernameFromAlias($alias) { function getUserFromAlias($alias): UserInterface {
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(['mail' => $alias]); $users = $this->entityTypeManager->getStorage('user')->loadByProperties(['mail' => $alias]);
if (count($users)) { if (count($users)) {
return reset($users)->getUsername(); return reset($users);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function getAlias(UserInterface $user){ function getAlias(UserInterface $user) : string {
return $user->getEmail(); return $user->getEmail();
} }
......
...@@ -21,7 +21,7 @@ class Uid extends Username implements AltLoginMethodInterface { ...@@ -21,7 +21,7 @@ class Uid extends Username implements AltLoginMethodInterface {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function applies($alias) { function applies($alias) : bool{
$num = (int)$alias; $num = (int)$alias;
return $num == $alias; return $num == $alias;
} }
...@@ -29,16 +29,16 @@ class Uid extends Username implements AltLoginMethodInterface { ...@@ -29,16 +29,16 @@ class Uid extends Username implements AltLoginMethodInterface {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function getUsernameFromAlias($alias) { function getUserFromAlias($alias) : UserInterface{
if ($user = User::load($alias)) { if ($user = User::load($alias)) {
return $user->getUsername(); return $user;
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function getAlias(UserInterface $user){ function getAlias(UserInterface $user) : string {
return $user->id(); return $user->id();
} }
......
...@@ -17,11 +17,25 @@ use Drupal\Core\Entity\Query\Sql\Condition; ...@@ -17,11 +17,25 @@ use Drupal\Core\Entity\Query\Sql\Condition;
*/ */
class Username implements AltLoginMethodInterface { class Username implements AltLoginMethodInterface {
private $entityTypeManager;
/**
* @param EntityTypeManagerInterface $entity_type_manager
*/
function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static (
$container->get('entity_type.manager'),
);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function dedupeAlias(UserInterface $user) { function dedupeAlias(UserInterface $user) :string {
// This is checked by the user module and database anyway. // This is checked by the user module and database anyway.
return FALSE; return FALSE;
} }
...@@ -29,21 +43,24 @@ class Username implements AltLoginMethodInterface { ...@@ -29,21 +43,24 @@ class Username implements AltLoginMethodInterface {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function applies($alias) { function applies($alias) : bool{
return TRUE; return TRUE;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function getUsernameFromAlias($alias) { function getUserFromAlias($alias) : UserInterface{
return $alias; $users = $this->entityTypeManager->getStorage('user')->loadByProperties(['name' => $alias]);
if (count($users)) {
return reset($users);
}
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
function getAlias(UserInterface $user){ function getAlias(UserInterface $user) : string {
return $user->getUsername(); return $user->getUsername();
} }
......
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