You are here

public function SimpleLdapUserMap::mapFromLdapToDrupal in Simple LDAP 7

Map from LDAP to Drupal.

Parameters

SimpleLdapUser $ldap_user: The LDAP user.

Array $edit: The edit array, as passed to user_save().

stdClass $drupal_user: The Drupal user account object.

File

simple_ldap_user/SimpleLdapUserMap.class.php, line 131
Class defining the LDAP <-> Drupal user field mappings.

Class

SimpleLdapUserMap
@file Class defining the LDAP <-> Drupal user field mappings.

Code

public function mapFromLdapToDrupal(SimpleLdapUser $ldap_user, array &$edit, stdClass $drupal_user) {

  // Mail is a special attribute.
  $attribute_mail = simple_ldap_user_variable_get('simple_ldap_user_attribute_mail');
  if (isset($ldap_user->{$attribute_mail}[0]) && (!empty($drupal_user->is_new) || $drupal_user->mail != $ldap_user->{$attribute_mail}[0])) {
    $edit['mail'] = $ldap_user->{$attribute_mail}[0];
  }

  // Synchronize the fields in the attribute map.
  foreach ($this->map as $attribute) {

    // Skip drupal-to-ldap many-to-one mappings.
    if (count($attribute['drupal']) > 1) {
      continue;
    }

    // Get the drupal field name and type.
    $drupal_attribute = reset($attribute['drupal']);
    $ldap_attribute = $ldap_user->{$attribute['ldap']};

    // If no records were found in LDAP, continue.
    if (!isset($ldap_attribute['count']) || $ldap_attribute['count'] == 0) {
      continue;
    }

    // Skip this field if it couldn't be parsed.
    if (!($parsed = $this
      ->parseDrupalAttribute($drupal_attribute))) {
      continue;
    }
    list($is_field, $field_name) = $parsed;

    // To avoid notices, set the value to NULL. This would most often be the
    // case on inserts.
    if (!isset($drupal_user->{$field_name})) {
      $drupal_user->{$field_name} = NULL;
    }

    // If this is a Field API field, store in appropriate array structure.
    if ($is_field) {
      $field_info = field_info_field($field_name);
      $check_cardinality = $field_info['cardinality'] != FIELD_CARDINALITY_UNLIMITED;
      $language = field_language('user', $drupal_user, $field_name);

      // Determine the columns for this field.
      $columns = $this
        ->drupalAttributeColumns($drupal_attribute);

      // Iterate over each LDAP value.
      for ($i = 0; $i < $ldap_attribute['count']; $i++) {

        // If cardinality has been reached, break.
        if ($check_cardinality && $i >= $field_info['cardinality']) {
          break;
        }

        // Don't mess with $columns, so it stays consistent for each value.
        $parents = $columns;
        array_unshift($parents, $field_name, $language, $i);
        drupal_array_set_nested_value($edit, $parents, $ldap_attribute[$i]);
      }
    }
    else {
      $edit[$field_name] = $ldap_attribute[0];
    }
  }
}