Projet

Général

Profil

Paste
Télécharger (8,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_user / ldap_user.test_form.inc @ bc175c27

1
<?php
2

    
3
/**
4
 * @file
5
 */
6

    
7
/**
8
 * Implements the LDAP user test page.
9
 *
10
 * @param $form_state
11
 *   A form state array.
12
 * @param $op
13
 *   An operatin - add or edit.
14
 * @param $sid
15
 *   A LDAP server ID.
16
 *
17
 * @return
18
 *   The form structure.
19
 */
20

    
21
function ldap_user_test_form($form, &$form_state, $op = NULL) {
22

    
23
  $username = @$_SESSION['ldap_user_test_form']['testing_drupal_username'];
24

    
25
  $form['#prefix'] = t('<h1>Test LDAP User Configuration</h1>');
26

    
27
  $form['#prefix'] .= t('This form simply tests an LDAP User configuration against an individual ldap or drupal user.
28
    It makes no changes to the drupal or ldap user.');
29

    
30
  $form['testing_drupal_username'] = array(
31
    '#type' => 'textfield',
32
    '#title' => t('Testing Drupal Username'),
33
    '#default_value' => $username,
34
    '#required' => 1,
35
    '#size' => 30,
36
    '#maxlength' => 255,
37
    '#description' => t('This is optional and used for testing this server\'s configuration against an actual username.  The user need not exist in Drupal and testing will not affect the user\'s LDAP or Drupal Account.'),
38
  );
39

    
40
  $form['test_mode'] = array(
41
      '#type' => 'radios',
42
      '#title' => t('Testing Mode'),
43
      '#required' => 0,
44
      '#default_value' => isset( $_SESSION['ldap_user_test_form']['test_mode']) ? $_SESSION['ldap_user_test_form']['test_mode'] : 'query',
45
      '#options' => array(
46
        'query' => t('Test Query.  Will not alter anything in drupal or LDAP'),
47
        'execute' => t('Execute Action.  Will perform provisioning configured for events below.  If this is selected only one action should be selected below'),
48
      ),
49
    );
50

    
51
  $synch_trigger_options = ldap_user_synch_triggers_key_values();
52

    
53
  $selected_actions = isset($_SESSION['ldap_user_test_form']['action']) ? $_SESSION['ldap_user_test_form']['action'] : array();
54
  $form['action'] = array(
55
      '#type' => 'checkboxes',
56
      '#title' => t('Actions/Event Handlers to Test'),
57
      '#required' => 0,
58
      '#default_value' => $selected_actions,
59
      '#options' => $synch_trigger_options,
60
      '#states' => array(
61
        'visible' => array(   // action to take.
62
          ':input[name="wsEnabled"]' => array('checked' => TRUE),
63
        ),
64
      ),
65
    );
66

    
67
  $form['submit'] = array(
68
    '#type' => 'submit',
69
    '#value' => 'test',
70
    '#weight' => 100,
71
  );
72

    
73
  return $form;
74
}
75

    
76
function ldap_user_test_form_validate($form, &$form_state) {
77
  if ($form_state['values']['test_mode'] == 'execute'  &&
78
    count(array_filter($form_state['values']['action'])) > 1 ) {
79
    form_set_error('test_mode', t('Only one action may be selected for "Execute Action" testing mode.'));
80
  }
81

    
82

    
83
}
84

    
85
/**
86
 * Submit hook for the LDAP server form.
87
 */
88
function ldap_user_test_form_submit($form, &$form_state) {
89

    
90
  $username = $form_state['values']['testing_drupal_username'];
91
  $selected_actions = $form_state['values']['action'];
92

    
93
  if ($username && count($selected_actions) > 0) {
94

    
95
    $synch_trigger_options = ldap_user_synch_triggers_key_values();
96

    
97
    $user_object = user_load_by_name($username);
98
    if ($user_object) {
99
      $user_entities = entity_load('user', array($user_object->uid));
100
      $user_entity = $user_entities[$user_object->uid];
101
    }
102
    else {
103
      $user_entity = NULL;
104
    }
105

    
106
    $ldap_user_conf = ldap_user_conf();
107
    $test_servers = array();
108
    $user_ldap_entry = FALSE;
109
    if ($ldap_user_conf->drupalAcctProvisionServer) {
110
      $test_servers[LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER] = $ldap_user_conf->drupalAcctProvisionServer;
111
      $user_ldap_entry = ldap_servers_get_user_ldap_data($username, $ldap_user_conf->drupalAcctProvisionServer);
112
    }
113
    if ($ldap_user_conf->ldapEntryProvisionServer) {
114
      $test_servers[LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY] = $ldap_user_conf->ldapEntryProvisionServer;
115
      if (!$user_ldap_entry) {
116
        $user_ldap_entry = ldap_servers_get_user_ldap_data($username, $ldap_user_conf->ldapEntryProvisionServer);
117
      }
118
    }
119
    $results = array();
120
    $results['username'] = $username;
121
    $results['user object (before provisioning or synching)'] = $user_object;
122
    $results['user entity (before provisioning or synching)'] = $user_entity;
123
    $results['related ldap entry (before provisioning or synching)'] = $user_ldap_entry;
124
    $results['ldap_user_conf'] = $ldap_user_conf;
125

    
126
    if (is_object($user_object)) {
127
      $authmaps = db_query("SELECT aid, uid, module, authname FROM {authmap} WHERE uid = :uid", array(':uid' => $user_object->uid))->fetchAllAssoc('aid', PDO::FETCH_ASSOC);
128
    }
129
    else {
130
      $authmaps = 'No authmaps available.  Authmaps only shown if user account exists beforehand';
131
      $user_object = new stdClass(); // need for testing.
132
      $user_object->name = $username;
133
    }
134
    $results['User Authmap'] = $authmaps;
135
    $results['LDAP User Configuration Object'] = $ldap_user_conf;
136

    
137
    $save = ($form_state['values']['test_mode'] == 'execute');
138
    $test_query = ($form_state['values']['test_mode'] != 'execute');
139
    $user_edit = array('name' => $username);
140

    
141
    foreach (array_filter($selected_actions) as $i => $synch_trigger) {
142
      $synch_trigger_description = $synch_trigger_options[$synch_trigger];
143
      foreach (array(LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY) as $direction) {
144
        if ($ldap_user_conf->provisionEnabled($direction, $synch_trigger)) {
145
          if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
146
            $discard = $ldap_user_conf->provisionDrupalAccount(NULL, $user_edit, NULL, $save);
147
            $results['provisionDrupalAccount method results']["context = $synch_trigger_description"]['proposed'] = $user_edit;
148
          }
149
          else {
150
            $provision_result = $ldap_user_conf->provisionLdapEntry($user_object, NULL, $test_query);
151
            $results['provisionLdapEntry method results']["context = $synch_trigger_description"] = $provision_result;
152
          }
153
        }
154
        else {
155
          if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
156
            $results['provisionDrupalAccount method results']["context = $synch_trigger_description"] = 'Not enabled.';
157
          }
158
          else {
159
            $results['provisionLdapEntry method results']["context = $synch_trigger_description"] = 'Not enabled.';
160
          }
161
        }
162
      }
163
    }
164
    // do all synchs second, in case logic of form changes to allow executing mulitple events
165
    foreach (array_filter($selected_actions) as $i => $synch_trigger) {
166
      $synch_trigger_description = $synch_trigger_options[$synch_trigger];
167
      foreach (array(LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER, LDAP_USER_PROV_DIRECTION_TO_LDAP_ENTRY) as $direction) {
168
        if ($ldap_user_conf->provisionEnabled($direction, $synch_trigger)) {
169
          if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
170
            $discard = $ldap_user_conf->synchToDrupalAccount(NULL, $user_edit, NULL, $test_query);
171
            $results['synchToDrupalAccount method results']["context = $synch_trigger_description"]['proposed'] = $user_edit;
172
          }
173
          else { // to ldap
174
            $provision_result = $ldap_user_conf->synchToLdapEntry($user_object, $user_edit, array(), $test_query);
175
            $results['synchToLdapEntry method results']["context = $synch_trigger_description"] = $provision_result;
176
          }
177
        }
178
        else {
179
          if ($direction == LDAP_USER_PROV_DIRECTION_TO_DRUPAL_USER) {
180
            $results['synchToDrupalAccount method results']["context = $synch_trigger_description"] = 'Not enabled.';
181
          }
182
          else { // to ldap
183
            $results['synchToLdapEntry method results']["context = $synch_trigger_description"] = 'Not enabled.';
184
          }
185
        }
186
      }
187
    }
188

    
189

    
190
    if (function_exists('dpm')) {
191
      dpm($results);
192
    }
193
    else {
194
      drupal_set_message(t('This form will not display results unless the devel module is enabled.'), 'warning');
195
    }
196
  }
197

    
198
  $_SESSION['ldap_user_test_form']['action'] = $form_state['values']['action'];
199
  $_SESSION['ldap_user_test_form']['test_mode'] = $form_state['values']['test_mode'];
200
  $_SESSION['ldap_user_test_form']['testing_drupal_username'] = $username;
201

    
202
  $form_state['redirect'] = LDAP_USER_TEST_FORM_PATH;
203

    
204
}