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) {
* the form-level validation.
*/
function alt_login_login_name_element_validate(&$element, $form_state) {
$alias = $element['#value'];
foreach (activePlugins() as $plugin_id => $plugin) {
$name = alt_login_convert_alias($element['#value']);
$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 ($name = $plugin->getUsernameFromAlias($alias)) {
$form_state->setValue('name', $name);
return;
return $name;
}
}
}
$form_state->setValue('name', '');
}
/**
* Get all the alternative login strings for the given user
*
......@@ -134,19 +145,16 @@ function alt_login_login_name_element_validate(&$element, $form_state) {
*/
function alt_login_get_aliases(UserInterface $user) {
$alts = [];
foreach (activePlugins() as $plugin) {
foreach (activePlugins() as $plugin_id => $plugin) {
$alts[$plugin_id] = $plugin->getAlias($user);
}
return $alts;
}
/**
* $context = [
* 'type' => $type,
* 'tokens' => $tokens,
* 'data' => $data,
* 'options' => $options,
* ];
* Implements hook_tokens_alter().
*
* Replace the username with any aliases.
*/
function alt_login_tokens_alter(&$replacements, array $context, $bubbleable_metadata) {
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
}
/**
* implements hook_user_presave().
* Implements hook_user_presave().
*/
function alt_login_user_presave(UserInterface $account) {
if ($account->isNew()) {
......@@ -197,6 +205,8 @@ function alt_login_validate_dedupe_aliases($form, $form_state) {
/**
* Utility
*
* Could be useful for exporting.
*
* @param UserInterface $user
* @param string $plugin_id
*
......
......@@ -2,7 +2,6 @@
namespace Drupal\alt_login\Authentication\Provider;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -14,15 +13,8 @@ class BasicAuth extends \Drupal\basic_auth\Authentication\Provider\BasicAuth {
* {@inheritdoc}
*/
public function authenticate(Request $request) {
$aliases = $this->configFactory->get('alt_login.settings')->get('login');
$user_id = $request->headers->get('PHP_AUTH_USER');
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());
}
$alias = $request->headers->get('PHP_AUTH_USER');
$request->headers->set('PHP_AUTH_USER', alt_login_convert_alias($alias));
return parent::authenticate($request);
}
......
......@@ -6,6 +6,8 @@ use Drupal\alt_login\AltLoginMethodInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
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.
......@@ -18,6 +20,8 @@ use Drupal\Core\Database\Database;
*/
class AddressName implements AltLoginMethodInterface {
use StringTranslationTrait;
/**
* The name of the address field on the user entity.
* @var string
......@@ -37,10 +41,12 @@ class AddressName implements AltLoginMethodInterface {
/**
* @param EmailValidator $entity_field_manager
* @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->database = $database;
$this->messenger = $messenger;
}
/**
......@@ -57,7 +63,8 @@ class AddressName implements AltLoginMethodInterface {
$plugin_id,
$plugin_definition,
$container->get('entity_field.manager'),
$container->get('database')
$container->get('database'),
$container->get('messenger')
);
}
......@@ -67,6 +74,9 @@ class AddressName implements AltLoginMethodInterface {
*/
function dedupeAlias(UserInterface $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);
if (!$user->isNew()) {
unset($uids[array_search($user->id(), $uids)]);
......@@ -99,7 +109,7 @@ class AddressName implements AltLoginMethodInterface {
*/
function getAlias(UserInterface $user){
$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