You are here

public function DrupalUserProcessor::provisionDrupalAccount in Lightweight Directory Access Protocol (LDAP) 8.3

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 $userData: A keyed array normally containing 'name' and optionally more.

Return value

bool|\Drupal\user\Entity\User Return the user on success or FALSE on any problem.

File

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

Class

DrupalUserProcessor
Handles processing of a user from LDAP to Drupal.

Namespace

Drupal\ldap_user\Processor

Code

public function provisionDrupalAccount(array $userData) {
  $this->account = User::create($userData);
  $ldapUser = FALSE;

  // Get an LDAP user from the LDAP server.
  if ($this->config
    ->get('drupalAcctProvisionServer')) {
    $ldapUser = $this->factory
      ->getUserDataFromServerByIdentifier($userData['name'], $this->config
      ->get('drupalAcctProvisionServer'));
  }

  // Still no LDAP user.
  if (!$ldapUser) {
    \Drupal::service('ldap.detail_log')
      ->log('@username: Failed to find associated LDAP entry for username in provision.', [
      '@username' => $userData['name'],
    ], 'ldap-user');
    return FALSE;
  }
  $this->server = $this->factory
    ->getServerByIdEnabled($this->config
    ->get('drupalAcctProvisionServer'));

  // If we don't have an account name already we should set one.
  if (!$this->account
    ->getAccountName()) {
    $this->account
      ->set('name', $ldapUser[$this->server
      ->get('user_attr')]);
  }

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

  // Look for existing Drupal account with the same PUID. If found, update
  // that user instead of creating a new user.
  $persistentUid = $this->server
    ->userPuidFromLdapEntry($ldapUser['attr']);
  $accountFromPuid = $persistentUid ? $this->server
    ->userAccountFromPuid($persistentUid) : FALSE;
  if ($accountFromPuid) {
    $this
      ->updateExistingAccountByPersistentUid($ldapUser, $accountFromPuid);
  }
  else {
    if (!$this
      ->createDrupalUser($ldapUser)) {
      return FALSE;
    }
  }
  return TRUE;
}