You are here

public function DrupalUserProcessor::createDrupalUserFromLdapEntry in Lightweight Directory Access Protocol (LDAP) 8.4

Provision a Drupal user account.

Given user data, create a user and apply LDAP attributes or assign to correct user if name has changed through PUID.

Parameters

array $user_data: A keyed array normally containing 'name' and optionally more.

Return value

bool Whether creation was a success.

File

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

Class

DrupalUserProcessor
Handles processing of a user from LDAP to Drupal.

Namespace

Drupal\ldap_user\Processor

Code

public function createDrupalUserFromLdapEntry(array $user_data) : bool {
  $this->account = $this->entityTypeManager
    ->getStorage('user')
    ->create($user_data);
  $this->server = $this->entityTypeManager
    ->getStorage('ldap_server')
    ->load($this->config
    ->get('drupalAcctProvisionServer'));

  // Get an LDAP user from the LDAP server.
  if ($this->config
    ->get('drupalAcctProvisionServer')) {
    $this->ldapUserManager
      ->setServer($this->server);
    $this->ldapEntry = $this->ldapUserManager
      ->getUserDataByIdentifier($this->account
      ->getAccountName());
  }
  if (!$this->ldapEntry) {
    $this->detailLog
      ->log('@username: Failed to find associated LDAP entry for username in provision.', [
      '@username' => $this->account
        ->getAccountName(),
    ], 'ldap-user');
    return FALSE;
  }

  // Can we get details from an LDAP server?
  $params = [
    'account' => $this->account,
    'prov_event' => self::EVENT_CREATE_DRUPAL_USER,
    'module' => 'ldap_user',
    'function' => 'createDrupalUserFromLdapEntry',
    'direction' => self::PROVISION_TO_DRUPAL,
  ];
  $this->moduleHandler
    ->alter('ldap_entry', $this->ldapEntry, $params);

  // Look for existing Drupal account with the same PUID. If found, update
  // that user instead of creating a new user.
  $persistentUid = $this->server
    ->derivePuidFromLdapResponse($this->ldapEntry);
  $accountFromPuid = !empty($persistentUid) ? $this->ldapUserManager
    ->getUserAccountFromPuid($persistentUid) : FALSE;
  if ($accountFromPuid) {
    $this
      ->updateExistingAccountByPersistentUid($accountFromPuid);
  }
  else {
    $this
      ->createDrupalUser();
  }
  return TRUE;
}