--- SimplesamlphpDrupalAuth.php.orig 2016-12-09 12:15:22.000000000 -0700 +++ SimplesamlphpDrupalAuth.php 2016-12-09 12:15:22.000000000 -0700 @@ -126,24 +126,22 @@ public function externalRegister($authname) { $account = FALSE; - // First we check the admin settings for simpleSAMLphp and find out if we - // are allowed to register users. - if (!$this->config->get('register_users')) { - - // We're not allowed to register new users on the site through simpleSAML. - // We let the user know about this and redirect to the user/login page. - drupal_set_message(t("We are sorry. While you have successfully authenticated, you are not yet entitled to access this site. Please ask the site administrator to provision access for you.")); - $this->simplesamlAuth->logout(base_path()); - - return FALSE; - } - // It's possible that a user with their username set to this authname // already exists in the Drupal database. $existing_user = $this->entityManager->getStorage('user')->loadByProperties(array('name' => $authname)); + // If we did not find an account, take more action to find an existing account from other modules. + if(!$existing_user) { + $attributes = $this->simplesamlAuth->getAttributes(); + foreach (\Drupal::moduleHandler()->getImplementations('simplesamlphp_auth_existing_user') as $module) { + $return_value = \Drupal::moduleHandler()->invoke($module, 'simplesamlphp_auth_existing_user', [$attributes]); + if ($return_value instanceof UserInterface) { + $existing_user = $return_value; + } + } + } $existing_user = $existing_user ? reset($existing_user) : FALSE; if ($existing_user) { - // If auto-enable SAML is activated, link this user to SAML. + //First lets see if we are allowed to link existing accounts. If we are we can determine if one exists. if ($this->config->get('autoenablesaml')) { if ($this->config->get('debug')) { $this->logger->debug('Linking authname %authname to existing Drupal user with ID %id because "Automatically enable SAML authentication for existing users upon successful login" setting is activated.', array( @@ -156,7 +154,7 @@ } else { if ($this->config->get('debug')) { - $this->logger->debug('A local Drupal user with username %authname already exists. Aborting the creation of a SAML-enabled Drupal user.', array( + $this->logger->debug('A local Drupal user with username %authname already exists. To link the accounts, enable the option to automatically enable SAML login for existing accounts.', array( '%authname' => $authname, )); } @@ -166,43 +164,31 @@ $this->simplesamlAuth->logout(base_path()); return FALSE; } + //If successful linking sync attributes + if ($account) { + $this->synchronizeUserAttributes($account); + } + } else { - // If auto-enable SAML is activated, take more action to find an existing - // user. - if ($this->config->get('autoenablesaml')) { - // Allow other modules to decide if there is an existing Drupal user, - // based on the supplied SAML atttributes. - $attributes = $this->simplesamlAuth->getAttributes(); - foreach (\Drupal::moduleHandler()->getImplementations('simplesamlphp_auth_existing_user') as $module) { - $return_value = \Drupal::moduleHandler()->invoke($module, 'simplesamlphp_auth_existing_user', [$attributes]); - if ($return_value instanceof UserInterface) { - $account = $return_value; - if ($this->config->get('debug')) { - $this->logger->debug('Linking authname %authname to existing Drupal user with ID %id because "Automatically enable SAML authentication for existing users upon successful login" setting is activated.', array( - '%authname' => $authname, - '%id' => $account->id(), - )); - } - $this->externalauth->linkExistingAccount($authname, 'simplesamlphp_auth', $account); - } + if ($this->config->get('register_users')) { + // Try to create the new user. + try { + $account = $this->externalauth->register($authname, 'simplesamlphp_auth'); + } + //If creating new user fails set error + catch (\Exception $ex) { + watchdog_exception('simplesamlphp_auth', $ex); + drupal_set_message(t('Error registering user: An account with this username already exists.'), 'error'); + } + //If successful creation sync attributes + if ($account) { + $this->synchronizeUserAttributes($account, TRUE); } } } - - if (!$account) { - // Create the new user. - try { - $account = $this->externalauth->register($authname, 'simplesamlphp_auth'); - } - catch (\Exception $ex) { - watchdog_exception('simplesamlphp_auth', $ex); - drupal_set_message(t('Error registering user: An account with this username already exists.'), 'error'); - } - } - + //If we found a user login the user. if ($account) { - $this->synchronizeUserAttributes($account, TRUE); return $this->externalauth->userLoginFinalize($account, $authname, 'simplesamlphp_auth'); } }