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

tidy up

parent bb387821
No related branches found
No related tags found
No related merge requests found
...@@ -112,19 +112,30 @@ function alt_login_form_alter(&$form, $form_state, $form_id) { ...@@ -112,19 +112,30 @@ function alt_login_form_alter(&$form, $form_state, $form_id) {
* the form-level validation. * the form-level validation.
*/ */
function alt_login_login_name_element_validate(&$element, $form_state) { function alt_login_login_name_element_validate(&$element, $form_state) {
$alias = $element['#value']; $name = alt_login_convert_alias($element['#value']);
foreach (activePlugins() as $plugin_id => $plugin) { $form_state->setValue('name', $name);
}
/**
* Utility
*
* Look up the given alias and return the real username for logging in. If the
* username is given but the plugin not enabled, return NULL.
*
* @param string $alias
*
* @return string | NULL
*/
function alt_login_convert_alias($alias) {
foreach (activePlugins() as $plugin) {
if ($plugin->applies($alias)) { if ($plugin->applies($alias)) {
if ($name = $plugin->getUsernameFromAlias($alias)) { if ($name = $plugin->getUsernameFromAlias($alias)) {
$form_state->setValue('name', $name); return $name;
return;
} }
} }
} }
$form_state->setValue('name', '');
} }
/** /**
* Get all the alternative login strings for the given user * Get all the alternative login strings for the given user
* *
...@@ -134,19 +145,16 @@ function alt_login_login_name_element_validate(&$element, $form_state) { ...@@ -134,19 +145,16 @@ function alt_login_login_name_element_validate(&$element, $form_state) {
*/ */
function alt_login_get_aliases(UserInterface $user) { function alt_login_get_aliases(UserInterface $user) {
$alts = []; $alts = [];
foreach (activePlugins() as $plugin) { foreach (activePlugins() as $plugin_id => $plugin) {
$alts[$plugin_id] = $plugin->getAlias($user); $alts[$plugin_id] = $plugin->getAlias($user);
} }
return $alts; return $alts;
} }
/** /**
* $context = [ * Implements hook_tokens_alter().
* 'type' => $type, *
* 'tokens' => $tokens, * Replace the username with any aliases.
* 'data' => $data,
* 'options' => $options,
* ];
*/ */
function alt_login_tokens_alter(&$replacements, array $context, $bubbleable_metadata) { function alt_login_tokens_alter(&$replacements, array $context, $bubbleable_metadata) {
if ($context['type'] == 'user' and $user = $context['data']['user'] and isset($replacements['[user:name]'])) { if ($context['type'] == 'user' and $user = $context['data']['user'] and isset($replacements['[user:name]'])) {
...@@ -155,7 +163,7 @@ function alt_login_tokens_alter(&$replacements, array $context, $bubbleable_meta ...@@ -155,7 +163,7 @@ function alt_login_tokens_alter(&$replacements, array $context, $bubbleable_meta
} }
/** /**
* implements hook_user_presave(). * Implements hook_user_presave().
*/ */
function alt_login_user_presave(UserInterface $account) { function alt_login_user_presave(UserInterface $account) {
if ($account->isNew()) { if ($account->isNew()) {
...@@ -197,6 +205,8 @@ function alt_login_validate_dedupe_aliases($form, $form_state) { ...@@ -197,6 +205,8 @@ function alt_login_validate_dedupe_aliases($form, $form_state) {
/** /**
* Utility * Utility
* *
* Could be useful for exporting.
*
* @param UserInterface $user * @param UserInterface $user
* @param string $plugin_id * @param string $plugin_id
* *
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Drupal\alt_login\Authentication\Provider; namespace Drupal\alt_login\Authentication\Provider;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
...@@ -14,15 +13,8 @@ class BasicAuth extends \Drupal\basic_auth\Authentication\Provider\BasicAuth { ...@@ -14,15 +13,8 @@ class BasicAuth extends \Drupal\basic_auth\Authentication\Provider\BasicAuth {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function authenticate(Request $request) { public function authenticate(Request $request) {
$aliases = $this->configFactory->get('alt_login.settings')->get('login'); $alias = $request->headers->get('PHP_AUTH_USER');
$user_id = $request->headers->get('PHP_AUTH_USER'); $request->headers->set('PHP_AUTH_USER', alt_login_convert_alias($alias));
if (!empty($aliases[ALT_LOGIN_WITH_UID]) and is_numeric($user_id)) {
$request->headers->set('PHP_AUTH_USER', User::load($user_id)->getAccountName());
}
elseif (!empty($aliases[ALT_LOGIN_WITH_UID]) and \Drupal::service('email.validator')->isValid($user_id)) {
$users = \Drupal::entityManager()->getStorage('user')->loadByProperties(['mail' => $user_id]);
$request->headers->set('PHP_AUTH_USER', reset($users)->getAccountName());
}
return parent::authenticate($request); return parent::authenticate($request);
} }
......
...@@ -6,6 +6,8 @@ use Drupal\alt_login\AltLoginMethodInterface; ...@@ -6,6 +6,8 @@ use Drupal\alt_login\AltLoginMethodInterface;
use Drupal\user\UserInterface; use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Database\Database; use Drupal\Core\Database\Database;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/** /**
* Plugin implementation for logging in with the user name as an alias. * Plugin implementation for logging in with the user name as an alias.
...@@ -18,6 +20,8 @@ use Drupal\Core\Database\Database; ...@@ -18,6 +20,8 @@ use Drupal\Core\Database\Database;
*/ */
class AddressName implements AltLoginMethodInterface { class AddressName implements AltLoginMethodInterface {
use StringTranslationTrait;
/** /**
* The name of the address field on the user entity. * The name of the address field on the user entity.
* @var string * @var string
...@@ -37,10 +41,12 @@ class AddressName implements AltLoginMethodInterface { ...@@ -37,10 +41,12 @@ class AddressName implements AltLoginMethodInterface {
/** /**
* @param EmailValidator $entity_field_manager * @param EmailValidator $entity_field_manager
* @param Database $database * @param Database $database
* @param MessengerInterface $messenger
*/ */
function __construct($configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, Database $database) { function __construct($configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, Database $database, MessengerInterface $messenger) {
$this->entityFieldManager = $entity_field_manager; $this->entityFieldManager = $entity_field_manager;
$this->database = $database; $this->database = $database;
$this->messenger = $messenger;
} }
/** /**
...@@ -57,7 +63,8 @@ class AddressName implements AltLoginMethodInterface { ...@@ -57,7 +63,8 @@ class AddressName implements AltLoginMethodInterface {
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('entity_field.manager'), $container->get('entity_field.manager'),
$container->get('database') $container->get('database'),
$container->get('messenger')
); );
} }
...@@ -67,6 +74,9 @@ class AddressName implements AltLoginMethodInterface { ...@@ -67,6 +74,9 @@ class AddressName implements AltLoginMethodInterface {
*/ */
function dedupeAlias(UserInterface $user) { function dedupeAlias(UserInterface $user) {
$alias = $this->getAlias($user); $alias = $this->getAlias($user);
if (empty($alias)) {
$this->messenger->addWarning($this->t('Neither given name nor family name provided in address field.'));
}
$uids = $this->getUids($alias); $uids = $this->getUids($alias);
if (!$user->isNew()) { if (!$user->isNew()) {
unset($uids[array_search($user->id(), $uids)]); unset($uids[array_search($user->id(), $uids)]);
...@@ -99,7 +109,7 @@ class AddressName implements AltLoginMethodInterface { ...@@ -99,7 +109,7 @@ class AddressName implements AltLoginMethodInterface {
*/ */
function getAlias(UserInterface $user){ function getAlias(UserInterface $user){
$field_name = $this->fieldName(); $field_name = $this->fieldName();
return $user->{$field_name}->given_name .' '.$user->{$field_name}->family_name; return implode(' ', [$user->{$field_name}->given_name, $user->{$field_name}->family_name]);
} }
......
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