/** * @param ldap entry array $ldap_entry * * @return string user's mail value or FALSE if none present */ public function userEmailFromLdapEntry($ldap_entry) { if ($ldap_entry && $this->mail_attr) { // not using template $mail = isset($ldap_entry[$this->mail_attr][0]) ? $ldap_entry[$this->mail_attr][0] : FALSE; return $mail; } elseif ($ldap_entry && $this->mail_template) { // template is of form [cn]@illinois.edu ldap_servers_module_load_include('inc', 'ldap_servers', 'ldap_servers.functions'); return ldap_servers_token_replace($ldap_entry, $this->mail_template, 'ldap_entry'); } else { return FALSE; } }
/** populate $user edit array (used in hook_user_save, hook_user_update, etc) * ... should not assume all attribues are present in ldap entry * * @param array ldap entry $ldap_user * @param object $ldap_server * @param array $edit see hook_user_save, hook_user_update, etc * @param drupal account object $account * @param string $op see hook_ldap_attributes_needed_alter */ 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; } } if ($this->isSynched('[property.name]', $prov_events, $direction) && !isset($edit['name'])) { $name = $ldap_server->userUsernameFromLdapEntry($ldap_user['attr']); if ($name) { $edit['name'] = $name; } } 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) { // //dpm('field detail');//dpm($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); }