You are here

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

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

Create a Drupal user.

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

File

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

Class

DrupalUserProcessor
Handles processing of a user from LDAP to Drupal.

Namespace

Drupal\ldap_user\Processor

Code

private function createDrupalUser() : void {
  $this->account
    ->enforceIsNew();
  $this
    ->applyAttributesToAccountOnCreate();
  $tokens = [
    '%drupal_username' => $this->account
      ->getAccountName(),
  ];
  if (empty($this->account
    ->getAccountName())) {
    $this->messenger
      ->addError($this
      ->t('User account creation failed because of invalid, empty derived Drupal username.'));
    $this->logger
      ->error('Failed to create Drupal account %drupal_username because Drupal username could not be derived.', $tokens);
    return;
  }
  if (!($mail = $this->account
    ->getEmail())) {
    $this->messenger
      ->addError($this
      ->t('User account creation failed because of invalid, empty derived email address.'));
    $this->logger
      ->error('Failed to create Drupal account %drupal_username because email address could not be derived by LDAP User module', $tokens);
    return;
  }
  $users = $this->entityTypeManager
    ->getStorage('user')
    ->loadByProperties([
    'mail' => $mail,
  ]);
  $account_with_same_email = $users ? reset($users) : FALSE;
  if ($account_with_same_email) {
    $this->logger
      ->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(),
    ]);
    $this->messenger
      ->addError($this
      ->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.'));
    return;
  }
  $this
    ->saveAccount();
  $this->externalAuth
    ->save($this->account, 'ldap_user', $this->account
    ->getAccountName());
}