Projet

Général

Profil

Paste
Télécharger (7,91 ko) Statistiques
| Branche: | Révision:

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

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
function ldap_user_test_form($form, &$form_state, $op = NULL) {
21

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

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

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

    
29
  $form['testing_drupal_username'] = [
30
    '#type' => 'textfield',
31
    '#title' => t('Testing Drupal Username'),
32
    '#default_value' => $username,
33
    '#required' => 1,
34
    '#size' => 30,
35
    '#maxlength' => 255,
36
    '#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.'),
37
  ];
38

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

    
50
  $synch_trigger_options = ldap_user_synch_triggers_key_values();
51

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

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

    
72
  return $form;
73
}
74

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

    
84
}
85

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

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

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

    
96
    $synch_trigger_options = ldap_user_synch_triggers_key_values();
97

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

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

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

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

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

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

    
201
  $_SESSION['ldap_user_test_form']['action'] = $form_state['values']['action'];
202
  $_SESSION['ldap_user_test_form']['test_mode'] = $form_state['values']['test_mode'];
203
  $_SESSION['ldap_user_test_form']['testing_drupal_username'] = $username;
204

    
205
  $form_state['redirect'] = LDAP_USER_TEST_FORM_PATH;
206

    
207
}