You are here

function ldap_servers_token_tokenize_user_account in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_servers/ldap_servers.tokens.inc \ldap_servers_token_tokenize_user_account()

Parameters

drupal user object $user_account:

array or 'all' $token_keys: 'all' signifies return all token/value pairs available; otherwise array lists token keys (e.g. property.name ...NOT [property.name])

string $pre: prefix of token.

string $post: suffix of token.

Return value

should return token/value pairs in array such as 'status' => 1 'uid' => 17

1 call to ldap_servers_token_tokenize_user_account()
ldap_servers_token_replace in ldap_servers/ldap_servers.tokens.inc
User_account.

File

ldap_servers/ldap_servers.tokens.inc, line 400
Collection of functions related to ldap tokens.

Code

function ldap_servers_token_tokenize_user_account($user_account, $token_keys = 'all', $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
  $detailed_watchdog_log = variable_get('ldap_help_watchdog_detail', 0);
  $tokens = [];
  $user_entered_password_available = (bool) ldap_user_ldap_provision_pwd('get');
  if ($token_keys == 'all') {

    // Add lowercase keyed entries to ldap array.
    foreach ((array) $user_account as $property_name => $value) {
      if (is_scalar($value) && $property_name != 'password') {
        $token_keys[] = 'property.' . $property_name;
        if (drupal_strtolower($property_name) != $property_name) {
          $token_keys[] = 'property.' . drupal_strtolower($property_name);
        }
      }
      elseif (isset($user_account->{$attr_name}[LANGUAGE_NONE][0]['value']) && is_scalar($user_account->{$attr_name}[LANGUAGE_NONE][0]['value'])) {
        $token_keys[] = 'field.' . $property_name;
        if (drupal_strtolower($property_name) != $property_name) {
          $token_keys[] = 'field.' . drupal_strtolower($property_name);
        }
      }
      else {

        // Field or property with no value, so no token can be generated.
      }
    }
    $ldap_user_conf_admin = new LdapUserConfAdmin();
    if ($ldap_user_conf_admin->setsLdapPassword) {
      $token_keys[] = 'password.random';
      $token_keys[] = 'password.user-random';
    }
  }
  foreach ($token_keys as $token_key) {
    $parts = explode('.', $token_key);
    $attr_type = $parts[0];
    $attr_name = $parts[1];
    $attr_conversion = isset($parts[2]) ? $parts[2] : 'none';
    $value = FALSE;
    $skip = FALSE;
    switch ($attr_type) {
      case 'field':
        $value = @is_scalar($user_account->{$attr_name}[LANGUAGE_NONE][0]['value']) ? $user_account->{$attr_name}[LANGUAGE_NONE][0]['value'] : '';
        break;
      case 'property':
        $value = @is_scalar($user_account->{$attr_name}) ? $user_account->{$attr_name} : '';
        break;
      case 'password':
        switch ($attr_name) {
          case 'user':
          case 'user-only':
            $pwd = ldap_user_ldap_provision_pwd('get');
            $value = $pwd ? $pwd : NULL;
            break;
          case 'user-random':
            $pwd = ldap_user_ldap_provision_pwd('get');
            $value = $pwd ? $pwd : user_password();
            break;
          case 'random':
            $value = user_password();
            break;
        }
        if (empty($value)) {
          $skip = TRUE;
        }
        break;
    }
    if (!$skip) {
      switch ($attr_conversion) {
        case 'none':
          break;
        case 'to-md5':
          $value = "{MD5}" . base64_encode(pack("H*", md5($value)));
          break;
        case 'to-lowercase':
          $value = drupal_strtolower($value);
          break;
      }
      $tokens[$pre . $token_key . $post] = $attr_type == 'password' ? $value : check_plain($value);
      if ($token_key != drupal_strtolower($token_key)) {
        $tokens[$pre . drupal_strtolower($token_key) . $post] = $attr_type == 'password' ? $value : check_plain($value);
      }
    }
  }
  return $tokens;
}