Projet

Général

Profil

Paste
Télécharger (1,49 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / cas_attributes / cas_ldap.tokens.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Token module integration.
6
 */
7

    
8
/**
9
 * Implements hook_token_info_alter().
10
 */
11
function cas_ldap_token_info_alter(&$data) {
12
  $data['tokens']['cas']['ldap'] = array(
13
    'name' => t('LDAP'),
14
    'description' => t('An LDAP attribute of the CAS user. <a href="@url">Available tokens</a>.', array('@url' => url('admin/config/people/cas/attributes/ldap'))),
15
    'dynamic' => TRUE,
16
  );
17
}
18

    
19
/**
20
 * Implements hook_tokens().
21
 */
22
function cas_ldap_tokens($type, $tokens, array $data = array(), array $options = array()) {
23
  $sanitize = !empty($options['sanitize']);
24
  $replacements = array();
25

    
26
  if ($type == 'cas' && !empty($data['cas'])) {
27
    $cas = $data['cas'];
28

    
29
    // Provide [cas:attribute:?] dynamic tokens.
30
    if ($attribute_tokens = token_find_with_prefix($tokens, 'ldap')) {
31
      $attribute = cas_ldap_attributes($cas);
32
      foreach ($attribute_tokens as $name => $original) {
33
        $name = drupal_strtolower($name);
34
        if (isset($attribute[$name])) {
35
          $value = $attribute[$name];
36
          if (is_array($value)) {
37
            $value = $value[0];
38
          }
39
          $replacements[$original] = $sanitize ? check_plain($value) : $value;
40
        }
41
        elseif ($name == '?') {
42
          $keys = array_keys($attribute);
43
          if ($sanitize) {
44
            $keys = array_map('check_plain', $keys);
45
          }
46
          $replacements[$original] = t('Available attributes: %keys', array('%keys' => implode(', ', $keys)));
47
        }
48
      }
49
    }
50
  }
51

    
52
  return $replacements;
53
}