You are here

public function DrupalUserProcessor::ldapAssociateDrupalAccount 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::ldapAssociateDrupalAccount()

Set LDAP associations of a Drupal account by altering user fields.

Parameters

string $drupalUsername: The Drupal username.

Return value

bool Returns FALSE on invalid user or LDAP accounts.

File

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

Class

DrupalUserProcessor
Handles processing of a user from LDAP to Drupal.

Namespace

Drupal\ldap_user\Processor

Code

public function ldapAssociateDrupalAccount($drupalUsername) {
  if ($this->config
    ->get('drupalAcctProvisionServer')) {
    $ldapServer = $this->factory
      ->getServerByIdEnabled($this->config
      ->get('drupalAcctProvisionServer'));
    $this->account = user_load_by_name($drupalUsername);
    if (!$this->account) {
      \Drupal::logger('ldap_user')
        ->error('Failed to LDAP associate Drupal account %drupal_username because account not found', [
        '%drupal_username' => $drupalUsername,
      ]);
      return FALSE;
    }
    $ldap_user = $ldapServer
      ->matchUsernameToExistingLdapEntry($drupalUsername);
    if (!$ldap_user) {
      \Drupal::logger('ldap_user')
        ->error('Failed to LDAP associate Drupal account %drupal_username because corresponding LDAP entry not found', [
        '%drupal_username' => $drupalUsername,
      ]);
      return FALSE;
    }
    $persistentUid = $ldapServer
      ->userPuidFromLdapEntry($ldap_user['attr']);
    if ($persistentUid) {
      $this->account
        ->set('ldap_user_puid', $persistentUid);
    }
    $this->account
      ->set('ldap_user_puid_property', $ldapServer
      ->get('unique_persistent_attr'));
    $this->account
      ->set('ldap_user_puid_sid', $ldapServer
      ->id());
    $this->account
      ->set('ldap_user_current_dn', $ldap_user['dn']);
    $this->account
      ->set('ldap_user_last_checked', time());
    $this->account
      ->set('ldap_user_ldap_exclude', 0);
    $this
      ->saveAccount();
    $this
      ->syncToDrupalAccount(self::EVENT_CREATE_DRUPAL_USER, $ldap_user);
    return TRUE;
  }
  else {
    return FALSE;
  }
}