You are here

function cas_attributes_map_fields in CAS Attributes 7

Map fields to the pre-defined CAS and LDAP tokens.

1 call to cas_attributes_map_fields()
cas_attributes_cas_user_presave in ./cas_attributes.module
Implements hook_cas_user_presave().

File

./cas_attributes.module, line 61
Allows user account and profile attributes to be automatically populated using tokens. Provides basic tokens for attributes returned by the CAS server.

Code

function cas_attributes_map_fields(&$edit, $account) {
  $data = array(
    'cas' => $edit['cas_user']['name'],
  );

  // Set each drupal field to its mapped attribute.
  $overwrite = variable_get('cas_attributes_overwrite', TRUE);
  foreach (variable_get('cas_attributes_relations', array()) as $drupal_field => $text) {
    $result = trim(token_replace($text, $data, array(
      'clear' => TRUE,
    )));
    $result = html_entity_decode($result);

    // Only update the fields if there is data to set.
    if (!empty($result)) {
      if ($drupal_field == 'name' || $drupal_field == 'mail') {

        // Only update field if overwrite setting is on, or if it is empty.
        if ($overwrite || empty($account->{$drupal_field})) {
          $edit[$drupal_field] = $result;
        }
      }
      else {

        // Only update field if overwrite setting is on, or if it is empty.
        if ($overwrite || empty($account->{$drupal_field})) {
          $edit[$drupal_field][LANGUAGE_NONE][0]['value'] = $result;
        }
      }
    }
  }
}