You are here

function ldap_user_ldap_attributes_needed_alter in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_user/ldap_user.module \ldap_user_ldap_attributes_needed_alter()
  2. 8.3 ldap_user/ldap_user.module \ldap_user_ldap_attributes_needed_alter()

Implements hook_ldap_attributes_needed_alter().

File

ldap_user/ldap_user.module, line 195
Module for the LDAP User Entity.

Code

function ldap_user_ldap_attributes_needed_alter(&$attributes, $params) {

  // Puid attributes are server specific.
  if (isset($params['sid']) && $params['sid']) {
    if (is_scalar($params['sid'])) {
      $ldap_server = ldap_servers_get_servers($params['sid'], 'enabled', TRUE);
    }
    else {
      $ldap_server = $params['sid'];
    }

    // Failed to find enabled server.
    if ($ldap_server === FALSE) {
      return;
    }
    $ldap_user_conf = ldap_user_conf();
    if (!isset($attributes['dn'])) {
      $attributes['dn'] = [];
    }

    // Force dn "attribute" to exist.
    $attributes['dn'] = ldap_servers_set_attribute_map($attributes['dn']);

    // Add the attributes required by the user configuration when provisioning drupal users.
    switch ($params['ldap_context']) {
      case 'ldap_user_insert_drupal_user':
      case 'ldap_user_update_drupal_user':
      case 'ldap_user_ldap_associate':
      case 'all':
        $attributes[$ldap_server->user_attr] = ldap_servers_set_attribute_map(@$attributes[$ldap_server->user_attr]);
        $attributes[$ldap_server->mail_attr] = ldap_servers_set_attribute_map(@$attributes[$ldap_server->mail_attr]);
        if ($ldap_server->picture_attr) {
          $attributes[$ldap_server->picture_attr] = ldap_servers_set_attribute_map(@$attributes[$ldap_server->picture_attr]);
        }
        if ($ldap_server->unique_persistent_attr) {
          $attributes[$ldap_server->unique_persistent_attr] = ldap_servers_set_attribute_map(@$attributes[$ldap_server->unique_persistent_attr]);
        }
        if ($ldap_server->mail_template) {
          ldap_servers_token_extract_attributes($attributes, $ldap_server->mail_template);
        }
        break;
    }
    $ldap_context = empty($params['ldap_context']) ? NULL : $params['ldap_context'];
    $direction = empty($params['direction']) ? $ldap_user_conf
      ->ldapContextToProvDirection($ldap_context) : $params['direction'];
    $attributes_required_by_user_module_mappings = $ldap_user_conf
      ->getLdapUserRequiredAttributes($direction, $ldap_context);
    $attributes = array_merge($attributes_required_by_user_module_mappings, $attributes);
  }
}