Issue #1994850. Part 2. Fix for no context when mapping drupal username to existing...
[project/ldap.git] / ldap_test / LdapTestFunctions.class.php
1 <?php
2
3 /**
4 * @file
5 *
6 * utility functions for ldap simpletests
7 * @todo could be moved into LdapTestCase.class.php
8 *
9 */
10
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 {
17
18 public $data = array();
19 public $ldapData = array(); // data in ldap array format, but keyed on dn
20 public $csvTables = array();
21 public $ldapTypeConf;
22
23 function __construct() {
24 module_load_include('module', 'ldap_servers');
25 $this->data['ldap_servers'] = ldap_test_ldap_servers_data();
26 module_load_include('module', 'ldap_user');
27 $this->data['ldap_user'] = ldap_test_ldap_user_data();
28 module_load_include('module', 'ldap_authentication');
29 $this->data['ldap_authorization'] = ldap_test_ldap_authorization_data();
30 module_load_include('module', 'ldap_authorization');
31 $this->data['ldap_authentication'] = ldap_test_ldap_authentication_data();
32 }
33
34 function configureLdapServers($sids, $feetures = FALSE, $feature_name = NULL) {
35 foreach ($sids as $i => $sid) {
36 $current_sids[$sid] = $sid;
37 variable_set('ldap_test_server__' . $sid, $this->data['ldap_servers'][$sid]);
38 }
39 variable_set('ldap_test_servers', $current_sids);
40 }
41
42 function setFakeServerProperty($sid, $prop, $value) {
43 $test_data = variable_get('ldap_test_server__' . $sid, array());
44 $test_data['properties'][$prop] = $value;
45 variable_set('ldap_test_server__' . $sid, $test_data);
46 }
47
48 function setFakeServerUserAttribute($sid, $dn, $attr_name, $attr_value, $i=0) {
49 $attr_name = drupal_strtolower($attr_name);
50 $test_data = variable_get('ldap_test_server__' . $sid, array());
51
52 $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'
55
56 $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'
59 variable_set('ldap_test_server__' . $sid, $test_data);
60 $ldap_server = ldap_servers_get_servers($sid, NULL, TRUE, TRUE); // clear server cache;
61 }
62
63 function configureLdapAuthentication($ldap_authentication_test_conf_id, $sids) {
64 module_load_include('php', 'ldap_authentication', 'LdapAuthenticationConfAdmin.class');
65 $options = $this->data['ldap_authentication'][$ldap_authentication_test_conf_id];
66 foreach ($sids as $i => $sid) {
67 $options['sids'][$sid] = $sid;
68 }
69 $ldapServerAdmin = new LdapAuthenticationConfAdmin();
70 foreach ($ldapServerAdmin->saveable as $prop_name) {
71 if (isset($options[$prop_name])) {
72 $ldapServerAdmin->{$prop_name} = $options[$prop_name];
73 }
74 }
75 $ldapServerAdmin->save();
76 }
77
78 function configureLdapUser($ldap_user_test_conf_id) {
79 module_load_include('php', 'ldap_user', 'LdapUserConfAdmin.class');
80 $ldapUserConfAdmin = new LdapUserConfAdmin();
81 $options = $this->data['ldap_user'][$ldap_user_test_conf_id];
82 foreach ($ldapUserConfAdmin->saveable as $prop_name) {
83 if (isset($options[$prop_name])) {
84 $ldapUserConfAdmin->{$prop_name} = $options[$prop_name];
85 }
86 }
87 $ldapUserConfAdmin->save();
88 }
89
90 function prepConsumerConf($consumer_confs) {
91 // create consumer authorization configuration.
92 foreach ($consumer_confs as $consumer_type => $consumer_conf) {
93 $consumer_obj = ldap_authorization_get_consumer_object($consumer_type);
94 $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer_obj, TRUE);
95 foreach ($consumer_conf as $property_name => $property_value) {
96 $consumer_conf_admin->{$property_name} = $property_value;
97 }
98 foreach ($consumer_conf_admin->mappings as $i => $mapping) {
99 $mappings = $consumer_obj->normalizeMappings(
100 array(
101 array($mapping['from'], $mapping['user_entered'])
102 )
103 , FALSE);
104 $consumer_conf_admin->mappings[$i] = $mappings[0];
105 }
106 $consumer_conf_admin->save();
107 }
108 }
109
110
111 function ldapUserIsAuthmapped($username) {
112 $authmaps = user_get_authmaps($username);
113 return ($authmaps && in_array('ldap_user', array_keys($authmaps)));
114 }
115
116 function drupalLdapUpdateUser($edit = array(), $ldap_authenticated = FALSE, $user) {
117 if (count($edit)) {
118 $user = user_save($user, $edit);
119 }
120 if ($ldap_authenticated) {
121 user_set_authmaps($user, array('authname_ldap_user' => $user->name));
122 }
123 return $user;
124 }
125 // from http://www.midwesternmac.com/blogs/jeff-geerling/programmatically-adding-roles
126 public function removeRoleFromUser($user, $role_name) {
127
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;
142 }
143 }
144 user_save($user, array('roles' => $new_roles));
145 }
146 }
147 }
148
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 }
155
156 /**
157 * set variable with fake test data
158 *
159 * @param string $test_ldap_id eg. 'hogwarts'
160 * @param string $test_ldap_type e.g. openLdap, openLdapTest1, etc.
161 * @parma string $sid where fake data is stored. e.g. 'default',
162 */
163 public function populateFakeLdapServerData($test_ldap_id, $sid = 'default') {
164
165 // read csvs into key/value array
166 // create fake ldap data array
167 $clones = empty($this->data['ldap_servers'][$sid]['clones']) ? FALSE : $this->data['ldap_servers'][$sid]['clones'];
168 $server_properties = $this->data['ldap_servers'][$sid]['properties'];
169 $this->getCsvLdapData($test_ldap_id);
170 foreach ($this->csvTables['users'] as $guid => $user) {
171 $dn = 'cn=' . $user['cn'] . ',' . $this->csvTables['conf'][$test_ldap_id]['userbasedn'];
172 $this->csvTables['users'][$guid]['dn'] = $dn;
173 $attributes = $this->generateUserLDAPAttributes($test_ldap_id, $user);
174 $this->addLDAPUserToLDAPArraysFromAttributes(
175 $user,
176 $sid,
177 $dn,
178 $attributes,
179 $server_properties['ldap_type'],
180 $server_properties['user_attr']
181 ) ;
182 }
183
184 if ($clones) {
185 $clonable_user = $this->csvTables['users'][101];
186 for ($i=0; $i < $clones; $i++) {
187 $user = $clonable_user;
188 $cn = "clone" . $i;
189 $dn = 'cn=' . $cn . ',' . $this->csvTables['conf'][$test_ldap_id]['userbasedn'];
190 $user['cn'] = $cn;
191 $user['dn'] = $dn;
192 $user['uid'] = 20 + $i;
193 $user['guid'] = 120 + $i;
194 $user['lname'] = $user['lname'] . "_$i";
195 $attributes = $this->generateUserLDAPAttributes($test_ldap_id, $user);
196 $this->addLDAPUserToLDAPArraysFromAttributes(
197 $user,
198 $sid,
199 $dn,
200 $attributes,
201 $server_properties['ldap_type'],
202 $server_properties['user_attr']
203 );
204 }
205 }
206
207 foreach ($this->csvTables['groups'] as $guid => $group) {
208 $dn = 'cn=' . $group['cn'] . ',' . $this->csvTables['conf'][$test_ldap_id]['groupbasedn'];
209 $this->csvTables['groups'][$guid]['dn'] = $dn;
210 $attributes = array(
211 'cn' => array(
212 0 => $group['cn'],
213 'count' => 1,
214 ),
215 'gid' => array(
216 0 => $group['gid'],
217 'count' => 1,
218 ),
219 'guid' => array(
220 0 => $guid,
221 'count' => 1,
222 ),
223 );
224
225 if ($server_properties['groupMembershipsAttr']) {
226 $membershipAttr = $server_properties['groupMembershipsAttr'];
227 foreach ($this->csvTables['memberships'] as $membership_id => $membership) {
228 if ($membership['gid'] == $group['gid']) {
229 $member_guid = $membership['member_guid'];
230 if (isset($this->csvTables['users'][$member_guid])) {
231 $member = $this->csvTables['users'][$member_guid];
232 }
233 elseif (isset($this->csvTables['groups'][$member_guid])) {
234 $member = $this->csvTables['groups'][$member_guid];
235 }
236 if ($server_properties['groupMembershipsAttrMatchingUserAttr'] == 'dn') {
237 $attributes[$server_properties['groupMembershipsAttr']][] = $member['dn'];
238 }
239 else {
240 $attributes[$server_properties['groupMembershipsAttr']][] = $member['attr'][$membershipAttr][0];
241 }
242 }
243 }
244 $attributes[$membershipAttr]['count'] = count($attributes[$membershipAttr]);
245
246 }
247 // need to figure out if memberOf type attribute is desired and populate it
248 $this->data['ldap_servers_by_guid'][$sid][$group['guid']]['attr'] = $attributes;
249 $this->data['ldap_servers_by_guid'][$sid][$group['guid']]['dn'] = $dn;
250 $this->data['ldap_servers'][$sid]['groups'][$dn]['attr'] = $attributes;
251 $this->ldapData['ldap_servers'][$sid][$dn] = $attributes;
252
253 }
254 if ($server_properties['groupUserMembershipsAttrExists']) {
255 $member_attr = $server_properties['groupUserMembershipsAttr'];
256 foreach ($this->csvTables['memberships'] as $gid => $membership) {
257 $group_dn = $this->data['ldap_servers_by_guid'][$sid][$membership['group_guid']]['dn'];
258 $user_dn = $this->data['ldap_servers_by_guid'][$sid][$membership['member_guid']]['dn'];
259 $this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr][] = $group_dn;
260 if (isset($this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count'])) {
261 unset($this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count']);
262 }
263 $this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]['count'] =
264 count( $this->ldapData['ldap_servers'][$sid][$user_dn][$member_attr]);
265 }
266 }
267
268 $this->data['ldap_servers'][$sid]['ldap'] = $this->ldapData['ldap_servers'][$sid];
269 $this->data['ldap_servers'][$sid]['csv'] = $this->csvTables;
270 variable_set('ldap_test_server__' . $sid, $this->data['ldap_servers'][$sid]);
271 $current_sids = variable_get('ldap_test_servers', array());
272 $current_sids[] = $sid;
273 variable_set('ldap_test_servers', array_unique($current_sids));
274 }
275
276 public function generateUserLDAPAttributes($test_ldap_id, $user) {
277 $attributes = array(
278 'cn' => array(
279 0 => $user['cn'],
280 'count' => 1,
281 ),
282 'mail' => array(
283 0 => $user['cn'] . '@' . $this->csvTables['conf'][$test_ldap_id]['mailhostname'],
284 'count' => 1,
285 ),
286 'uid' => array(
287 0 => $user['uid'],
288 'count' => 1,
289 ),
290 'guid' => array(
291 0 => $user['guid'],
292 'count' => 1,
293 ),
294 'sn' => array(
295 0 => $user['lname'],
296 'count' => 1,
297 ),
298 'givenname' => array(
299 0 => $user['fname'],
300 'count' => 1,
301 ),
302 'house' => array(
303 0 => $user['house'],
304 'count' => 1,
305 ),
306 'department' => array(
307 0 => $user['department'],
308 'count' => 1,
309 ),
310 'faculty' => array(
311 0 => (int)(boolean)$user['faculty'],
312 'count' => 1,
313 ),
314 'staff' => array(
315 0 => (int)(boolean)$user['staff'],
316 'count' => 1,
317 ),
318 'student' => array(
319 0 => (int)(boolean)$user['student'],
320 'count' => 1,
321 ),
322 'gpa' => array(
323 0 => $user['gpa'],
324 'count' => 1,
325 ),
326 'probation' => array(
327 0 => (int)(boolean)$user['probation'],
328 'count' => 1,
329 ),
330 'password' => array(
331 0 => 'goodpwd',
332 'count' => 1,
333 ),
334 );
335 return $attributes;
336 }
337
338 public function addLDAPUserToLDAPArraysFromAttributes($user, $sid, $dn, $attributes, $ldap_type, $user_attr) {
339
340 if ($ldap_type == 'activedirectory') {
341 $attributes[$user_attr] = array(0 => $user['cn'], 'count' => 1);
342 $attributes['distinguishedname'] = array( 0 => $dn, 'count' => 1);
343 }
344 elseif ($ldap_type == 'openldap') {
345
346 }
347
348 $this->data['ldap_servers'][$sid]['users'][$dn]['attr'] = $attributes;
349 $this->data['ldap_servers_by_guid'][$sid][$user['guid']]['attr'] = $attributes;
350 $this->data['ldap_servers_by_guid'][$sid][$user['guid']]['dn'] = $dn;
351 $this->ldapData['ldap_servers'][$sid][$dn] = $attributes;
352 $this->ldapData['ldap_servers'][$sid][$dn]['count'] = count($attributes);
353 }
354
355 public function getCsvLdapData($test_ldap_id) {
356 foreach (array('groups', 'users', 'memberships', 'conf') as $type) {
357 $path = drupal_get_path('module', 'ldap_test') . '/test_ldap/' . $test_ldap_id . '/' . $type . '.csv';
358 $this->csvTables[$type] = $this->parseCsv($path);
359 }
360 }
361
362 public function parseCsv($filepath) {
363 $row = 1;
364 $table = array();
365 if (($handle = fopen($filepath, "r")) !== FALSE) {
366 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
367 if (count($data) > 1) {
368 $table[] = $data;
369 }
370 }
371 fclose($handle);
372 }
373
374 $table_associative = array();
375 $headings = array_shift($table);
376 foreach ($table as $i => $row) {
377 $row_id = $row[0];
378 foreach ($row as $j => $item) {
379 $table_associative[$row_id][$headings[$j]] = $item;
380 }
381 }
382
383 return $table_associative;
384
385 }
386
387 }