You are here

private function DrupalUserProcessor::createDrupalUser in Lightweight Directory Access Protocol (LDAP) 8.3

Same name and namespace in other branches
  1. 8.4 ldap_user/src/Processor/DrupalUserProcessor.php \Drupal\ldap_user\Processor\DrupalUserProcessor::createDrupalUser()

Create a Drupal user.

Parameters

array $ldap_user: The LDAP user.

1 call to DrupalUserProcessor::createDrupalUser()
DrupalUserProcessor::provisionDrupalAccount in ldap_user/src/Processor/DrupalUserProcessor.php
Provision a Drupal user account.

File

ldap_user/src/Processor/DrupalUserProcessor.php, line 648

Class

DrupalUserProcessor
Handles processing of a user from LDAP to Drupal.

Namespace

Drupal\ldap_user\Processor

Code

private function createDrupalUser(array $ldap_user) {
  $this->account
    ->enforceIsNew();
  $this
    ->applyAttributesToAccount($ldap_user, self::PROVISION_TO_DRUPAL, [
    self::EVENT_CREATE_DRUPAL_USER,
  ]);
  $tokens = [
    '%drupal_username' => $this->account
      ->getAccountName(),
  ];
  if (empty($this->account
    ->getAccountName())) {
    drupal_set_message(t('User account creation failed because of invalid, empty derived Drupal username.'), 'error');
    \Drupal::logger('ldap_user')
      ->error('Failed to create Drupal account %drupal_username because Drupal username could not be derived.', $tokens);
    return FALSE;
  }
  if (!($mail = $this->account
    ->getEmail())) {
    drupal_set_message(t('User account creation failed because of invalid, empty derived email address.'), 'error');
    \Drupal::logger('ldap_user')
      ->error('Failed to create Drupal account %drupal_username because email address could not be derived by LDAP User module', $tokens);
    return FALSE;
  }
  if ($account_with_same_email = user_load_by_mail($mail)) {
    \Drupal::logger('ldap_user')
      ->error('LDAP user %drupal_username has email address (%email) conflict with a Drupal user %duplicate_name', [
      '%drupal_username' => $this->account
        ->getAccountName(),
      '%email' => $mail,
      '%duplicate_name' => $account_with_same_email
        ->getAccountName(),
    ]);
    drupal_set_message(t('Another user already exists in the system with the same email address. You should contact the system administrator in order to solve this conflict.'), 'error');
    return FALSE;
  }
  $this
    ->saveAccount();
  if (!$this->account) {
    drupal_set_message(t('User account creation failed because of system problems.'), 'error');
  }
  else {
    ExternalAuthenticationHelper::setUserIdentifier($this->account, $this->account
      ->getAccountName());
    return TRUE;
  }
}