17ee9dd9b26d30cb3eb95be7da7844b3be724d13
[moodle.git] / auth / ldap / tests / plugin_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * LDAP authentication plugin tests.
19  *
20  * NOTE: in order to execute this test you need to set up
21  *       OpenLDAP server with core, cosine, nis and internet schemas
22  *       and add configuration constants to config.php or phpunit.xml configuration file:
23  *
24  * define('TEST_AUTH_LDAP_HOST_URL', 'ldap://127.0.0.1');
25  * define('TEST_AUTH_LDAP_BIND_DN', 'cn=someuser,dc=example,dc=local');
26  * define('TEST_AUTH_LDAP_BIND_PW', 'somepassword');
27  * define('TEST_AUTH_LDAP_DOMAIN', 'dc=example,dc=local');
28  *
29  * @package    auth_ldap
30  * @category   phpunit
31  * @copyright  2013 Petr Skoda {@link http://skodak.org}
32  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33  */
35 defined('MOODLE_INTERNAL') || die();
37 class auth_ldap_plugin_testcase extends advanced_testcase {
39     /**
40      * Data provider for auth_ldap tests
41      *
42      * Used to ensure that all the paged stuff works properly, irrespectively
43      * of the pagesize configured (that implies all the chunking and paging
44      * built in the plugis is doing its work consistently). Both searching and
45      * not searching within subcontexts.
46      *
47      * @return array[]
48      */
49     public function auth_ldap_provider() {
50         $pagesizes = [1, 3, 5, 1000];
51         $subcontexts = [0, 1];
52         $combinations = [];
53         foreach ($pagesizes as $pagesize) {
54             foreach ($subcontexts as $subcontext) {
55                 $combinations["pagesize {$pagesize}, subcontexts {$subcontext}"] = [$pagesize, $subcontext];
56             }
57         }
58         return $combinations;
59     }
61     /**
62      * General auth_ldap testcase
63      *
64      * @dataProvider auth_ldap_provider
65      * @param int $pagesize Value to be configured in settings controlling page size.
66      * @param int $subcontext Value to be configured in settings controlling searching in subcontexts.
67      */
68     public function test_auth_ldap(int $pagesize, int $subcontext) {
69         global $CFG, $DB;
71         if (!extension_loaded('ldap')) {
72             $this->markTestSkipped('LDAP extension is not loaded.');
73         }
75         $this->resetAfterTest();
77         require_once($CFG->dirroot.'/auth/ldap/auth.php');
78         require_once($CFG->libdir.'/ldaplib.php');
80         if (!defined('TEST_AUTH_LDAP_HOST_URL') or !defined('TEST_AUTH_LDAP_BIND_DN') or !defined('TEST_AUTH_LDAP_BIND_PW') or !defined('TEST_AUTH_LDAP_DOMAIN')) {
81             $this->markTestSkipped('External LDAP test server not configured.');
82         }
84         // Make sure we can connect the server.
85         $debuginfo = '';
86         if (!$connection = ldap_connect_moodle(TEST_AUTH_LDAP_HOST_URL, 3, 'rfc2307', TEST_AUTH_LDAP_BIND_DN, TEST_AUTH_LDAP_BIND_PW, LDAP_DEREF_NEVER, $debuginfo, false)) {
87             $this->markTestSkipped('Can not connect to LDAP test server: '.$debuginfo);
88         }
90         $this->enable_plugin();
92         // Create new empty test container.
93         $topdn = 'dc=moodletest,'.TEST_AUTH_LDAP_DOMAIN;
95         $this->recursive_delete($connection, TEST_AUTH_LDAP_DOMAIN, 'dc=moodletest');
97         $o = array();
98         $o['objectClass'] = array('dcObject', 'organizationalUnit');
99         $o['dc']         = 'moodletest';
100         $o['ou']         = 'MOODLETEST';
101         if (!ldap_add($connection, 'dc=moodletest,'.TEST_AUTH_LDAP_DOMAIN, $o)) {
102             $this->markTestSkipped('Can not create test LDAP container.');
103         }
105         // Create a few users.
106         $o = array();
107         $o['objectClass'] = array('organizationalUnit');
108         $o['ou']          = 'users';
109         ldap_add($connection, 'ou='.$o['ou'].','.$topdn, $o);
111         for ($i=1; $i<=5; $i++) {
112             $this->create_ldap_user($connection, $topdn, $i);
113         }
115         // Set up creators group.
116         $o = array();
117         $o['objectClass'] = array('posixGroup');
118         $o['cn']          = 'creators';
119         $o['gidNumber']   = 1;
120         $o['memberUid']   = array('username1', 'username2');
121         ldap_add($connection, 'cn='.$o['cn'].','.$topdn, $o);
123         $creatorrole = $DB->get_record('role', array('shortname'=>'coursecreator'));
124         $this->assertNotEmpty($creatorrole);
127         // Configure the plugin a bit.
128         set_config('host_url', TEST_AUTH_LDAP_HOST_URL, 'auth_ldap');
129         set_config('start_tls', 0, 'auth_ldap');
130         set_config('ldap_version', 3, 'auth_ldap');
131         set_config('ldapencoding', 'utf-8', 'auth_ldap');
132         set_config('pagesize', $pagesize, 'auth_ldap');
133         set_config('bind_dn', TEST_AUTH_LDAP_BIND_DN, 'auth_ldap');
134         set_config('bind_pw', TEST_AUTH_LDAP_BIND_PW, 'auth_ldap');
135         set_config('user_type', 'rfc2307', 'auth_ldap');
136         set_config('contexts', 'ou=users,'.$topdn, 'auth_ldap');
137         set_config('search_sub', $subcontext, 'auth_ldap');
138         set_config('opt_deref', LDAP_DEREF_NEVER, 'auth_ldap');
139         set_config('user_attribute', 'cn', 'auth_ldap');
140         set_config('memberattribute', 'memberuid', 'auth_ldap');
141         set_config('memberattribute_isdn', 0, 'auth_ldap');
142         set_config('coursecreatorcontext', 'cn=creators,'.$topdn, 'auth_ldap');
143         set_config('removeuser', AUTH_REMOVEUSER_KEEP, 'auth_ldap');
145         set_config('field_map_email', 'mail', 'auth_ldap');
146         set_config('field_updatelocal_email', 'oncreate', 'auth_ldap');
147         set_config('field_updateremote_email', '0', 'auth_ldap');
148         set_config('field_lock_email', 'unlocked', 'auth_ldap');
150         set_config('field_map_firstname', 'givenName', 'auth_ldap');
151         set_config('field_updatelocal_firstname', 'oncreate', 'auth_ldap');
152         set_config('field_updateremote_firstname', '0', 'auth_ldap');
153         set_config('field_lock_firstname', 'unlocked', 'auth_ldap');
155         set_config('field_map_lastname', 'sn', 'auth_ldap');
156         set_config('field_updatelocal_lastname', 'oncreate', 'auth_ldap');
157         set_config('field_updateremote_lastname', '0', 'auth_ldap');
158         set_config('field_lock_lastname', 'unlocked', 'auth_ldap');
161         $this->assertEquals(2, $DB->count_records('user'));
162         $this->assertEquals(0, $DB->count_records('role_assignments'));
164         /** @var auth_plugin_ldap $auth */
165         $auth = get_auth_plugin('ldap');
167         ob_start();
168         $sink = $this->redirectEvents();
169         $auth->sync_users(true);
170         $events = $sink->get_events();
171         $sink->close();
172         ob_end_clean();
174         // Check events, 5 users created with 2 users having roles.
175         $this->assertCount(7, $events);
176         foreach ($events as $index => $event) {
177             $usercreatedindex = array(0, 2, 4, 5, 6);
178             $roleassignedindex = array (1, 3);
179             if (in_array($index, $usercreatedindex)) {
180                 $this->assertInstanceOf('\core\event\user_created', $event);
181             }
182             if (in_array($index, $roleassignedindex)) {
183                 $this->assertInstanceOf('\core\event\role_assigned', $event);
184             }
185         }
187         $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
188         $this->assertEquals(2, $DB->count_records('role_assignments'));
189         $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
191         for ($i=1; $i<=5; $i++) {
192             $this->assertTrue($DB->record_exists('user', array('username'=>'username'.$i, 'email'=>'user'.$i.'@example.com', 'firstname'=>'Firstname'.$i, 'lastname'=>'Lastname'.$i)));
193         }
195         $this->delete_ldap_user($connection, $topdn, 1);
197         ob_start();
198         $sink = $this->redirectEvents();
199         $auth->sync_users(true);
200         $events = $sink->get_events();
201         $sink->close();
202         ob_end_clean();
204         // Check events, no new event.
205         $this->assertCount(0, $events);
207         $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
208         $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
209         $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
210         $this->assertEquals(2, $DB->count_records('role_assignments'));
211         $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
214         set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth_ldap');
216         /** @var auth_plugin_ldap $auth */
217         $auth = get_auth_plugin('ldap');
219         ob_start();
220         $sink = $this->redirectEvents();
221         $auth->sync_users(true);
222         $events = $sink->get_events();
223         $sink->close();
224         ob_end_clean();
226         // Check events, 1 user got updated.
227         $this->assertCount(1, $events);
228         $event = reset($events);
229         $this->assertInstanceOf('\core\event\user_updated', $event);
231         $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
232         $this->assertEquals(0, $DB->count_records('user', array('auth'=>'nologin', 'username'=>'username1')));
233         $this->assertEquals(1, $DB->count_records('user', array('auth'=>'ldap', 'suspended'=>'1', 'username'=>'username1')));
234         $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
235         $this->assertEquals(2, $DB->count_records('role_assignments'));
236         $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
238         $this->create_ldap_user($connection, $topdn, 1);
240         ob_start();
241         $sink = $this->redirectEvents();
242         $auth->sync_users(true);
243         $events = $sink->get_events();
244         $sink->close();
245         ob_end_clean();
247         // Check events, 1 user got updated.
248         $this->assertCount(1, $events);
249         $event = reset($events);
250         $this->assertInstanceOf('\core\event\user_updated', $event);
252         $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
253         $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
254         $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
255         $this->assertEquals(2, $DB->count_records('role_assignments'));
256         $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
258         $DB->set_field('user', 'auth', 'nologin', array('username'=>'username1'));
260         ob_start();
261         $sink = $this->redirectEvents();
262         $auth->sync_users(true);
263         $events = $sink->get_events();
264         $sink->close();
265         ob_end_clean();
267         // Check events, 1 user got updated.
268         $this->assertCount(1, $events);
269         $event = reset($events);
270         $this->assertInstanceOf('\core\event\user_updated', $event);
272         $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
273         $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
274         $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
275         $this->assertEquals(2, $DB->count_records('role_assignments'));
276         $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
278         set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth_ldap');
280         /** @var auth_plugin_ldap $auth */
281         $auth = get_auth_plugin('ldap');
283         $this->delete_ldap_user($connection, $topdn, 1);
285         ob_start();
286         $sink = $this->redirectEvents();
287         $auth->sync_users(true);
288         $events = $sink->get_events();
289         $sink->close();
290         ob_end_clean();
292         // Check events, 2 events role_unassigned and user_deleted.
293         $this->assertCount(2, $events);
294         $event = array_pop($events);
295         $this->assertInstanceOf('\core\event\user_deleted', $event);
296         $event = array_pop($events);
297         $this->assertInstanceOf('\core\event\role_unassigned', $event);
299         $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
300         $this->assertEquals(0, $DB->count_records('user', array('username'=>'username1')));
301         $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
302         $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
303         $this->assertEquals(1, $DB->count_records('role_assignments'));
304         $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
306         $this->create_ldap_user($connection, $topdn, 1);
308         ob_start();
309         $sink = $this->redirectEvents();
310         $auth->sync_users(true);
311         $events = $sink->get_events();
312         $sink->close();
313         ob_end_clean();
315         // Check events, 2 events role_assigned and user_created.
316         $this->assertCount(2, $events);
317         $event = array_pop($events);
318         $this->assertInstanceOf('\core\event\role_assigned', $event);
319         $event = array_pop($events);
320         $this->assertInstanceOf('\core\event\user_created', $event);
322         $this->assertEquals(6, $DB->count_records('user', array('auth'=>'ldap')));
323         $this->assertEquals(1, $DB->count_records('user', array('username'=>'username1')));
324         $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
325         $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
326         $this->assertEquals(2, $DB->count_records('role_assignments'));
327         $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
330         $this->recursive_delete($connection, TEST_AUTH_LDAP_DOMAIN, 'dc=moodletest');
331         ldap_close($connection);
332     }
334     /**
335      * Test logging in via LDAP calls a user_loggedin event.
336      */
337     public function test_ldap_user_loggedin_event() {
338         global $CFG, $DB, $USER;
340         require_once($CFG->dirroot . '/auth/ldap/auth.php');
342         $this->resetAfterTest();
344         $this->assertFalse(isloggedin());
345         $user = $DB->get_record('user', array('username'=>'admin'));
347         // Note: we are just going to trigger the function that calls the event,
348         // not actually perform a LDAP login, for the sake of sanity.
349         $ldap = new auth_plugin_ldap();
351         // Set the key for the cache flag we want to set which is used by LDAP.
352         set_cache_flag($ldap->pluginconfig . '/ntlmsess', sesskey(), $user->username, AUTH_NTLMTIMEOUT);
354         // We are going to need to set the sesskey as the user's password in order for the LDAP log in to work.
355         update_internal_user_password($user, sesskey());
357         // The function ntlmsso_finish is responsible for triggering the event, so call it directly and catch the event.
358         $sink = $this->redirectEvents();
359         // We need to supress this function call, or else we will get the message "session_regenerate_id(): Cannot
360         // regenerate session id - headers already sent" as the ntlmsso_finish function calls complete_user_login
361         @$ldap->ntlmsso_finish();
362         $events = $sink->get_events();
363         $sink->close();
365         // Check that the event is valid.
366         $this->assertCount(1, $events);
367         $event = reset($events);
368         $this->assertInstanceOf('\core\event\user_loggedin', $event);
369         $this->assertEquals('user', $event->objecttable);
370         $this->assertEquals('2', $event->objectid);
371         $this->assertEquals(context_system::instance()->id, $event->contextid);
372         $expectedlog = array(SITEID, 'user', 'login', 'view.php?id=' . $USER->id . '&course=' . SITEID, $user->id,
373             0, $user->id);
374         $this->assertEventLegacyLogData($expectedlog, $event);
375     }
377     /**
378      * Test logging in via LDAP calls a user_loggedin event.
379      */
380     public function test_ldap_user_signup() {
381         global $CFG, $DB;
383         // User to create.
384         $user = array(
385             'username' => 'usersignuptest1',
386             'password' => 'Moodle2014!',
387             'idnumber' => 'idsignuptest1',
388             'firstname' => 'First Name User Test 1',
389             'lastname' => 'Last Name User Test 1',
390             'middlename' => 'Middle Name User Test 1',
391             'lastnamephonetic' => '最後のお名前のテスト一号',
392             'firstnamephonetic' => 'お名前のテスト一号',
393             'alternatename' => 'Alternate Name User Test 1',
394             'email' => 'usersignuptest1@example.com',
395             'description' => 'This is a description for user 1',
396             'city' => 'Perth',
397             'country' => 'AU',
398             'mnethostid' => $CFG->mnet_localhost_id,
399             'auth' => 'ldap'
400             );
402         if (!extension_loaded('ldap')) {
403             $this->markTestSkipped('LDAP extension is not loaded.');
404         }
406         $this->resetAfterTest();
408         require_once($CFG->dirroot.'/auth/ldap/auth.php');
409         require_once($CFG->libdir.'/ldaplib.php');
411         if (!defined('TEST_AUTH_LDAP_HOST_URL') or !defined('TEST_AUTH_LDAP_BIND_DN') or !defined('TEST_AUTH_LDAP_BIND_PW') or !defined('TEST_AUTH_LDAP_DOMAIN')) {
412             $this->markTestSkipped('External LDAP test server not configured.');
413         }
415         // Make sure we can connect the server.
416         $debuginfo = '';
417         if (!$connection = ldap_connect_moodle(TEST_AUTH_LDAP_HOST_URL, 3, 'rfc2307', TEST_AUTH_LDAP_BIND_DN, TEST_AUTH_LDAP_BIND_PW, LDAP_DEREF_NEVER, $debuginfo, false)) {
418             $this->markTestSkipped('Can not connect to LDAP test server: '.$debuginfo);
419         }
421         $this->enable_plugin();
423         // Create new empty test container.
424         $topdn = 'dc=moodletest,'.TEST_AUTH_LDAP_DOMAIN;
426         $this->recursive_delete($connection, TEST_AUTH_LDAP_DOMAIN, 'dc=moodletest');
428         $o = array();
429         $o['objectClass'] = array('dcObject', 'organizationalUnit');
430         $o['dc']         = 'moodletest';
431         $o['ou']         = 'MOODLETEST';
432         if (!ldap_add($connection, 'dc=moodletest,'.TEST_AUTH_LDAP_DOMAIN, $o)) {
433             $this->markTestSkipped('Can not create test LDAP container.');
434         }
436         // Create a few users.
437         $o = array();
438         $o['objectClass'] = array('organizationalUnit');
439         $o['ou']          = 'users';
440         ldap_add($connection, 'ou='.$o['ou'].','.$topdn, $o);
442         // Configure the plugin a bit.
443         set_config('host_url', TEST_AUTH_LDAP_HOST_URL, 'auth_ldap');
444         set_config('start_tls', 0, 'auth_ldap');
445         set_config('ldap_version', 3, 'auth_ldap');
446         set_config('ldapencoding', 'utf-8', 'auth_ldap');
447         set_config('pagesize', '2', 'auth_ldap');
448         set_config('bind_dn', TEST_AUTH_LDAP_BIND_DN, 'auth_ldap');
449         set_config('bind_pw', TEST_AUTH_LDAP_BIND_PW, 'auth_ldap');
450         set_config('user_type', 'rfc2307', 'auth_ldap');
451         set_config('contexts', 'ou=users,'.$topdn, 'auth_ldap');
452         set_config('search_sub', 0, 'auth_ldap');
453         set_config('opt_deref', LDAP_DEREF_NEVER, 'auth_ldap');
454         set_config('user_attribute', 'cn', 'auth_ldap');
455         set_config('memberattribute', 'memberuid', 'auth_ldap');
456         set_config('memberattribute_isdn', 0, 'auth_ldap');
457         set_config('creators', 'cn=creators,'.$topdn, 'auth_ldap');
458         set_config('removeuser', AUTH_REMOVEUSER_KEEP, 'auth_ldap');
460         set_config('field_map_email', 'mail', 'auth_ldap');
461         set_config('field_updatelocal_email', 'oncreate', 'auth_ldap');
462         set_config('field_updateremote_email', '0', 'auth_ldap');
463         set_config('field_lock_email', 'unlocked', 'auth_ldap');
465         set_config('field_map_firstname', 'givenName', 'auth_ldap');
466         set_config('field_updatelocal_firstname', 'oncreate', 'auth_ldap');
467         set_config('field_updateremote_firstname', '0', 'auth_ldap');
468         set_config('field_lock_firstname', 'unlocked', 'auth_ldap');
470         set_config('field_map_lastname', 'sn', 'auth_ldap');
471         set_config('field_updatelocal_lastname', 'oncreate', 'auth_ldap');
472         set_config('field_updateremote_lastname', '0', 'auth_ldap');
473         set_config('field_lock_lastname', 'unlocked', 'auth_ldap');
474         set_config('passtype', 'md5', 'auth_ldap');
475         set_config('create_context', 'ou=users,'.$topdn, 'auth_ldap');
477         $this->assertEquals(2, $DB->count_records('user'));
478         $this->assertEquals(0, $DB->count_records('role_assignments'));
480         /** @var auth_plugin_ldap $auth */
481         $auth = get_auth_plugin('ldap');
483         $sink = $this->redirectEvents();
484         $mailsink = $this->redirectEmails();
485         $auth->user_signup((object)$user, false);
486         $this->assertEquals(1, $mailsink->count());
487         $events = $sink->get_events();
488         $sink->close();
490         // Verify 2 events get generated.
491         $this->assertCount(2, $events);
493         // Get record from db.
494         $dbuser = $DB->get_record('user', array('username' => $user['username']));
495         $user['id'] = $dbuser->id;
497         // Last event is user_created.
498         $event = array_pop($events);
499         $this->assertInstanceOf('\core\event\user_created', $event);
500         $this->assertEquals($user['id'], $event->objectid);
501         $this->assertEquals('user_created', $event->get_legacy_eventname());
502         $this->assertEquals(context_user::instance($user['id']), $event->get_context());
503         $expectedlogdata = array(SITEID, 'user', 'add', '/view.php?id='.$event->objectid, fullname($dbuser));
504         $this->assertEventLegacyLogData($expectedlogdata, $event);
506         // First event is user_password_updated.
507         $event = array_pop($events);
508         $this->assertInstanceOf('\core\event\user_password_updated', $event);
509         $this->assertEventContextNotUsed($event);
511         // Delete user which we just created.
512         ldap_delete($connection, 'cn='.$user['username'].',ou=users,'.$topdn);
513     }
515     protected function create_ldap_user($connection, $topdn, $i) {
516         $o = array();
517         $o['objectClass']   = array('inetOrgPerson', 'organizationalPerson', 'person', 'posixAccount');
518         $o['cn']            = 'username'.$i;
519         $o['sn']            = 'Lastname'.$i;
520         $o['givenName']     = 'Firstname'.$i;
521         $o['uid']           = $o['cn'];
522         $o['uidnumber']     = 2000+$i;
523         $o['gidNumber']     = 1000+$i;
524         $o['homeDirectory'] = '/';
525         $o['mail']          = 'user'.$i.'@example.com';
526         $o['userPassword']  = 'pass'.$i;
527         ldap_add($connection, 'cn='.$o['cn'].',ou=users,'.$topdn, $o);
528     }
530     protected function delete_ldap_user($connection, $topdn, $i) {
531         ldap_delete($connection, 'cn=username'.$i.',ou=users,'.$topdn);
532     }
534     protected function enable_plugin() {
535         $auths = get_enabled_auth_plugins(true);
536         if (!in_array('ldap', $auths)) {
537             $auths[] = 'ldap';
539         }
540         set_config('auth', implode(',', $auths));
541     }
543     protected function recursive_delete($connection, $dn, $filter) {
544         if ($res = ldap_list($connection, $dn, $filter, array('dn'))) {
545             $info = ldap_get_entries($connection, $res);
546             ldap_free_result($res);
547             if ($info['count'] > 0) {
548                 if ($res = ldap_search($connection, "$filter,$dn", 'cn=*', array('dn'))) {
549                     $info = ldap_get_entries($connection, $res);
550                     ldap_free_result($res);
551                     foreach ($info as $i) {
552                         if (isset($i['dn'])) {
553                             ldap_delete($connection, $i['dn']);
554                         }
555                     }
556                 }
557                 if ($res = ldap_search($connection, "$filter,$dn", 'ou=*', array('dn'))) {
558                     $info = ldap_get_entries($connection, $res);
559                     ldap_free_result($res);
560                     foreach ($info as $i) {
561                         if (isset($i['dn']) and $info[0]['dn'] != $i['dn']) {
562                             ldap_delete($connection, $i['dn']);
563                         }
564                     }
565                 }
566                 ldap_delete($connection, "$filter,$dn");
567             }
568         }
569     }