You are here

function LdapUserConf::ldapAssociateDrupalAccount in Lightweight Directory Access Protocol (LDAP) 8.2

Same name and namespace in other branches
  1. 7.2 ldap_user/LdapUserConf.class.php \LdapUserConf::ldapAssociateDrupalAccount()

set ldap associations of a drupal account by altering user fields

Parameters

string $drupal_username:

Return value

boolean TRUE on success, FALSE on error or failure because of invalid user or ldap accounts

File

ldap_user/LdapUserConf.class.php, line 1118

Class

LdapUserConf

Code

function ldapAssociateDrupalAccount($drupal_username) {
  if ($this->drupalAcctProvisionServer) {
    $prov_events = array(
      LDAP_USER_EVENT_LDAP_ASSOCIATE_DRUPAL_ACCT,
    );
    $ldap_server = ldap_servers_get_servers($this->drupalAcctProvisionServer, 'enabled', TRUE);

    // $ldap_user['sid']
    $account = user_load_by_name($drupal_username);
    $ldap_user = ldap_servers_get_user_ldap_data($drupal_username, $this->drupalAcctProvisionServer, 'ldap_user_prov_to_drupal');
    if (!$account) {
      watchdog('ldap_user', 'Failed to LDAP associate drupal account %drupal_username because account not found', array(
        '%drupal_username' => $drupal_username,
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    elseif (!$ldap_user) {
      watchdog('ldap_user', 'Failed to LDAP associate drupal account %drupal_username because corresponding LDAP entry not found', array(
        '%drupal_username' => $drupal_username,
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    else {
      $user_edit = array();
      $user_edit['data']['ldap_user']['init'] = array(
        'sid' => $ldap_user['sid'],
        'dn' => $ldap_user['dn'],
        'mail' => $account->mail,
      );
      $ldap_user_puid = $ldap_server
        ->userPuidFromLdapEntry($ldap_user['attr']);
      if ($ldap_user_puid) {
        $user_edit['ldap_user_puid'][LANGUAGE_NONE][0]['value'] = $ldap_user_puid;

        //
      }
      $user_edit['ldap_user_puid_property'][LANGUAGE_NONE][0]['value'] = $ldap_server->unique_persistent_attr;
      $user_edit['ldap_user_puid_sid'][LANGUAGE_NONE][0]['value'] = $ldap_server->sid;
      $user_edit['ldap_user_current_dn'][LANGUAGE_NONE][0]['value'] = $ldap_user['dn'];
      $account = user_save($account, $user_edit, 'ldap_user');
      return (bool) $account;
    }
  }
  else {
    return FALSE;
  }
}