Projet

Général

Profil

Révision 32700c57

Ajouté par Assos Assos il y a environ 5 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/ldap/ldap_test/LdapTestFunctions.class.php
2 2

  
3 3
/**
4 4
 * @file
5
 *
6
 * utility functions for ldap simpletests
5
 * Utility functions for ldap simpletests.
7 6
 * @todo could be moved into LdapTestCase.class.php
8
 *
9 7
 */
10 8

  
11
require_once('ldap_servers.conf.inc');
12
require_once('ldap_user.conf.inc');
13
require_once('ldap_authentication.conf.inc');
14
require_once('ldap_authorization.conf.inc');
15

  
16
class LdapTestFunctions  {
9
require_once 'ldap_servers.conf.inc';
10
require_once 'ldap_user.conf.inc';
11
require_once 'ldap_authentication.conf.inc';
12
require_once 'ldap_authorization.conf.inc';
13
/**
14
 *
15
 */
16
class LdapTestFunctions {
17 17

  
18
  public $data = array();
19
  public $ldapData = array();  // data in ldap array format, but keyed on dn
20
  public $csvTables = array();
18
  public $data = [];
19
  /**
20
   * Data in ldap array format, but keyed on dn.
21
   */
22
  public $ldapData = [];
23
  public $csvTables = [];
21 24
  public $ldapTypeConf;
22 25

  
23
  function __construct() {
26
  /**
27
   *
28
   */
29
  public function __construct() {
24 30
    module_load_include('module', 'ldap_servers');
25 31
    $this->data['ldap_servers'] = ldap_test_ldap_servers_data();
26 32
    module_load_include('module', 'ldap_user');
......
31 37
    $this->data['ldap_authentication'] = ldap_test_ldap_authentication_data();
32 38
  }
33 39

  
34
  function configureLdapServers($sids, $feetures = FALSE, $feature_name = NULL) {
40
  /**
41
   *
42
   */
43
  public function configureLdapServers($sids, $feetures = FALSE, $feature_name = NULL) {
35 44
    foreach ($sids as $i => $sid) {
36 45
      $current_sids[$sid] = $sid;
37 46
      variable_set('ldap_test_server__' . $sid, $this->data['ldap_servers'][$sid]);
......
39 48
    variable_set('ldap_test_servers', $current_sids);
40 49
  }
41 50

  
42
  function setFakeServerProperty($sid, $prop, $value) {
43
    $test_data = variable_get('ldap_test_server__' . $sid, array());
51
  /**
52
   *
53
   */
54
  public function setFakeServerProperty($sid, $prop, $value) {
55
    $test_data = variable_get('ldap_test_server__' . $sid, []);
44 56
    $test_data['properties'][$prop] = $value;
45 57
    variable_set('ldap_test_server__' . $sid, $test_data);
46 58
  }
47 59

  
48
  function setFakeServerUserAttribute($sid, $dn, $attr_name, $attr_value, $i=0) {
60
  /**
61
   *
62
   */
63
  public function setFakeServerUserAttribute($sid, $dn, $attr_name, $attr_value, $i = 0) {
49 64
    $attr_name = drupal_strtolower($attr_name);
50
    $test_data = variable_get('ldap_test_server__' . $sid, array());
65
    $test_data = variable_get('ldap_test_server__' . $sid, []);
51 66

  
52 67
    $test_data['entries'][$dn][$attr_name][$i] = $attr_value;
53
    $count_set = (int)isset($test_data['entries'][$dn][$attr_name]['count']);
54
    $test_data['entries'][$dn][$attr_name]['count'] = count($test_data['entries'][$dn][$attr_name]) - $count_set; // don't count the 'count'
68
    $count_set = (int) isset($test_data['entries'][$dn][$attr_name]['count']);
69
    // don't count the 'count'.
70
    $test_data['entries'][$dn][$attr_name]['count'] = count($test_data['entries'][$dn][$attr_name]) - $count_set;
55 71

  
56 72
    $test_data['ldap'][$dn][$attr_name][$i] = $attr_value;
57
    $count_set = (int)isset($test_data['ldap'][$dn][$attr_name]['count']);
58
    $test_data['ldap'][$dn][$attr_name]['count'] = count($test_data['ldap'][$dn][$attr_name]) - $count_set; // don't count the 'count'
73
    $count_set = (int) isset($test_data['ldap'][$dn][$attr_name]['count']);
74
    // don't count the 'count'.
75
    $test_data['ldap'][$dn][$attr_name]['count'] = count($test_data['ldap'][$dn][$attr_name]) - $count_set;
59 76
    variable_set('ldap_test_server__' . $sid, $test_data);
60
    $ldap_server = ldap_servers_get_servers($sid, NULL, TRUE, TRUE); // clear server cache;
77
    // Clear server cache;.
78
    $ldap_server = ldap_servers_get_servers($sid, NULL, TRUE, TRUE);
61 79
  }
62 80

  
63
  function configureLdapAuthentication($ldap_authentication_test_conf_id, $sids) {
81
  /**
82
   *
83
   */
84
  public function configureLdapAuthentication($ldap_authentication_test_conf_id, $sids) {
64 85
    module_load_include('php', 'ldap_authentication', 'LdapAuthenticationConfAdmin.class');
65 86
    $options = $this->data['ldap_authentication'][$ldap_authentication_test_conf_id];
66 87
    foreach ($sids as $i => $sid) {
......
75 96
    $ldapServerAdmin->save();
76 97
  }
77 98

  
78
  function configureLdapUser($ldap_user_test_conf_id) {
99
  /**
100
   *
101
   */
102
  public function configureLdapUser($ldap_user_test_conf_id) {
79 103
    module_load_include('php', 'ldap_user', 'LdapUserConfAdmin.class');
80 104
    $ldapUserConfAdmin = new LdapUserConfAdmin();
81 105
    $options = $this->data['ldap_user'][$ldap_user_test_conf_id];
......
87 111
    $ldapUserConfAdmin->save();
88 112
  }
89 113

  
90
  function prepConsumerConf($consumer_confs) {
91
    // create consumer authorization configuration.
114
  /**
115
   *
116
   */
117
  public function prepConsumerConf($consumer_confs) {
118
    // Create consumer authorization configuration.
92 119
    foreach ($consumer_confs as $consumer_type => $consumer_conf) {
93 120
      $consumer_obj = ldap_authorization_get_consumer_object($consumer_type);
94 121
      $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer_obj, TRUE);
......
97 124
      }
98 125
      foreach ($consumer_conf_admin->mappings as $i => $mapping) {
99 126
        $mappings = $consumer_obj->normalizeMappings(
100
          array(
101
            array($mapping['from'], $mapping['user_entered'])
102
          )
103
          , FALSE);
127
          [
128
            [$mapping['from'], $mapping['user_entered']],
129
          ],
130
           FALSE);
104 131
        $consumer_conf_admin->mappings[$i] = $mappings[0];
105 132
      }
106 133
      $consumer_conf_admin->save();
107 134
    }
108 135
  }
109 136

  
110

  
111
  function ldapUserIsAuthmapped($username) {
137
  /**
138
   *
139
   */
140
  public function ldapUserIsAuthmapped($username) {
112 141
    $authmaps = user_get_authmaps($username);
113 142
    return ($authmaps && in_array('ldap_user', array_keys($authmaps)));
114 143
  }
115 144

  
116
  function drupalLdapUpdateUser($edit = array(), $ldap_authenticated = FALSE, $user) {
145
  /**
146
   *
147
   */
148
  public function drupalLdapUpdateUser($edit = [], $ldap_authenticated = FALSE, $user) {
117 149
    if (count($edit)) {
118 150
      $user = user_save($user, $edit);
119 151
    }
120 152
    if ($ldap_authenticated) {
121
      user_set_authmaps($user, array('authname_ldap_user' => $user->name));
153
      user_set_authmaps($user, ['authname_ldap_user' => $user->name]);
122 154
    }
123 155
    return $user;
124 156
  }
125
// from http://www.midwesternmac.com/blogs/jeff-geerling/programmatically-adding-roles
126
public function removeRoleFromUser($user, $role_name) {
127 157

  
128
  if (is_numeric($user)) {
129
    $user = user_load($user);
130
  }
131
  $key = array_search($role_name, $user->roles);
132
  if ($key == TRUE) {
133
    // Get the rid from the roles table.
134
    $roles = user_roles(TRUE);
135
    $rid = array_search($role_name, $roles);
136
    if ($rid != FALSE) {
137
      // Make a copy of the roles array, without the deleted one.
138
      $new_roles = array();
139
      foreach($user->roles as $id => $name) {
140
        if ($id != $rid) {
141
          $new_roles[$id] = $name;
158
  /**
159
   * From http://www.midwesternmac.com/blogs/jeff-geerling/programmatically-adding-roles.
160
   */
161
  public function removeRoleFromUser($user, $role_name) {
162

  
163
    if (is_numeric($user)) {
164
      $user = user_load($user);
165
    }
166
    $key = array_search($role_name, $user->roles);
167
    if ($key == TRUE) {
168
      // Get the rid from the roles table.
169
      $roles = user_roles(TRUE);
170
      $rid = array_search($role_name, $roles);
171
      if ($rid != FALSE) {
172
        // Make a copy of the roles array, without the deleted one.
173
        $new_roles = [];
174
        foreach ($user->roles as $id => $name) {
175
          if ($id != $rid) {
176
            $new_roles[$id] = $name;
177
          }
142 178
        }
179
        user_save($user, ['roles' => $new_roles]);
143 180
      }
144
      user_save($user, array('roles' => $new_roles));
145 181
    }
146 182
  }
147
}
148 183

  
149
    public function userByNameFlushingCache($name) {
150
      $user = user_load_by_name($name);
151
      $users = user_load_multiple(array($user->uid), array(), TRUE); // clear user cache
152
      $user = $users[$user->uid];
153
      return $user;
154
    }
184
  /**
185
   *
186
   */
187
  public function userByNameFlushingCache($name) {
188
    $user = user_load_by_name($name);
189
    // Clear user cache.
190
    $users = user_load_multiple([$user->uid], [], TRUE);
191
    $user = $users[$user->uid];
192
    return $user;
193
  }
155 194

  
156
 /**
157
   * set variable with fake test data
195
  /**
196
   * Set variable with fake test data.
197
   *
198
   * @param string $test_ldap_id
199
   *   eg. 'hogwarts'.
200
   * @param string $test_ldap_type
201
   *   e.g. openLdap, openLdapTest1, etc.
158 202
   *
159
   * @param string $test_ldap_id eg. 'hogwarts'
160
   * @param string $test_ldap_type e.g. openLdap, openLdapTest1, etc.
161 203
   * @parma string $sid where fake data is stored. e.g. 'default',
162 204
   */
163 205
  public function populateFakeLdapServerData($test_ldap_id, $sid = 'default') {
164 206

  
165
    // read csvs into key/value array
166
    // create fake ldap data array
207
    // Read csvs into key/value array
208
    // create fake ldap data array.
167 209
    $clones = empty($this->data['ldap_servers'][$sid]['clones']) ? FALSE : $this->data['ldap_servers'][$sid]['clones'];
168 210
    $server_properties = $this->data['ldap_servers'][$sid]['properties'];
169 211
    $this->getCsvLdapData($test_ldap_id);
......
178 220
        $attributes,
179 221
        $server_properties['ldap_type'],
180 222
        $server_properties['user_attr']
181
      ) ;
223
      );
182 224
    }
183 225

  
184 226
    if ($clones) {
......
207 249
    foreach ($this->csvTables['groups'] as $guid => $group) {
208 250
      $dn = 'cn=' . $group['cn'] . ',' . $this->csvTables['conf'][$test_ldap_id]['groupbasedn'];
209 251
      $this->csvTables['groups'][$guid]['dn'] = $dn;
210
      $attributes = array(
211
        'cn' => array(
252
      $attributes = [
253
        'cn' => [
212 254
          0 => $group['cn'],
213 255
          'count' => 1,
214
        ),
215
        'gid' => array(
256
        ],
257
        'gid' => [
216 258
          0 => $group['gid'],
217 259
          'count' => 1,
218
        ),
219
        'guid' => array(
260
        ],
261
        'guid' => [
220 262
          0 => $guid,
221 263
          'count' => 1,
222
        ),
223
      );
264
        ],
265
      ];
224 266

  
225 267
      if ($server_properties['groupMembershipsAttr']) {
226 268
        $membershipAttr = $server_properties['groupMembershipsAttr'];
......
244 286
        $attributes[$membershipAttr]['count'] = count($attributes[$membershipAttr]);
245 287

  
246 288
      }
247
      // need to figure out if memberOf type attribute is desired and populate it
289
      // Need to figure out if memberOf type attribute is desired and populate it.
248 290
      $this->data['ldap_servers_by_guid'][$sid][$group['guid']]['attr'] = $attributes;
249 291
      $this->data['ldap_servers_by_guid'][$sid][$group['guid']]['dn'] = $dn;
250 292
      $this->data['ldap_servers'][$sid]['groups'][$dn]['attr'] = $attributes;
......
261 303
          unset($this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count']);
262 304
        }
263 305
        $this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count'] =
264
        count( $this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]);
306
        count($this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]);
265 307
      }
266 308
    }
267 309

  
268 310
    $this->data['ldap_servers'][$sid]['ldap'] = $this->ldapData['ldap_servers'][$sid];
269 311
    $this->data['ldap_servers'][$sid]['csv'] = $this->csvTables;
270 312
    variable_set('ldap_test_server__' . $sid, $this->data['ldap_servers'][$sid]);
271
    $current_sids = variable_get('ldap_test_servers', array());
313
    $current_sids = variable_get('ldap_test_servers', []);
272 314
    $current_sids[] = $sid;
273 315
    variable_set('ldap_test_servers', array_unique($current_sids));
274 316
  }
275 317

  
318
  /**
319
   *
320
   */
276 321
  public function generateUserLDAPAttributes($test_ldap_id, $user) {
277
    $attributes = array(
278
      'cn' => array(
322
    $attributes = [
323
      'cn' => [
279 324
        0 => $user['cn'],
280 325
        'count' => 1,
281
      ),
282
      'mail' => array(
326
      ],
327
      'mail' => [
283 328
        0 => $user['cn'] . '@' . $this->csvTables['conf'][$test_ldap_id]['mailhostname'],
284 329
        'count' => 1,
285
      ),
286
      'uid' => array(
330
      ],
331
      'uid' => [
287 332
        0 => $user['uid'],
288 333
        'count' => 1,
289
      ),
290
      'guid' => array(
334
      ],
335
      'guid' => [
291 336
        0 => $user['guid'],
292 337
        'count' => 1,
293
      ),
294
      'sn' => array(
338
      ],
339
      'sn' => [
295 340
        0 => $user['lname'],
296 341
        'count' => 1,
297
      ),
298
      'givenname' => array(
342
      ],
343
      'givenname' => [
299 344
        0 => $user['fname'],
300 345
        'count' => 1,
301
      ),
302
      'house' => array(
346
      ],
347
      'house' => [
303 348
        0 => $user['house'],
304 349
        'count' => 1,
305
      ),
306
      'department' => array(
350
      ],
351
      'department' => [
307 352
        0 => $user['department'],
308 353
        'count' => 1,
309
      ),
310
      'faculty' => array(
311
        0 => (int)(boolean)$user['faculty'],
354
      ],
355
      'faculty' => [
356
        0 => (int) (boolean) $user['faculty'],
312 357
        'count' => 1,
313
      ),
314
      'staff' => array(
315
        0 => (int)(boolean)$user['staff'],
358
      ],
359
      'staff' => [
360
        0 => (int) (boolean) $user['staff'],
316 361
        'count' => 1,
317
      ),
318
      'student' => array(
319
        0 => (int)(boolean)$user['student'],
362
      ],
363
      'student' => [
364
        0 => (int) (boolean) $user['student'],
320 365
        'count' => 1,
321
      ),
322
      'gpa' => array(
366
      ],
367
      'gpa' => [
323 368
        0 => $user['gpa'],
324 369
        'count' => 1,
325
      ),
326
      'probation' => array(
327
        0 => (int)(boolean)$user['probation'],
370
      ],
371
      'probation' => [
372
        0 => (int) (boolean) $user['probation'],
328 373
        'count' => 1,
329
      ),
330
      'password'  => array(
374
      ],
375
      'password'  => [
331 376
        0 => 'goodpwd',
332 377
        'count' => 1,
333
      ),
334
    );
378
      ],
379
    ];
335 380
    return $attributes;
336 381
  }
337 382

  
383
  /**
384
   *
385
   */
338 386
  public function addLDAPUserToLDAPArraysFromAttributes($user, $sid, $dn, $attributes, $ldap_type, $user_attr) {
339 387

  
340 388
    if ($ldap_type == 'activedirectory') {
341
      $attributes[$user_attr] = array(0 => $user['cn'], 'count' => 1);
342
      $attributes['distinguishedname'] = array( 0 => $dn, 'count' => 1);
389
      $attributes[$user_attr] = [0 => $user['cn'], 'count' => 1];
390
      $attributes['distinguishedname'] = [0 => $dn, 'count' => 1];
343 391
    }
344 392
    elseif ($ldap_type == 'openldap') {
345 393

  
......
352 400
    $this->ldapData['ldap_servers'][$sid][$dn]['count'] = count($attributes);
353 401
  }
354 402

  
403
  /**
404
   *
405
   */
355 406
  public function getCsvLdapData($test_ldap_id) {
356
    foreach (array('groups', 'users', 'memberships', 'conf') as $type) {
407
    foreach (['groups', 'users', 'memberships', 'conf'] as $type) {
357 408
      $path = drupal_get_path('module', 'ldap_test') . '/test_ldap/' . $test_ldap_id . '/' . $type . '.csv';
358 409
      $this->csvTables[$type] = $this->parseCsv($path);
359 410
    }
360 411
  }
361 412

  
413
  /**
414
   *
415
   */
362 416
  public function parseCsv($filepath) {
363 417
    $row = 1;
364
    $table = array();
418
    $table = [];
365 419
    if (($handle = fopen($filepath, "r")) !== FALSE) {
366 420
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
367 421
        if (count($data) > 1) {
......
371 425
      fclose($handle);
372 426
    }
373 427

  
374
    $table_associative = array();
428
    $table_associative = [];
375 429
    $headings = array_shift($table);
376 430
    foreach ($table as $i => $row) {
377 431
      $row_id = $row[0];

Formats disponibles : Unified diff