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

Commit

Permalink
fix open db transaction on exceptions + other small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
my-curiosity committed Oct 31, 2023
1 parent 5e77737 commit 5e9b626
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions classes/archiveduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,30 @@ public function archive_me() {
*/
public function activate_me() {
global $DB;
$transaction = $DB->start_delegated_transaction();
$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'));
} 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));
try {
$transaction = $DB->start_delegated_transaction();
// Deletes record of plugin table tool_cleanupusers.
if (!$DB->record_exists('tool_cleanupusers', array('id' => $this->id))) {
throw new cleanupusers_exception("Failed to reactivate " . $this->username . " : user not found in tool_cleanupusers");
} else if (!$DB->record_exists('tool_cleanupusers_archive', array('id' => $this->id))) {
throw new cleanupusers_exception("Failed to reactivate " . $this->username . " : user not found in tool_cleanupusers_archive");
} else if ($DB->record_exists('user', array('username' => $this->username))) {
throw new cleanupusers_exception("Failed to reactivate " . $this->username . " : user already in user table");
} else {
// Both record exist, so we have a user which can be reactivated.
// If the user is in table replace data.
$shadowuser = $DB->get_record('tool_cleanupusers_archive', array('id' => $this->id));

$DB->update_record('user', $shadowuser);
// Delete records from tool_cleanupusers_archive table.
$DB->delete_records('tool_cleanupusers_archive', array('id' => $user->id));
user_update_user($shadowuser, false);
// Delete records from tool_cleanupusers and tool_cleanupusers_archive tables.
$DB->delete_records('tool_cleanupusers', array('id' => $this->id));
$DB->delete_records('tool_cleanupusers_archive', array('id' => $this->id));
}
$transaction->allow_commit();
}
catch (\Throwable $e) {
$transaction->rollback($e);
}
// Gets the new user for additional checks.
$transaction->allow_commit();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/task/archive_user_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private function change_user_deprovisionstatus($userarray, $intention) {
// No default since if-clause checks the intention parameter.
}
$countersuccess++;
} catch (cleanupusers_exception $e) {
} catch (\Throwable $e) {
$failures[$key] = $user->id;
}
}
Expand Down

0 comments on commit 5e9b626

Please sign in to comment.