mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
Merge branch 'w28_MDL-40243_m26_ldapsuspend' of https://github.com/skodak/moodle
This commit is contained in:
commit
8a011a9f0a
@ -747,39 +747,57 @@ class auth_plugin_ldap extends auth_plugin_base {
|
||||
/// User removal
|
||||
// Find users in DB that aren't in ldap -- to be removed!
|
||||
// this is still not as scalable (but how often do we mass delete?)
|
||||
if ($this->config->removeuser != AUTH_REMOVEUSER_KEEP) {
|
||||
$sql = 'SELECT u.*
|
||||
|
||||
if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
|
||||
$sql = "SELECT u.*
|
||||
FROM {user} u
|
||||
LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
|
||||
WHERE u.auth = ?
|
||||
LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
|
||||
WHERE u.auth = :auth
|
||||
AND u.deleted = 0
|
||||
AND e.username IS NULL';
|
||||
$remove_users = $DB->get_records_sql($sql, array($this->authtype));
|
||||
AND e.username IS NULL";
|
||||
$remove_users = $DB->get_records_sql($sql, array('auth'=>$this->authtype));
|
||||
|
||||
if (!empty($remove_users)) {
|
||||
print_string('userentriestoremove', 'auth_ldap', count($remove_users));
|
||||
|
||||
foreach ($remove_users as $user) {
|
||||
if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
|
||||
if (delete_user($user)) {
|
||||
echo "\t"; print_string('auth_dbdeleteuser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
|
||||
} else {
|
||||
echo "\t"; print_string('auth_dbdeleteusererror', 'auth_db', $user->username); echo "\n";
|
||||
}
|
||||
} else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
|
||||
$updateuser = new stdClass();
|
||||
$updateuser->id = $user->id;
|
||||
$updateuser->auth = 'nologin';
|
||||
$DB->update_record('user', $updateuser);
|
||||
echo "\t"; print_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
|
||||
$euser = $DB->get_record('user', array('id' => $user->id));
|
||||
events_trigger('user_updated', $euser);
|
||||
if (delete_user($user)) {
|
||||
echo "\t"; print_string('auth_dbdeleteuser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
|
||||
} else {
|
||||
echo "\t"; print_string('auth_dbdeleteusererror', 'auth_db', $user->username); echo "\n";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print_string('nouserentriestoremove', 'auth_ldap');
|
||||
}
|
||||
unset($remove_users); // free mem!
|
||||
unset($remove_users); // Free mem!
|
||||
|
||||
} else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
|
||||
$sql = "SELECT u.*
|
||||
FROM {user} u
|
||||
LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
|
||||
WHERE u.auth = :auth
|
||||
AND u.deleted = 0
|
||||
AND u.suspended = 0
|
||||
AND e.username IS NULL";
|
||||
$remove_users = $DB->get_records_sql($sql, array('auth'=>$this->authtype));
|
||||
|
||||
if (!empty($remove_users)) {
|
||||
print_string('userentriestoremove', 'auth_ldap', count($remove_users));
|
||||
|
||||
foreach ($remove_users as $user) {
|
||||
$updateuser = new stdClass();
|
||||
$updateuser->id = $user->id;
|
||||
$updateuser->suspended = 1;
|
||||
$DB->update_record('user', $updateuser);
|
||||
echo "\t"; print_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
|
||||
$euser = $DB->get_record('user', array('id' => $user->id));
|
||||
events_trigger('user_updated', $euser);
|
||||
session_kill_user($user->id);
|
||||
}
|
||||
} else {
|
||||
print_string('nouserentriestoremove', 'auth_ldap');
|
||||
}
|
||||
unset($remove_users); // Free mem!
|
||||
}
|
||||
|
||||
/// Revive suspended users
|
||||
@ -787,8 +805,9 @@ class auth_plugin_ldap extends auth_plugin_base {
|
||||
$sql = "SELECT u.id, u.username
|
||||
FROM {user} u
|
||||
JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
|
||||
WHERE u.auth = 'nologin' AND u.deleted = 0";
|
||||
$revive_users = $DB->get_records_sql($sql);
|
||||
WHERE (u.auth = 'nologin' OR (u.auth = ? AND u.suspended = 1)) AND u.deleted = 0";
|
||||
// Note: 'nologin' is there for backwards compatibility.
|
||||
$revive_users = $DB->get_records_sql($sql, array($this->authtype));
|
||||
|
||||
if (!empty($revive_users)) {
|
||||
print_string('userentriestorevive', 'auth_ldap', count($revive_users));
|
||||
@ -797,6 +816,7 @@ class auth_plugin_ldap extends auth_plugin_base {
|
||||
$updateuser = new stdClass();
|
||||
$updateuser->id = $user->id;
|
||||
$updateuser->auth = $this->authtype;
|
||||
$updateuser->suspended = 0;
|
||||
$DB->update_record('user', $updateuser);
|
||||
echo "\t"; print_string('auth_dbreviveduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
|
||||
$euser = $DB->get_record('user', array('id' => $user->id));
|
||||
|
@ -34,7 +34,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
class auth_ldap_testcase extends advanced_testcase {
|
||||
class auth_ldap_plugin_testcase extends advanced_testcase {
|
||||
|
||||
public function test_auth_ldap() {
|
||||
global $CFG, $DB;
|
||||
@ -169,9 +169,9 @@ class auth_ldap_testcase extends advanced_testcase {
|
||||
$auth->sync_users(true);
|
||||
ob_end_clean();
|
||||
|
||||
$this->assertEquals(4, $DB->count_records('user', array('auth'=>'ldap')));
|
||||
$this->assertEquals(1, $DB->count_records('user', array('auth'=>'nologin', 'username'=>'username1')));
|
||||
$this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
|
||||
$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
|
||||
$this->assertEquals(0, $DB->count_records('user', array('auth'=>'nologin', 'username'=>'username1')));
|
||||
$this->assertEquals(1, $DB->count_records('user', array('auth'=>'ldap', 'suspended'=>'1', 'username'=>'username1')));
|
||||
$this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
|
||||
@ -188,6 +188,17 @@ class auth_ldap_testcase extends advanced_testcase {
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
|
||||
|
||||
$DB->set_field('user', 'auth', 'nologin', array('username'=>'username1'));
|
||||
|
||||
ob_start();
|
||||
$auth->sync_users(true);
|
||||
ob_end_clean();
|
||||
|
||||
$this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
|
||||
$this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
|
||||
$this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
|
||||
|
||||
set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth/ldap');
|
||||
|
Loading…
x
Reference in New Issue
Block a user