Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
remove unnecessary try/catch blocks, fix some typos
Browse files Browse the repository at this point in the history
  • Loading branch information
my-curiosity committed Dec 8, 2023
1 parent a299f6b commit cd58843
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 110 deletions.
193 changes: 90 additions & 103 deletions classes/archiveduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,89 +73,80 @@ public function __construct($id, $suspended, $lastaccess, $username, $deleted) {
* Suspends the user.
* Therefore, makes an entry in tool_cleanupusers and tool_cleanupusers_archive tables.
* Throws an exception when the user is already suspended.
* @throws \Throwable
* @throws cleanupusers_exception
*/
public function archive_me() {
global $DB;
try {
// Get the current user.
$user = \core_user::get_user($this->id);
$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;
// 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.
$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.
if ($DB->record_exists('tool_cleanupusers_archive', ['id' => $shadowuser->id])) {
$DB->delete_records('tool_cleanupusers_archive', ['id' => $shadowuser->id]);
}
$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 = $DB->start_delegated_transaction();
// Get the current user.
$user = \core_user::get_user($this->id);
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;
// 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.
$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.
if ($DB->record_exists('tool_cleanupusers_archive', ['id' => $shadowuser->id])) {
$DB->delete_records('tool_cleanupusers_archive', ['id' => $shadowuser->id]);
}
$transaction->allow_commit();
} catch (\Throwable $e) {
$transaction->rollback($e);
$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.
* @throws \Throwable
* @throws cleanupusers_exception
*/
public function activate_me() {
global $DB;
try {
// Get the current user.
$user = \core_user::get_user($this->id);
$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])) {
// Get the current user.
$user = \core_user::get_user($this->id);
$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 has no entry in archive");
" : user suspended by the plugin already in user table");
} 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]);
}
// 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 {
// User was suspended manually.
throw new cleanupusers_exception("Failed to reactivate " . $user->username .
" : user not suspended by the plugin");
}
$transaction->allow_commit();
} catch (\Throwable $e) {
$transaction->rollback($e);
} else {
// User was suspended manually.
throw new cleanupusers_exception("Failed to reactivate " . $user->username .
" : user not suspended by the plugin");
}
$transaction->allow_commit();
}

/**
Expand All @@ -166,50 +157,46 @@ public function activate_me() {
* (2) Hashes the username with the sha256 function.
* (3) Calls the moodle core delete_user function.
*
* @throws \Throwable
* @throws cleanupusers_exception
*/
public function delete_me() {
global $DB;
try {
// Get the current user.
$user = \core_user::get_user($this->id);
$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 delete " . $user->username .
" : user suspended by the plugin has no entry in archive");
} else {
// Deletes the records in both plugin tables.
$DB->delete_records('tool_cleanupusers', ['id' => $user->id]);
$DB->delete_records('tool_cleanupusers_archive', ['id' => $user->id]);
}
} else {
// User was suspended manually.
$transaction = $DB->start_delegated_transaction();
// Get the current user.
$user = \core_user::get_user($this->id);
// 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 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 suspended by the plugin has no entry in archive");
} else {
// Deletes the records in both plugin tables.
$DB->delete_records('tool_cleanupusers', ['id' => $user->id]);
$DB->delete_records('tool_cleanupusers_archive', ['id' => $user->id]);
}
$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();
} catch (\Throwable $e) {
$transaction->rollback($e);
} else {
// 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();
}

/**
Expand Down
12 changes: 6 additions & 6 deletions classes/task/archive_user_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* A scheduled task for tool_cleanupusers cron.
*
* The Class archive_user_task is supposed to show the admin a page of users which will be archived and expectes a submit or
* The Class archive_user_task is supposed to show the admin a page of users which will be archived and expects a submit or
* cancel reaction.
* @package tool_cleanupusers
* @copyright 2016 N Herrmann
Expand Down Expand Up @@ -52,8 +52,8 @@ public function get_name() {

/**
* Runs the cron job - Calls for the currently activated sub-plugin to return arrays of users.
* Distinguishes between users to reacticate, suspend and delete.
* Subsequently sends an e-mail to the admin containing information about the amount of successfully changed users
* Distinguishes between users to reactivate, suspend and delete.
* Subsequently, sends an e-mail to the admin containing information about the amount of successfully changed users
* and the amount of failures.
* Last but not least triggers an event with the same information.
*
Expand Down Expand Up @@ -148,9 +148,9 @@ private function change_user_deprovisionstatus($userarray, $intention) {
// Array of users who could not be changed.
$failures = [];

// Alternatively one could have wrote different function for each intention.
// However this would have produced duplicated code.
// Therefore checking the intention parameter repeatedly was preferred.
// Alternatively one could have written different function for each intention.
// However, this would have produced duplicated code.
// Therefore, checking the intention parameter repeatedly was preferred.
foreach ($userarray as $key => $user) {
if ($user->deleted == 0 && !is_siteadmin($user)) {
$changinguser = new archiveduser(
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

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

$plugin->version = 2023120500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023120800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2022041900; // Requires Moodle version 4.0 or higher.
$plugin->component = 'tool_cleanupusers'; // Full name of the plugin (used for diagnostics).
$plugin->release = 'v1.0-r2';
Expand Down

0 comments on commit cd58843

Please sign in to comment.