Projet

Général

Profil

Paste
Télécharger (4,05 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / cas_attributes / cas_attributes.module @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Allows user account and profile attributes to be automatically populated
6
 * using tokens. Provides basic tokens for attributes returned by the CAS
7
 * server.
8
 */
9

    
10
/**
11
 * Implements hook_menu().
12
 */
13
function cas_attributes_menu() {
14
  $items = array();
15

    
16
  $items['admin/config/people/cas/attributes'] = array(
17
    'title' => 'Attributes',
18
    'description' => 'Manage the relationships between CAS attributes and user fields.',
19
    'page callback' => 'drupal_get_form',
20
    'page arguments' => array('cas_attributes_admin_settings'),
21
    'access arguments' => array('administer cas'),
22
    'file' => 'cas_attributes.admin.inc',
23
    'type' => MENU_LOCAL_TASK,
24
    'weight' => -8,
25
  );
26
  $items['admin/config/people/cas/attributes/settings'] = array(
27
    'title' => 'Settings',
28
    'type' => MENU_DEFAULT_LOCAL_TASK,
29
    'weight' => -10,
30
  );
31
  $items['admin/config/people/cas/attributes/cas'] = array(
32
    'title' => 'CAS Attribute Tokens',
33
    'description' => 'Get a list of all available CAS Attributes',
34
    'page callback' => 'cas_attributes_list',
35
    'access arguments' => array('administer cas'),
36
    'file' => 'cas_attributes.admin.inc',
37
    'type' => MENU_LOCAL_TASK,
38
    'weight' => -8,
39
  );
40

    
41
  return $items;
42
}
43

    
44
/**
45
 * Implements hook_cas_user_presave().
46
 */
47
function cas_attributes_cas_user_presave(&$edit, $account) {
48
  // We synchronize on the first login (always) and on future logins (if chosen).
49
  if ($account->login && !variable_get('cas_attributes_sync_every_login', NULL)) {
50
    // The user has logged in before and we are not set to always synchronize.
51
    return;
52
  }
53

    
54
  $data = array('cas' => $edit['cas_user']['name']);
55

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

    
62
    // Only update the fields if there is data to set.
63
    if (!empty($result)) {
64
      if ($drupal_field == 'name' || $drupal_field == 'mail') {
65
        // Only update field if overwrite setting is on, or if it is empty.
66
        if ($overwrite || empty($account->{$drupal_field})) {
67
          $edit[$drupal_field] = $result;
68
        }
69
      }
70
      else {
71
        // Only update field if overwrite setting is on, or if it is empty.
72
        if ($overwrite || empty($account->{$drupal_field})) {
73
          $edit[$drupal_field][LANGUAGE_NONE][0]['value'] = $result;
74
        }
75
      }
76
    }
77
  }
78

    
79
  // Manage Roles
80

    
81
  // Make sure there are attributes to check.
82
  $mapping = variable_get('cas_attributes_roles_mapping', '');
83
  if (!empty($mapping)) {
84
    // Get the users attributes.
85
    $user_attributes = cas_phpcas_attributes($data['cas']);
86
    // Allow other modules to manipulate the attribute values.
87
    // Can't use module_invoke_all() because we need to pass byref.
88
    $arguments = array(&$user_attributes);
89
    foreach (module_implements('cas_attributes_roles_modify') as $module) {
90
      $function = $module . '_cas_attributes_roles_modify';
91
      call_user_func_array($function, $arguments);
92
    }
93

    
94
    // Build all the attirbutes to check.
95
    $cas_user_roles = array();
96
    $attributes_to_check = explode("\n", trim($mapping));
97
    foreach ($attributes_to_check as $attribute) {
98
      $attribute = trim($attribute);
99
      if (!empty($user_attributes[$attribute])) {
100
        if (is_array($user_attributes[$attribute])) {
101
          $cas_user_roles = array_merge($cas_user_roles, $user_attributes[$attribute]);
102
        }
103
        else {
104
          $cas_user_roles[] = $user_attributes[$attribute];
105
        }
106
      }
107
    }
108

    
109
    // Loop through all the managed roles and see if the user has access to them
110
    // and set accordingly.
111
    $roles = user_roles();
112
    foreach (variable_get('cas_attributes_roles_manage', array()) as $rid) {
113
      if (!empty($rid)) {
114
        if (in_array($roles[$rid], $cas_user_roles)) {
115
          $edit['roles'][$rid] = $roles[$rid];
116
        }
117
        else {
118
          unset($edit['roles'][$rid]);
119
        }
120
      }
121
    }
122
  }
123
}