From c72112cfde6914e8066db540d24e33eb6e1bf69d Mon Sep 17 00:00:00 2001
From: Matthew Slater <matslats@fastmail.com>
Date: Wed, 31 Oct 2018 17:29:04 +1100
Subject: [PATCH] tidy up

---
 alt_login.module                          | 38 ++++++++++++++---------
 src/Authentication/Provider/BasicAuth.php | 12 ++-----
 src/Plugin/AltLoginMethod/AddressName.php | 16 ++++++++--
 3 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/alt_login.module b/alt_login.module
index 3e02f4b..4a456d6 100644
--- a/alt_login.module
+++ b/alt_login.module
@@ -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
  *
diff --git a/src/Authentication/Provider/BasicAuth.php b/src/Authentication/Provider/BasicAuth.php
index db5ce3e..c3ef3a7 100644
--- a/src/Authentication/Provider/BasicAuth.php
+++ b/src/Authentication/Provider/BasicAuth.php
@@ -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);
   }
 
diff --git a/src/Plugin/AltLoginMethod/AddressName.php b/src/Plugin/AltLoginMethod/AddressName.php
index 98158c5..d5022e3 100644
--- a/src/Plugin/AltLoginMethod/AddressName.php
+++ b/src/Plugin/AltLoginMethod/AddressName.php
@@ -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]);
   }
 
 
-- 
GitLab