Skip to content

Commit

Permalink
changes from cleanupusers_tu
Browse files Browse the repository at this point in the history
  • Loading branch information
my-curiosity committed Jan 2, 2024
1 parent 2fdfd8d commit e30ef8a
Show file tree
Hide file tree
Showing 39 changed files with 840 additions and 593 deletions.
Empty file modified README.md
100755 → 100644
Empty file.
190 changes: 88 additions & 102 deletions classes/archiveduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot.'/lib/moodlelib.php');
require_once($CFG->dirroot . '/user/lib.php');
require_once($CFG->dirroot . '/lib/moodlelib.php');

use \core\session\manager;
use core\session\manager;
/**
* The class collects the necessary information to suspend, delete and activate users.
*
Expand All @@ -38,7 +38,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class archiveduser {

/** @var int The id of the user */
public $id;

Expand Down Expand Up @@ -72,149 +71,136 @@ public function __construct($id, $suspended, $lastaccess, $username, $deleted) {

/**
* Suspends the user.
*
* Therefore makes an entry in the tool_cleanupusers table. Throws an error when the user that should be
* suspended is already suspended or is the sideadmin.
*
* Therefore, makes an entry in tool_cleanupusers and tool_cleanupusers_archive tables.
* Throws an exception when the user is already suspended.
* @throws cleanupusers_exception
*/
public function archive_me() {
global $DB;
$transaction = $DB->start_delegated_transaction();
// Get the current user.
$user = \core_user::get_user($this->id);

// Only apply to users who are not yet suspended, not admins, and to users with correct name.
if ($user->suspended == 0 && !is_siteadmin($user) && $user->username ==
\core_user::clean_field($user->username, 'username')) {
$transaction = $DB->start_delegated_transaction();
if ($user->suspended == 1) {
throw new cleanupusers_exception("Failed to suspend " . $user->username .
" : user is already suspended");
} else if (!($user->username == \core_user::clean_field($user->username, 'username'))) {
throw new cleanupusers_exception("Failed to suspend " . $user->username .
" : username is not cleaned");
} else {
// We are already getting the shadowuser here to keep the original suspended status.
$shadowuser = clone $user;
// In case the user was not suspended previously he/she might be logged in we kill his/her session.
if ($user->suspended = 0) {
$user->suspended = 1;
manager::kill_user_sessions($user->id);
user_update_user($user, false);
}

$timestamp = time();
$tooluser = $DB->get_record('tool_cleanupusers', array('id' => $user->id));

// The user might be logged in, so we must kill his/her session.
$user->suspended = 1;
manager::kill_user_sessions($user->id);
user_update_user($user, false);
// Document time of editing user in Database.
// In case there is no entry in the tool table make a new one.
if (empty($tooluser)) {
$DB->insert_record_raw('tool_cleanupusers', array('id' => $user->id, 'archived' => 1,
'timestamp' => $timestamp), true, false, true);
$timestamp = time();
if (!$DB->record_exists('tool_cleanupusers', ['id' => $user->id])) {
$DB->insert_record_raw('tool_cleanupusers', ['id' => $user->id, 'archived' => 1,
'timestamp' => $timestamp], true, false, true);
}

// Insert copy of user in second DB and replace user in main table when entry was successful.
$DB->delete_records('tool_cleanupusers_archive', array('id' => $shadowuser->id));

$success = $DB->insert_record_raw('tool_cleanupusers_archive', $shadowuser, true, false, true);

if ($success == true) {
// Replaces the current user with a pseudo_user that has no reference.
$cloneuser = $this->give_suspended_pseudo_user($shadowuser->id, $timestamp);
$cloneuser->suspended = 1;
user_update_user($cloneuser, false);
if ($DB->record_exists('tool_cleanupusers_archive', ['id' => $shadowuser->id])) {
$DB->delete_records('tool_cleanupusers_archive', ['id' => $shadowuser->id]);
}
$transaction->allow_commit();
// No error here since user was maybe manually suspended in user table.

} else {
throw new cleanupusers_exception(get_string('errormessagenotsuspend', 'tool_cleanupusers'));
$DB->insert_record_raw('tool_cleanupusers_archive', $shadowuser, true, false, true);
// Replaces the current user with a pseudo_user that has no reference.
$cloneuser = $this->give_suspended_pseudo_user($shadowuser->id, $timestamp);
user_update_user($cloneuser, false);
}
$transaction->allow_commit();
}

/**
* Reactivates the user.
* Therefore deletes the entry in the tool_cleanupusers table and throws an exception when no entry is available.
* In case a user with the same name exist this user is deleted and the plugin user is restored.
* Therefore, deletes the entry in the tool_cleanupusers table and throws an exception when no entry is available.
* @throws cleanupusers_exception
*/
public function activate_me() {
global $DB;
$transaction = $DB->start_delegated_transaction();
// Get the current user.
$user = \core_user::get_user($this->id);

// Deletes record of plugin table tool_cleanupusers.
if (!$DB->record_exists('tool_cleanupusers', array('id' => $user->id))) {
throw new cleanupusers_exception(get_string('errormessagenotactive', 'tool_cleanupusers'));
} else if (!$DB->record_exists('tool_cleanupusers_archive', array('id' => $user->id))) {
throw new cleanupusers_exception(get_string('errormessagenotactive', 'tool_cleanupusers'));
} else if ($DB->record_exists('user', array('username' => $this->username))) {
throw new cleanupusers_exception(get_string('errormessagenotactive', 'tool_cleanupusers'));
$transaction = $DB->start_delegated_transaction();
// User was suspended by the plugin.
if ($DB->record_exists('tool_cleanupusers', ['id' => $user->id])) {
if (!$DB->record_exists('tool_cleanupusers_archive', ['id' => $user->id])) {
throw new cleanupusers_exception("Failed to reactivate " . $user->username .
" : user suspended by the plugin has no entry in archive");
} else {
$shadowuser = $DB->get_record('tool_cleanupusers_archive', ['id' => $user->id]);
if ($DB->record_exists('user', ['username' => $shadowuser->username])) {
throw new cleanupusers_exception("Failed to reactivate " . $user->username .
" : user suspended by the plugin already in user table");
} else {
// Both records exist, so we have a user which can be reactivated.
// If the user is in table replace data.
user_update_user($shadowuser, false);
// Delete records from tool_cleanupusers and tool_cleanupusers_archive tables.
$DB->delete_records('tool_cleanupusers', ['id' => $user->id]);
$DB->delete_records('tool_cleanupusers_archive', ['id' => $user->id]);
}
}
} else {
// Both record exist so we have a user which can be reactivated.
$DB->delete_records('tool_cleanupusers', array('id' => $user->id));
// If the user is in table replace data.
$shadowuser = $DB->get_record('tool_cleanupusers_archive', array('id' => $user->id));

$DB->update_record('user', $shadowuser);
// Delete records from tool_cleanupusers_archive table.
$DB->delete_records('tool_cleanupusers_archive', array('id' => $user->id));
// User was suspended manually.
throw new cleanupusers_exception("Failed to reactivate " . $user->username .
" : user not suspended by the plugin");
}
// Gets the new user for additional checks.
$transaction->allow_commit();
}

/**
* Deletes the user.
*
* Therefore
* (1) Deletes the entry in the tool_cleanupusers and the tool_cleanupusers_archive table.
* Therefore,
* (1) Deletes the entry in the tool_cleanupusers and the tool_cleanupusers_archive tables.
* (2) Hashes the username with the sha256 function.
* (3) Calls the moodle core delete_user function..
*
* Throws an error when the side admin should be deleted or user is already flagged as deleted.
* (3) Calls the moodle core delete_user function.
*
* @throws cleanupusers_exception
*/
public function delete_me() {
global $DB;

$transaction = $DB->start_delegated_transaction();
// Get the current user.
$user = \core_user::get_user($this->id);

if ($user && $user->deleted == 0 && !is_siteadmin($user)) {

$transaction = $DB->start_delegated_transaction();

// Deletes the records in both plugin tables.
$DB->delete_records('tool_cleanupusers', array('id' => $user->id));

$DB->delete_records('tool_cleanupusers_archive', array('id' => $user->id));

// To secure that plugins that reference the user table do not fail create empty user with a hash as username.
$newusername = hash('md5', $user->username);

// Checks whether the username already exist (possible but unlikely).
if (!$DB->record_exists('user', array("username" => $newusername))) {
$user->username = $newusername;
user_update_user($user, false);
// User was suspended by the plugin.
if ($DB->record_exists('tool_cleanupusers', ['id' => $user->id])) {
if (!$DB->record_exists('tool_cleanupusers_archive', ['id' => $user->id])) {
throw new cleanupusers_exception("Failed to delete " . $user->username .
" : user suspended by the plugin has no entry in archive");
} else {
// In the unlikely case that hash(username) exist in the table, while loop generates new username.
while ($DB->record_exists('user', array("username" => $newusername))) {
$tempname = $newusername;
$newusername = hash('md5', $user->username . $tempname);
}
$user->username = $newusername;
user_update_user($user, false);
// Deletes the records in both plugin tables.
$DB->delete_records('tool_cleanupusers', ['id' => $user->id]);
$DB->delete_records('tool_cleanupusers_archive', ['id' => $user->id]);
}

manager::kill_user_sessions($user->id);
// Core Function has to be executed finally.
// It can not be executed earlier since moodle then prevents further operations on the user.
// The Function adds @unknownemail.invalid. and a timestamp to the username.
// It is secured, that the username is below 100 characters since sha256 produces 64 characters and the...
// additional string has only 32 characters.
delete_user($user);
$transaction->allow_commit();
} else {
throw new cleanupusers_exception(get_string('errormessagenotdelete', 'tool_cleanupusers'));
// User was suspended manually.
throw new cleanupusers_exception("Failed to delete " . $user->username .
" : user not suspended by the plugin");
}
// To secure that plugins that reference the user table do not fail create empty user with a hash as username.
$newusername = hash('md5', $user->username);
// Checks whether the username already exist (possible but unlikely).
// In the unlikely case that hash(username) exist in the table, while loop generates new username.
while ($DB->record_exists('user', ["username" => $newusername])) {
$tempname = $newusername;
$newusername = hash('md5', $user->username . $tempname);
}
$user->username = $newusername;
user_update_user($user, false);
manager::kill_user_sessions($user->id);
// Core Function has to be executed finally.
// It can not be executed earlier since moodle then prevents further operations on the user.
// The Function adds @unknownemail.invalid. and a timestamp to the username.
// It is secured, that the username is below 100 characters since sha256 produces 64 characters and the...
// additional string has only 32 characters.
user_delete_user($user);
$transaction->allow_commit();
}

/**
* Creates a empty user with 'anonym + id' as username and 'Anonym' as Firstname.
* Creates an empty user with 'anonym + id' as username and 'Anonym' as Firstname.
*
* @param int $id
* @param int $timestamp
Expand Down
1 change: 0 additions & 1 deletion classes/cleanupusers_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanupusers_exception extends \moodle_exception {

/**
* Constructor
* @param string $errorcode The name of the string from webservice.php to print
Expand Down
1 change: 0 additions & 1 deletion classes/cleanupusers_reactivate_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanupusers_reactivate_exception extends \moodle_exception {

/**
* Constructor
* @param string $errorcode The name of the string from webservice.php to print
Expand Down
7 changes: 3 additions & 4 deletions classes/event/deprovisionusercronjob_completed.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace tool_cleanupusers\event;

use \core\event\base;
use core\event\base;
/**
* The tool_cleanupusers event informs admin about outcome of cron-job.
*
Expand All @@ -33,7 +33,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class deprovisionusercronjob_completed extends base {

/**
* Creates a simple event with the number of users archives and deleted as additional information.
*
Expand All @@ -43,8 +42,8 @@ class deprovisionusercronjob_completed extends base {
* @return \core\event\base
*/
public static function create_simple($context, $numbersuspended, $numberdeleted) {
return self::create(array('context' => $context, 'other' => array('numbersuspended' => $numbersuspended,
'numberdeleted' => $numberdeleted)));
return self::create(['context' => $context, 'other' => ['numbersuspended' => $numbersuspended,
'numberdeleted' => $numberdeleted]]);
}

/**
Expand Down
1 change: 0 additions & 1 deletion classes/plugininfo/userstatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class userstatus extends base {

/**
* Returns true when sub-plugin can be deleted false when not.
* Returns false for the userstatuswwu sub-plugin and for any plugin currently in usage, otherwise true.
Expand Down
2 changes: 1 addition & 1 deletion classes/subplugin_select_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function definition() {
$mform = $this->_form;
// Gets all available plugins of type userstatus.
$plugins = core_plugin_manager::instance()->get_plugins_of_type('userstatus');
$types = array();
$types = [];

foreach ($plugins as $value) {
$types[$value->name] = $value->name;
Expand Down
39 changes: 25 additions & 14 deletions classes/table/never_logged_in_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

use core_user\fields;

defined('MOODLE_INTERNAL') || die();

/**
* Create a class for a custom sql_table for the tool_cleanupusers
*
Expand All @@ -35,8 +33,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class never_logged_in_table extends \table_sql {


/**
* Constructor.
* @param array $users
Expand All @@ -47,12 +43,12 @@ class never_logged_in_table extends \table_sql {
public function __construct($users, $sqlwhere, $param) {
parent::__construct('tool_cleanupusers_never_logged_in_table');
// Define the list of columns to show.
$columns = array('id', 'username', 'fullname', 'suspended', 'deleted');
$columns = ['id', 'username', 'fullname', 'suspended', 'deleted'];
$this->define_columns($columns);

// Define the titles of columns to show in header.
$headers = array(get_string('id', 'tool_cleanupusers'), get_string('Neverloggedin', 'tool_cleanupusers'),
get_string('fullname'), get_string('Archived', 'tool_cleanupusers'), 'Archive');
$headers = [get_string('id', 'tool_cleanupusers'), get_string('Neverloggedin', 'tool_cleanupusers'),
get_string('fullname'), get_string('Archived', 'tool_cleanupusers'), 'Archive'];
$this->define_headers($headers);

$idsasstring = '';
Expand All @@ -66,7 +62,10 @@ public function __construct($users, $sqlwhere, $param) {
$where .= ' AND ' . $sqlwhere;
}

$this->set_sql('id, username, lastaccess, suspended, ' .implode(', ', fields::get_name_fields()), '{user}', $where, $param);
$this->set_sql('id, username, lastaccess, suspended, ' . implode(
', ',
fields::get_name_fields()
), '{user}', $where, $param);
}

/**
Expand All @@ -93,15 +92,27 @@ public function col_deleted($values) {
if ($values->suspended == 0) {
$url = new \moodle_url('/admin/tool/cleanupusers/handleuser.php', ['userid' => $values->id, 'action' => 'suspend']);

return \html_writer::link($url,
$OUTPUT->pix_icon('t/removecontact', get_string('hideuser', 'tool_cleanupusers'), 'moodle',
['class' => "imggroup-" . $values->id]));
return \html_writer::link(
$url,
$OUTPUT->pix_icon(
't/removecontact',
get_string('hideuser', 'tool_cleanupusers'),
'moodle',
['class' => "imggroup-" . $values->id]
)
);
} else {
$url = new \moodle_url('/admin/tool/cleanupusers/handleuser.php', ['userid' => $values->id, 'action' => 'reactivate']);

return \html_writer::link($url,
$OUTPUT->pix_icon('t/reload', get_string('hideuser', 'tool_cleanupusers'), 'moodle',
['class' => "imggroup-" . $values->id]));
return \html_writer::link(
$url,
$OUTPUT->pix_icon(
't/reload',
get_string('hideuser', 'tool_cleanupusers'),
'moodle',
['class' => "imggroup-" . $values->id]
)
);
}
}
}
Loading

0 comments on commit e30ef8a

Please sign in to comment.