diff --git a/auth/db/auth.php b/auth/db/auth.php index 66b63f3f17a..f72b318d3fc 100644 --- a/auth/db/auth.php +++ b/auth/db/auth.php @@ -224,25 +224,31 @@ class auth_plugin_db extends auth_plugin_base { // delete obsolete internal users if (!empty($this->config->removeuser)) { + $suspendselect = ""; + if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) { + $suspendselect = "AND u.suspended = 0"; + } + // find obsolete users if (count($userlist)) { list($notin_sql, $params) = $DB->get_in_or_equal($userlist, SQL_PARAMS_NAMED, 'u', false); $params['authtype'] = $this->authtype; $sql = "SELECT u.* FROM {user} u - WHERE u.auth=:authtype AND u.deleted=0 AND u.username $notin_sql"; + WHERE u.auth=:authtype AND u.deleted=0 AND u.mnethostid=:mnethostid $suspendselect AND u.username $notin_sql"; } else { $sql = "SELECT u.* FROM {user} u - WHERE u.auth=:authtype AND u.deleted=0"; + WHERE u.auth=:authtype AND u.deleted=0 AND u.mnethostid=:mnethostid $suspendselect"; $params = array(); $params['authtype'] = $this->authtype; } + $params['mnethostid'] = $CFG->mnet_localhost_id; $remove_users = $DB->get_records_sql($sql, $params); if (!empty($remove_users)) { if ($verbose) { - mtrace(print_string('auth_dbuserstoremove','auth_db', count($remove_users))); + mtrace(get_string('auth_dbuserstoremove','auth_db', count($remove_users))); } foreach ($remove_users as $user) { @@ -254,7 +260,7 @@ class auth_plugin_db extends auth_plugin_base { } else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) { $updateuser = new stdClass(); $updateuser->id = $user->id; - $updateuser->auth = 'nologin'; + $updateuser->suspended = 1; $updateuser->timemodified = time(); $DB->update_record('user', $updateuser); if ($verbose) { @@ -324,11 +330,15 @@ class auth_plugin_db extends auth_plugin_base { /// // NOTE: this is very memory intensive // and generally inefficient - $sql = 'SELECT u.id, u.username - FROM {user} u - WHERE u.auth=\'' . $this->authtype . '\' AND u.deleted=\'0\''; + $suspendselect = ""; + if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) { + $suspendselect = "AND u.suspended = 0"; + } + $sql = "SELECT u.id, u.username + FROM {user} u + WHERE u.auth=:authtype AND u.deleted='0' AND mnethostid=:mnethostid $suspendselect"; - $users = $DB->get_records_sql($sql); + $users = $DB->get_records_sql($sql, array('authtype'=>$this->authtype, 'mnethostid'=>$CFG->mnet_localhost_id)); // simplify down to usernames $usernames = array(); @@ -349,6 +359,15 @@ class auth_plugin_db extends auth_plugin_base { $transaction = $DB->start_delegated_transaction(); foreach($add_users as $user) { $username = $user; + if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) { + if ($old_user = $DB->get_record('user', array('username'=>$username, 'deleted'=>0, 'suspended'=>1, 'mnethostid'=>$CFG->mnet_localhost_id, 'auth'=>$this->authtype))) { + $DB->set_field('user', 'suspended', 0, array('id'=>$old_user->id)); + if ($verbose) { + mtrace("\t".get_string('auth_dbreviveduser', 'auth_db', array('name'=>$username, 'id'=>$old_user->id))); + } + continue; + } + } // Do not try to undelete users here, instead select suspending if you ever expect users will reappear.