You are here

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

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

populate $user edit array (used in hook_user_save, hook_user_update, etc) ... should not assume all attribues are present in ldap entry

Parameters

array ldap entry $ldap_user:

array $edit see hook_user_save, hook_user_update, etc:

object $ldap_server:

enum $direction:

array $prov_events:

2 calls to LdapUserConf::entryToUserEdit()
LdapUserConf::provisionDrupalAccount in ldap_user/LdapUserConf.class.php
given a drupal account, query ldap and get all user fields and save user account (note: parameters are in odd order to match synchDrupalAccount handle)
LdapUserConf::synchToDrupalAccount in ldap_user/LdapUserConf.class.php
given a drupal account, query ldap and get all user fields and create user account

File

ldap_user/LdapUserConf.class.php, line 1177

Class

LdapUserConf

Code

function entryToUserEdit($ldap_user, &$edit, $ldap_server, $direction = LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, $prov_events = NULL) {

  // need array of user fields and which direction and when they should be synched.
  if (!$prov_events) {
    $prov_events = ldap_user_all_events();
  }
  $mail_synched = $this
    ->isSynched('[property.mail]', $prov_events, $direction);
  if (!isset($edit['mail']) && $mail_synched) {
    $derived_mail = $ldap_server
      ->userEmailFromLdapEntry($ldap_user['attr']);
    if ($derived_mail) {
      $edit['mail'] = $derived_mail;
    }
  }
  $drupal_username = $ldap_server
    ->userUsernameFromLdapEntry($ldap_user['attr']);
  if ($this
    ->isSynched('[property.picture]', $prov_events, $direction)) {
    $picture = $ldap_server
      ->userPictureFromLdapEntry($ldap_user['attr'], $drupal_username);
    if ($picture) {
      $edit['picture'] = $picture;
      if (isset($picture->md5Sum)) {
        $edit['data']['ldap_user']['init']['thumb5md'] = $picture->md5Sum;
      }
    }
  }
  if ($this
    ->isSynched('[property.name]', $prov_events, $direction) && !isset($edit['name']) && $drupal_username) {
    $edit['name'] = $drupal_username;
  }
  if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER && in_array(LDAP_USER_EVENT_CREATE_DRUPAL_USER, $prov_events)) {
    $edit['mail'] = isset($edit['mail']) ? $edit['mail'] : $ldap_user['mail'];
    $edit['pass'] = isset($edit['pass']) ? $edit['pass'] : user_password(20);
    $edit['init'] = isset($edit['init']) ? $edit['init'] : $edit['mail'];
    $edit['status'] = isset($edit['status']) ? $edit['status'] : 1;
    $edit['signature'] = isset($edit['signature']) ? $edit['signature'] : '';
    $edit['data']['ldap_user']['init'] = array(
      'sid' => $ldap_user['sid'],
      'dn' => $ldap_user['dn'],
      'mail' => $edit['mail'],
    );
  }

  /**
   * basic $user ldap fields
   */
  if ($this
    ->isSynched('[field.ldap_user_puid]', $prov_events, $direction)) {
    $ldap_user_puid = $ldap_server
      ->userPuidFromLdapEntry($ldap_user['attr']);
    if ($ldap_user_puid) {
      $edit['ldap_user_puid'][LANGUAGE_NONE][0]['value'] = $ldap_user_puid;

      //
    }
  }
  if ($this
    ->isSynched('[field.ldap_user_puid_property]', $prov_events, $direction)) {
    $edit['ldap_user_puid_property'][LANGUAGE_NONE][0]['value'] = $ldap_server->unique_persistent_attr;
  }
  if ($this
    ->isSynched('[field.ldap_user_puid_sid]', $prov_events, $direction)) {
    $edit['ldap_user_puid_sid'][LANGUAGE_NONE][0]['value'] = $ldap_server->sid;
  }
  if ($this
    ->isSynched('[field.ldap_user_current_dn]', $prov_events, $direction)) {
    $edit['ldap_user_current_dn'][LANGUAGE_NONE][0]['value'] = $ldap_user['dn'];
  }

  // Get any additional mappings.
  $mappings = $this
    ->getSynchMappings($direction, $prov_events);

  // Loop over the mappings.
  foreach ($mappings as $user_attr_key => $field_detail) {

    // Make sure this mapping is relevant to the sync context.
    if (!$this
      ->isSynched($user_attr_key, $prov_events, $direction)) {
      continue;
    }

    /**
     * if "convert from binary is selected" and no particular method is in token,
     * default to ldap_servers_binary() function
     */
    if ($field_detail['convert'] && strpos($field_detail['ldap_attr'], ';') === FALSE) {
      $field_detail['ldap_attr'] = str_replace(']', ';binary]', $field_detail['ldap_attr']);
    }
    $value = ldap_servers_token_replace($ldap_user['attr'], $field_detail['ldap_attr'], 'ldap_entry');
    list($value_type, $value_name, $value_instance) = ldap_servers_parse_user_attr_name($user_attr_key);

    // $value_instance not used, may have future use case
    // Are we dealing with a field?
    if ($value_type == 'field') {

      // Field api field - first we get the field.
      $field = field_info_field($value_name);

      // Then the columns for the field in the schema.
      $columns = array_keys($field['columns']);

      // Then we convert the value into an array if it's scalar.
      $values = $field['cardinality'] == 1 ? array(
        $value,
      ) : (array) $value;
      $items = array();

      // Loop over the values and set them in our $items array.
      foreach ($values as $delta => $value) {
        if (isset($value)) {

          // We set the first column value only, this is consistent with
          // the Entity Api (@see entity_metadata_field_property_set).
          $items[$delta][$columns[0]] = $value;
        }
      }

      // Add them to our edited item.
      $edit[$value_name][LANGUAGE_NONE] = $items;
    }
    elseif ($value_type == 'property') {

      // Straight property.
      $edit[$value_name] = $value;
    }
  }

  // Allow other modules to have a say.
  drupal_alter('ldap_user_edit_user', $edit, $ldap_user, $ldap_server, $prov_events);
  if (isset($edit['name']) && $edit['name'] == '') {

    // don't let empty 'name' value pass for user
    unset($edit['name']);
  }
}