From c683f02a29ffb63a0d76d363d12592badbf0b9e4 Mon Sep 17 00:00:00 2001
From: Richard Kalinec <rkalinec@gmail.com>
Date: Sat, 4 Feb 2017 23:26:56 +0100
Subject: [PATCH] Match existing users also when creating accounts for unknown
 users is disabled

Moved the check for webserver_auth_create_user (which is FALSE by
default) from before checking if just user record in authmap table is
missing and matching an existing user depending on the value of
webserver_auth_match_existing (which is TRUE by default) after these
tasks right before creating the account for an unknown user depending on
the value of webserver_auth_create_user. This allows adding a record to
the authmap table for an existing user to authenticate using this module
without the need for enabling creating completely new accounts for
unknown users. The present state was inevitably resulting in the
administrator and all existing users being locked out of Drupal site
upon enabling the module, what was contrary to the intention of the
developers, as is clearly stated by the comment above the check for the
value of webserver_auth_match_existing.
---
 webserver_auth.module | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/webserver_auth.module b/webserver_auth.module
index a7f5ce7..17f3a31 100644
--- a/webserver_auth.module
+++ b/webserver_auth.module
@@ -165,12 +165,6 @@ function _webserver_auth_route() {
     return;
   }
 
-  // Check if we should create accounts for unknown users
-  if (! variable_get('webserver_auth_create_user', FALSE)) {
-    watchdog('webserver_auth', 'user @authname does not exist in the database or is not included in the authmap table for webserver_auth, and the create user option is disabled.', array('@authname' => $authname));
-    return;
-  }
-
   // First check if just authmap is missing
   $account = user_load_by_name($authname);
   if ($account) {
@@ -197,6 +191,12 @@ function _webserver_auth_route() {
     }
     return;
   }
+  
+  // Check if we should create accounts for unknown users
+  if (! variable_get('webserver_auth_create_user', FALSE)) {
+    watchdog('webserver_auth', 'user @authname does not exist in the database or is not included in the authmap table for webserver_auth, and the create user option is disabled.', array('@authname' => $authname));
+    return;
+  }
 
   /* We didn't find the user so we create an account for them.
      Note, hook_user_presave() is called during this process.
