1
|
<?php
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
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
|
|
18
|
public $data = [];
|
19
|
|
20
|
|
21
|
|
22
|
public $ldapData = [];
|
23
|
public $csvTables = [];
|
24
|
public $ldapTypeConf;
|
25
|
|
26
|
|
27
|
|
28
|
|
29
|
public function __construct() {
|
30
|
module_load_include('module', 'ldap_servers');
|
31
|
$this->data['ldap_servers'] = ldap_test_ldap_servers_data();
|
32
|
module_load_include('module', 'ldap_user');
|
33
|
$this->data['ldap_user'] = ldap_test_ldap_user_data();
|
34
|
module_load_include('module', 'ldap_authentication');
|
35
|
$this->data['ldap_authorization'] = ldap_test_ldap_authorization_data();
|
36
|
module_load_include('module', 'ldap_authorization');
|
37
|
$this->data['ldap_authentication'] = ldap_test_ldap_authentication_data();
|
38
|
}
|
39
|
|
40
|
|
41
|
|
42
|
|
43
|
public function configureLdapServers($sids, $feetures = FALSE, $feature_name = NULL) {
|
44
|
foreach ($sids as $i => $sid) {
|
45
|
$current_sids[$sid] = $sid;
|
46
|
variable_set('ldap_test_server__' . $sid, $this->data['ldap_servers'][$sid]);
|
47
|
}
|
48
|
variable_set('ldap_test_servers', $current_sids);
|
49
|
}
|
50
|
|
51
|
|
52
|
|
53
|
|
54
|
public function setFakeServerProperty($sid, $prop, $value) {
|
55
|
$test_data = variable_get('ldap_test_server__' . $sid, []);
|
56
|
$test_data['properties'][$prop] = $value;
|
57
|
variable_set('ldap_test_server__' . $sid, $test_data);
|
58
|
}
|
59
|
|
60
|
|
61
|
|
62
|
|
63
|
public function setFakeServerUserAttribute($sid, $dn, $attr_name, $attr_value, $i = 0) {
|
64
|
$attr_name = drupal_strtolower($attr_name);
|
65
|
$test_data = variable_get('ldap_test_server__' . $sid, []);
|
66
|
|
67
|
$test_data['entries'][$dn][$attr_name][$i] = $attr_value;
|
68
|
$count_set = (int) isset($test_data['entries'][$dn][$attr_name]['count']);
|
69
|
|
70
|
$test_data['entries'][$dn][$attr_name]['count'] = count($test_data['entries'][$dn][$attr_name]) - $count_set;
|
71
|
|
72
|
$test_data['ldap'][$dn][$attr_name][$i] = $attr_value;
|
73
|
$count_set = (int) isset($test_data['ldap'][$dn][$attr_name]['count']);
|
74
|
|
75
|
$test_data['ldap'][$dn][$attr_name]['count'] = count($test_data['ldap'][$dn][$attr_name]) - $count_set;
|
76
|
variable_set('ldap_test_server__' . $sid, $test_data);
|
77
|
|
78
|
$ldap_server = ldap_servers_get_servers($sid, NULL, TRUE, TRUE);
|
79
|
}
|
80
|
|
81
|
|
82
|
|
83
|
|
84
|
public function configureLdapAuthentication($ldap_authentication_test_conf_id, $sids) {
|
85
|
module_load_include('php', 'ldap_authentication', 'LdapAuthenticationConfAdmin.class');
|
86
|
$options = $this->data['ldap_authentication'][$ldap_authentication_test_conf_id];
|
87
|
foreach ($sids as $i => $sid) {
|
88
|
$options['sids'][$sid] = $sid;
|
89
|
}
|
90
|
$ldapServerAdmin = new LdapAuthenticationConfAdmin();
|
91
|
foreach ($ldapServerAdmin->saveable as $prop_name) {
|
92
|
if (isset($options[$prop_name])) {
|
93
|
$ldapServerAdmin->{$prop_name} = $options[$prop_name];
|
94
|
}
|
95
|
}
|
96
|
$ldapServerAdmin->save();
|
97
|
}
|
98
|
|
99
|
|
100
|
|
101
|
|
102
|
public function configureLdapUser($ldap_user_test_conf_id) {
|
103
|
module_load_include('php', 'ldap_user', 'LdapUserConfAdmin.class');
|
104
|
$ldapUserConfAdmin = new LdapUserConfAdmin();
|
105
|
$options = $this->data['ldap_user'][$ldap_user_test_conf_id];
|
106
|
foreach ($ldapUserConfAdmin->saveable as $prop_name) {
|
107
|
if (isset($options[$prop_name])) {
|
108
|
$ldapUserConfAdmin->{$prop_name} = $options[$prop_name];
|
109
|
}
|
110
|
}
|
111
|
$ldapUserConfAdmin->save();
|
112
|
}
|
113
|
|
114
|
|
115
|
|
116
|
|
117
|
public function prepConsumerConf($consumer_confs) {
|
118
|
|
119
|
foreach ($consumer_confs as $consumer_type => $consumer_conf) {
|
120
|
$consumer_obj = ldap_authorization_get_consumer_object($consumer_type);
|
121
|
$consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer_obj, TRUE);
|
122
|
foreach ($consumer_conf as $property_name => $property_value) {
|
123
|
$consumer_conf_admin->{$property_name} = $property_value;
|
124
|
}
|
125
|
foreach ($consumer_conf_admin->mappings as $i => $mapping) {
|
126
|
$mappings = $consumer_obj->normalizeMappings(
|
127
|
[
|
128
|
[$mapping['from'], $mapping['user_entered']],
|
129
|
],
|
130
|
FALSE);
|
131
|
$consumer_conf_admin->mappings[$i] = $mappings[0];
|
132
|
}
|
133
|
$consumer_conf_admin->save();
|
134
|
}
|
135
|
}
|
136
|
|
137
|
|
138
|
|
139
|
|
140
|
public function ldapUserIsAuthmapped($username) {
|
141
|
$authmaps = user_get_authmaps($username);
|
142
|
return ($authmaps && in_array('ldap_user', array_keys($authmaps)));
|
143
|
}
|
144
|
|
145
|
|
146
|
|
147
|
|
148
|
public function drupalLdapUpdateUser($edit = [], $ldap_authenticated = FALSE, $user) {
|
149
|
if (count($edit)) {
|
150
|
$user = user_save($user, $edit);
|
151
|
}
|
152
|
if ($ldap_authenticated) {
|
153
|
user_set_authmaps($user, ['authname_ldap_user' => $user->name]);
|
154
|
}
|
155
|
return $user;
|
156
|
}
|
157
|
|
158
|
|
159
|
|
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
|
|
169
|
$roles = user_roles(TRUE);
|
170
|
$rid = array_search($role_name, $roles);
|
171
|
if ($rid != FALSE) {
|
172
|
|
173
|
$new_roles = [];
|
174
|
foreach ($user->roles as $id => $name) {
|
175
|
if ($id != $rid) {
|
176
|
$new_roles[$id] = $name;
|
177
|
}
|
178
|
}
|
179
|
user_save($user, ['roles' => $new_roles]);
|
180
|
}
|
181
|
}
|
182
|
}
|
183
|
|
184
|
|
185
|
|
186
|
|
187
|
public function userByNameFlushingCache($name) {
|
188
|
$user = user_load_by_name($name);
|
189
|
|
190
|
$users = user_load_multiple([$user->uid], [], TRUE);
|
191
|
$user = $users[$user->uid];
|
192
|
return $user;
|
193
|
}
|
194
|
|
195
|
|
196
|
|
197
|
|
198
|
|
199
|
|
200
|
|
201
|
|
202
|
|
203
|
|
204
|
|
205
|
public function populateFakeLdapServerData($test_ldap_id, $sid = 'default') {
|
206
|
|
207
|
|
208
|
|
209
|
$clones = empty($this->data['ldap_servers'][$sid]['clones']) ? FALSE : $this->data['ldap_servers'][$sid]['clones'];
|
210
|
$server_properties = $this->data['ldap_servers'][$sid]['properties'];
|
211
|
$this->getCsvLdapData($test_ldap_id);
|
212
|
foreach ($this->csvTables['users'] as $guid => $user) {
|
213
|
$dn = 'cn=' . $user['cn'] . ',' . $this->csvTables['conf'][$test_ldap_id]['userbasedn'];
|
214
|
$this->csvTables['users'][$guid]['dn'] = $dn;
|
215
|
$attributes = $this->generateUserLDAPAttributes($test_ldap_id, $user);
|
216
|
$this->addLDAPUserToLDAPArraysFromAttributes(
|
217
|
$user,
|
218
|
$sid,
|
219
|
$dn,
|
220
|
$attributes,
|
221
|
$server_properties['ldap_type'],
|
222
|
$server_properties['user_attr']
|
223
|
);
|
224
|
}
|
225
|
|
226
|
if ($clones) {
|
227
|
$clonable_user = $this->csvTables['users'][101];
|
228
|
for ($i = 0; $i < $clones; $i++) {
|
229
|
$user = $clonable_user;
|
230
|
$cn = "clone" . $i;
|
231
|
$dn = 'cn=' . $cn . ',' . $this->csvTables['conf'][$test_ldap_id]['userbasedn'];
|
232
|
$user['cn'] = $cn;
|
233
|
$user['dn'] = $dn;
|
234
|
$user['uid'] = 20 + $i;
|
235
|
$user['guid'] = 120 + $i;
|
236
|
$user['lname'] = $user['lname'] . "_$i";
|
237
|
$attributes = $this->generateUserLDAPAttributes($test_ldap_id, $user);
|
238
|
$this->addLDAPUserToLDAPArraysFromAttributes(
|
239
|
$user,
|
240
|
$sid,
|
241
|
$dn,
|
242
|
$attributes,
|
243
|
$server_properties['ldap_type'],
|
244
|
$server_properties['user_attr']
|
245
|
);
|
246
|
}
|
247
|
}
|
248
|
|
249
|
foreach ($this->csvTables['groups'] as $guid => $group) {
|
250
|
$dn = 'cn=' . $group['cn'] . ',' . $this->csvTables['conf'][$test_ldap_id]['groupbasedn'];
|
251
|
$this->csvTables['groups'][$guid]['dn'] = $dn;
|
252
|
$attributes = [
|
253
|
'cn' => [
|
254
|
0 => $group['cn'],
|
255
|
'count' => 1,
|
256
|
],
|
257
|
'gid' => [
|
258
|
0 => $group['gid'],
|
259
|
'count' => 1,
|
260
|
],
|
261
|
'guid' => [
|
262
|
0 => $guid,
|
263
|
'count' => 1,
|
264
|
],
|
265
|
];
|
266
|
|
267
|
if ($server_properties['groupMembershipsAttr']) {
|
268
|
$membershipAttr = $server_properties['groupMembershipsAttr'];
|
269
|
foreach ($this->csvTables['memberships'] as $membership_id => $membership) {
|
270
|
if ($membership['gid'] == $group['gid']) {
|
271
|
$member_guid = $membership['member_guid'];
|
272
|
if (isset($this->csvTables['users'][$member_guid])) {
|
273
|
$member = $this->csvTables['users'][$member_guid];
|
274
|
}
|
275
|
elseif (isset($this->csvTables['groups'][$member_guid])) {
|
276
|
$member = $this->csvTables['groups'][$member_guid];
|
277
|
}
|
278
|
if ($server_properties['groupMembershipsAttrMatchingUserAttr'] == 'dn') {
|
279
|
$attributes[$server_properties['groupMembershipsAttr']][] = $member['dn'];
|
280
|
}
|
281
|
else {
|
282
|
$attributes[$server_properties['groupMembershipsAttr']][] = $member['attr'][$membershipAttr][0];
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
$attributes[$membershipAttr]['count'] = count($attributes[$membershipAttr]);
|
287
|
|
288
|
}
|
289
|
|
290
|
$this->data['ldap_servers_by_guid'][$sid][$group['guid']]['attr'] = $attributes;
|
291
|
$this->data['ldap_servers_by_guid'][$sid][$group['guid']]['dn'] = $dn;
|
292
|
$this->data['ldap_servers'][$sid]['groups'][$dn]['attr'] = $attributes;
|
293
|
$this->ldapData['ldap_servers'][$sid][$dn] = $attributes;
|
294
|
|
295
|
}
|
296
|
if ($server_properties['groupUserMembershipsAttrExists']) {
|
297
|
$member_attr = $server_properties['groupUserMembershipsAttr'];
|
298
|
foreach ($this->csvTables['memberships'] as $gid => $membership) {
|
299
|
$group_dn = $this->data['ldap_servers_by_guid'][$sid][$membership['group_guid']]['dn'];
|
300
|
$user_dn = $this->data['ldap_servers_by_guid'][$sid][$membership['member_guid']]['dn'];
|
301
|
$this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr][] = $group_dn;
|
302
|
if (isset($this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count'])) {
|
303
|
unset($this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count']);
|
304
|
}
|
305
|
$this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count'] =
|
306
|
count($this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]);
|
307
|
}
|
308
|
}
|
309
|
|
310
|
$this->data['ldap_servers'][$sid]['ldap'] = $this->ldapData['ldap_servers'][$sid];
|
311
|
$this->data['ldap_servers'][$sid]['csv'] = $this->csvTables;
|
312
|
variable_set('ldap_test_server__' . $sid, $this->data['ldap_servers'][$sid]);
|
313
|
$current_sids = variable_get('ldap_test_servers', []);
|
314
|
$current_sids[] = $sid;
|
315
|
variable_set('ldap_test_servers', array_unique($current_sids));
|
316
|
}
|
317
|
|
318
|
|
319
|
|
320
|
|
321
|
public function generateUserLDAPAttributes($test_ldap_id, $user) {
|
322
|
$attributes = [
|
323
|
'cn' => [
|
324
|
0 => $user['cn'],
|
325
|
'count' => 1,
|
326
|
],
|
327
|
'mail' => [
|
328
|
0 => $user['cn'] . '@' . $this->csvTables['conf'][$test_ldap_id]['mailhostname'],
|
329
|
'count' => 1,
|
330
|
],
|
331
|
'uid' => [
|
332
|
0 => $user['uid'],
|
333
|
'count' => 1,
|
334
|
],
|
335
|
'guid' => [
|
336
|
0 => $user['guid'],
|
337
|
'count' => 1,
|
338
|
],
|
339
|
'sn' => [
|
340
|
0 => $user['lname'],
|
341
|
'count' => 1,
|
342
|
],
|
343
|
'givenname' => [
|
344
|
0 => $user['fname'],
|
345
|
'count' => 1,
|
346
|
],
|
347
|
'house' => [
|
348
|
0 => $user['house'],
|
349
|
'count' => 1,
|
350
|
],
|
351
|
'department' => [
|
352
|
0 => $user['department'],
|
353
|
'count' => 1,
|
354
|
],
|
355
|
'faculty' => [
|
356
|
0 => (int) (boolean) $user['faculty'],
|
357
|
'count' => 1,
|
358
|
],
|
359
|
'staff' => [
|
360
|
0 => (int) (boolean) $user['staff'],
|
361
|
'count' => 1,
|
362
|
],
|
363
|
'student' => [
|
364
|
0 => (int) (boolean) $user['student'],
|
365
|
'count' => 1,
|
366
|
],
|
367
|
'gpa' => [
|
368
|
0 => $user['gpa'],
|
369
|
'count' => 1,
|
370
|
],
|
371
|
'probation' => [
|
372
|
0 => (int) (boolean) $user['probation'],
|
373
|
'count' => 1,
|
374
|
],
|
375
|
'password' => [
|
376
|
0 => 'goodpwd',
|
377
|
'count' => 1,
|
378
|
],
|
379
|
];
|
380
|
return $attributes;
|
381
|
}
|
382
|
|
383
|
|
384
|
|
385
|
|
386
|
public function addLDAPUserToLDAPArraysFromAttributes($user, $sid, $dn, $attributes, $ldap_type, $user_attr) {
|
387
|
|
388
|
if ($ldap_type == 'activedirectory') {
|
389
|
$attributes[$user_attr] = [0 => $user['cn'], 'count' => 1];
|
390
|
$attributes['distinguishedname'] = [0 => $dn, 'count' => 1];
|
391
|
}
|
392
|
elseif ($ldap_type == 'openldap') {
|
393
|
|
394
|
}
|
395
|
|
396
|
$this->data['ldap_servers'][$sid]['users'][$dn]['attr'] = $attributes;
|
397
|
$this->data['ldap_servers_by_guid'][$sid][$user['guid']]['attr'] = $attributes;
|
398
|
$this->data['ldap_servers_by_guid'][$sid][$user['guid']]['dn'] = $dn;
|
399
|
$this->ldapData['ldap_servers'][$sid][$dn] = $attributes;
|
400
|
$this->ldapData['ldap_servers'][$sid][$dn]['count'] = count($attributes);
|
401
|
}
|
402
|
|
403
|
|
404
|
|
405
|
|
406
|
public function getCsvLdapData($test_ldap_id) {
|
407
|
foreach (['groups', 'users', 'memberships', 'conf'] as $type) {
|
408
|
$path = drupal_get_path('module', 'ldap_test') . '/test_ldap/' . $test_ldap_id . '/' . $type . '.csv';
|
409
|
$this->csvTables[$type] = $this->parseCsv($path);
|
410
|
}
|
411
|
}
|
412
|
|
413
|
|
414
|
|
415
|
|
416
|
public function parseCsv($filepath) {
|
417
|
$row = 1;
|
418
|
$table = [];
|
419
|
if (($handle = fopen($filepath, "r")) !== FALSE) {
|
420
|
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
|
421
|
if (count($data) > 1) {
|
422
|
$table[] = $data;
|
423
|
}
|
424
|
}
|
425
|
fclose($handle);
|
426
|
}
|
427
|
|
428
|
$table_associative = [];
|
429
|
$headings = array_shift($table);
|
430
|
foreach ($table as $i => $row) {
|
431
|
$row_id = $row[0];
|
432
|
foreach ($row as $j => $item) {
|
433
|
$table_associative[$row_id][$headings[$j]] = $item;
|
434
|
}
|
435
|
}
|
436
|
|
437
|
return $table_associative;
|
438
|
|
439
|
}
|
440
|
|
441
|
}
|