Projet

Général

Profil

Paste
Télécharger (9,36 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authorization / ldap_authorization.admin.test.inc @ bc175c27

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 bc175c27 Assos Assos
 * Form to test a ldap authorization consumer configuration.
6 85ad3d82 Assos Assos
 */
7
8
/**
9 bc175c27 Assos Assos
 * Form for adding, updating, and deleting a single ldap authorization consumer configuration.
10
 *
11
 * @param mixed $form
12
 * @param mixed $form_state
13 85ad3d82 Assos Assos
 *
14
 * @return array drupal form array
15
 */
16
function ldap_authorization_test_form($form, &$form_state, $consumer_type) {
17
  $consumer = ldap_authorization_get_consumer_object($consumer_type);
18
  $consumer_tokens = ldap_authorization_tokens($consumer);
19
20
  if (isset($_SESSION['ldap_authorization_test_query']['result'])) {
21
    $form['result'] = array(
22
      '#type' => 'item',
23
      '#markup' => $_SESSION['ldap_authorization_test_query']['result'],
24 bc175c27 Assos Assos
    );
25 85ad3d82 Assos Assos
26
    $form['pre-filtered'] = array(
27
      '#type' => 'fieldset',
28
      '#title' => t('Prefiltered and Final Mappings'),
29
      '#collapsible' => TRUE,
30
      '#collapsed' => TRUE,
31
    );
32
    $form['pre-filtered']['overview'] = array(
33 bc175c27 Assos Assos
      '#type' => 'item',
34
      '#markup' => t('Below are the "raw authorizations" and which strategy they are derived from.  If enabled, the mapping filter is applied to these.'),
35 85ad3d82 Assos Assos
    );
36
37
    if (isset($_SESSION['ldap_authorization_test_query']['maps'])) {
38
      $i = 0;
39
      foreach ($_SESSION['ldap_authorization_test_query']['maps'] as $map => $data) {
40
        $i++;
41
        $form['pre-filtered']['map' . $i] = array(
42 bc175c27 Assos Assos
          '#type' => 'item',
43
          '#markup' => is_scalar($data) ? "<h2>$map (without filter)</h2>" . $data : theme('item_list', array('items' => $data, 'type' => 'ul', 'title' => "$map (without filter)")),
44 85ad3d82 Assos Assos
        );
45
      }
46
    }
47
48
    $form['use_first_attr_as_groupid'] = array(
49
      '#type' => 'fieldset',
50
      '#title' => t('After "Convert full dn to value of first attribute before mapping"'),
51
      '#collapsible' => TRUE,
52
      '#collapsed' => TRUE,
53
    );
54
55
    if ($consumer->consumerConf->useFirstAttrAsGroupId) {
56
      if (isset($_SESSION['ldap_authorization_test_query']['useFirstAttrAsGroupId'])) {
57
        $form['use_first_attr_as_groupid']['data'] = array(
58 bc175c27 Assos Assos
          '#type' => 'item',
59
          '#markup' => theme('item_list', array('items' => $_SESSION['ldap_authorization_test_query']['useFirstAttrAsGroupId'], 'type' => 'ul', 'title' => "Convert full dn to value of first attribute before mapping")),
60 85ad3d82 Assos Assos
        );
61
      }
62
    }
63
    else {
64
      $form['use_first_attr_as_groupid']['data'] = array(
65 bc175c27 Assos Assos
        '#type' => 'item',
66
        '#markup' => "Convert full dn to value of first attribute before mapping. NOT enabled",
67 85ad3d82 Assos Assos
      );
68
    }
69
70
    $form['post mappings'] = array(
71
      '#type' => 'fieldset',
72
      '#title' => t('After Mappings and Filters Applied'),
73
      '#collapsible' => TRUE,
74
      '#collapsed' => TRUE,
75
    );
76
77
    $maps = array();
78
    foreach ($consumer->consumerConf->mappings as $i => $mapping) {
79
      $maps[] = $mapping['from'] . '|' . $mapping['user_entered'];
80
    }
81
82
    $form['post mappings']['conf'] = array(
83
      '#type' => 'item',
84 bc175c27 Assos Assos
      '#markup' => 'Use Mappings as Filter = ' . (int) $consumer->consumerConf->useMappingsAsFilter .
85
      theme('item_list', array(
86
        'items' => $maps,
87
        'type' => 'ul',
88
        'title' => 'Configured Mappings',
89 85ad3d82 Assos Assos
      )
90 bc175c27 Assos Assos
      ),
91 85ad3d82 Assos Assos
    );
92
    if ($consumer->consumerConf->mappings) {
93
      $form['post mappings']['conf']['#markup'] =
94 bc175c27 Assos Assos
      'Use Mappings as Filter = ' . (int) $consumer->consumerConf->useMappingsAsFilter .
95 85ad3d82 Assos Assos
          theme('item_list', array(
96 bc175c27 Assos Assos
            'items' => $maps,
97
            'type' => 'ul',
98
            'title' => 'Configured Mappings',
99
          )
100 85ad3d82 Assos Assos
      );
101
    }
102
    else {
103
      $form['post mappings']['conf']['#markup'] =
104
      'Use Mappings as Filter = ' .
105 bc175c27 Assos Assos
      (int) $consumer->consumerConf->useMappingsAsFilter .
106 85ad3d82 Assos Assos
      '<br/>No Mappings Configured';
107
    }
108
109
    if (isset($_SESSION['ldap_authorization_test_query']['post mappings'])) {
110
      $form['post mappings']['data'] = array(
111 bc175c27 Assos Assos
        '#type' => 'item',
112
        '#markup' => theme('item_list', array('items' => $_SESSION['ldap_authorization_test_query']['post mappings'], 'type' => 'ul', 'title' => "Results after any filtering and mappings applied")),
113 85ad3d82 Assos Assos
      );
114
    }
115
116
  }
117
118
  if (isset($_SESSION['ldap_authorization_test_query']['setting_data'])) {
119
    foreach ($_SESSION['ldap_authorization_test_query']['setting_data'] as $title => $data) {
120
      $form[$title] = array(
121
        '#type' => 'fieldset',
122
        '#title' => $title,
123
        '#collapsible' => TRUE,
124
        '#collapsed' => TRUE,
125
      );
126
      $form[$title]['overview'] = array(
127 bc175c27 Assos Assos
        '#type' => 'item',
128
        '#markup' => "<pre>" . print_r($data, TRUE) . "</pre>",
129 85ad3d82 Assos Assos
      );
130
    }
131
  }
132
133
  $form['intro'] = array(
134 bc175c27 Assos Assos
    '#type' => 'item',
135
    '#markup' => t('<h1>Test LDAP to !consumer_name Configuration</h1>
136 85ad3d82 Assos Assos
      <p>This form will not actually grant any authorizations, its just to show
137
      what authorizations would be granted with this configuration.</p>', $consumer_tokens),
138
  );
139
140
  $form['consumer_type'] = array(
141
    '#type' => 'hidden',
142
    '#default_value' => $consumer_type,
143
  );
144
145
  $form['usernames'] = array(
146
    '#type' => 'textarea',
147
    '#title' => t('Drupal usernames to test !consumer_shortName authorizations results for.  One per line.', $consumer_tokens),
148
    '#default_value' => @$_SESSION['ldap_authorization_test_query']['usernames'] ,
149
    '#cols' => 50,
150
    '#rows' => 6,
151
    '#description' => '',
152
  );
153
154
  $form['random_users'] = array(
155
    '#type' => 'checkbox',
156
    '#default_value' => @$_SESSION['ldap_authorization_test_form']['random_users'],
157
    '#title' => t('Use 10 random users', $consumer_tokens),
158
  );
159
160
  $form['execute_authorizations'] = array(
161
    '#type' => 'checkbox',
162
    '#default_value' => @$_SESSION['ldap_authorization_test_form']['execute_authorizations'],
163
    '#title' => t('Actually grant or revoke example authorizations.
164
      This will grant and revoke based on the ldap authorization configuration
165
      options such as whether to revoke or regrant manually applied authorizations.
166
      Try with this unchecked first, then check to see how authorizations are applied.', $consumer_tokens),
167
  );
168
169
  $form['user_data_clear'] = array(
170
    '#type' => 'checkbox',
171
    '#default_value' => @$_SESSION['ldap_authorization_test_form']['user_data_clear'],
172
    '#title' => t('Clear <pre>$user->data[ldap_authorization][<consumer type>]</pre> data for test users.', $consumer_tokens),
173
  );
174
175
  $form['submit'] = array(
176
    '#type' => 'submit',
177
    '#value' => 'test',
178
  );
179
  unset($_SESSION['ldap_authorization_test_query']);
180
  return $form;
181
}
182
183
/**
184 bc175c27 Assos Assos
 * Validate handler for the ldap_authorization_test.
185 85ad3d82 Assos Assos
 */
186
function ldap_authorization_test_form_validate($form, &$form_state) {
187
188
  $values = $form_state['values'];
189
  if (!($values['usernames'] || $values['random_users'])) {
190
    form_set_error('No options chosen', t('Usernames must be given or random users checked.'));
191
  }
192
193
}
194
195
/**
196 bc175c27 Assos Assos
 * Submit handler function for ldap_authorization_test.
197 85ad3d82 Assos Assos
 */
198
function ldap_authorization_test_form_submit($form, &$form_state) {
199
  $consumer_type = $form_state['values']['consumer_type'];
200
  $consumer = ldap_authorization_get_consumer_object($consumer_type);
201
  $consumer_tokens = ldap_authorization_tokens($consumer);
202
203
  $results = array();
204
  $users_listed = $array = preg_split('/[\n\r]+/', $form_state['values']['usernames']);
205
  $random_users = array();
206
  $_SESSION['ldap_authorization_test_query']['random_users'] = $form_state['values']['random_users'];
207
  $_SESSION['ldap_authorization_test_query']['usernames'] = $form_state['values']['usernames'];
208 bc175c27 Assos Assos
  // Add 10 random usernames to test.
209
  if ($form_state['values']['random_users']) {
210
    // Not using user_load_multiple because need randomness outside of query.
211 85ad3d82 Assos Assos
    $select = db_select('users', 'u');
212
    $select->fields('u');
213
214
    try {
215 bc175c27 Assos Assos
      $random_users = $select->execute()->fetchAllAssoc('name', PDO::FETCH_ASSOC);
216 85ad3d82 Assos Assos
    }
217
    catch (Exception $e) {
218
      drupal_set_message(t('db users query failed. Message = %message, query= %query',
219
        array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
220
      return "";
221
    }
222
223
  }
224
  $user_names = array_unique(array_merge(array_keys($random_users), $users_listed));
225
  $i = 0;
226
  foreach ($user_names as $username) {
227
    if ($username) {
228
      if (!$user = user_load_by_name($username)) {
229 bc175c27 Assos Assos
        // If not existing user, create fake user assumed to be ldap authenticated.
230 85ad3d82 Assos Assos
        $user = new stdClass();
231
        $user->name = $username;
232
        $user->ldap_test = TRUE;
233
        $user->ldap_authenticated = TRUE;
234
      }
235
      else {
236
        if (function_exists('dpm')) {
237
          dpm("user: $username"); dpm($user);
238
        }
239
      }
240
241
      if ($form_state['values']['user_data_clear'] == 1) {
242
        $user_data = $user->data;
243
        unset($user_data['ldap_authorizations'][$consumer_type]);
244
        $user = user_save($user, array('data' => $user_data));
245
      }
246
247
      $action = ($form_state['values']['execute_authorizations'] == 1) ? "test_query_set" : 'test_query';
248
      list($results[$username], $notifications[$username]) = ldap_authorizations_user_authorizations($user, $action, $consumer_type, 'logon');
249 bc175c27 Assos Assos
      // Remove authorizations from other consumer types.
250 85ad3d82 Assos Assos
      $results[$username] = array($consumer_type => $results[$username][$consumer_type]);
251
      $i++;
252
      if ($i == 10) {
253
        break;
254
      }
255
    }
256
  }
257
258 bc175c27 Assos Assos
  $table = theme('ldap_authorization_test_results', array('results' => $results, 'consumer' => $consumer, 'notifications' => $notifications));
259 85ad3d82 Assos Assos
  $_SESSION['ldap_authorization_test_query']['result'] = $table;
260
  $form_state['redirect'] = LDAP_SERVERS_MENU_BASE_PATH . '/authorization/test/' . $consumer->consumerType;
261
262
}