Skip to content

Commit

Permalink
add modifiable names for suspended users
Browse files Browse the repository at this point in the history
  • Loading branch information
my-curiosity committed Jun 26, 2024
1 parent 98ee118 commit 3f496d9
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The plugin aims to make users that are suspended **anonymous**. Therefore, the f
(usernames must be unique therefore the id is appended).
- The field `firstname` is set to *Anonym*.
- Consequently, references in e.g. the forum activity merely refer to a user called `Anonym`.
- `username` and `firstname` of suspended users can be customized
- Replaces all other data in the `user` table with the appropriate null value.
- When viewing the page of the user he/she cannot be identified.

Expand Down
6 changes: 3 additions & 3 deletions classes/archiveduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function delete_me() {
}

/**
* Creates an empty user with 'anonym + id' as username and 'Anonym' as Firstname.
* Creates an empty user with ':suspendusername + id' as username and ':suspendfirstname' as Firstname.
*
* @param int $id
* @param int $timestamp
Expand All @@ -217,8 +217,8 @@ private function give_suspended_pseudo_user($id, $timestamp) {
$cloneuser = new \stdClass();
$cloneuser->id = $id;
// Usernames have to be unique therefore the id is used.
$cloneuser->username = 'anonym' . $id;
$cloneuser->firstname = 'Anonym';
$cloneuser->username = get_config('tool_cleanupusers_settings', 'suspendusername') . $id;
$cloneuser->firstname = get_config('tool_cleanupusers_settings', 'suspendfirstname');
$cloneuser->lastname = '';
$cloneuser->suspended = 1;
$cloneuser->email = '';
Expand Down
7 changes: 7 additions & 0 deletions lang/en/tool_cleanupusers.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@
$string['cronjobcomplete'] = 'tool_cleanupusers cron job complete';
$string['cronjobwasrunning'] = 'The tool_cleanupusers cron job was running. No user was suspended or deleted.';
$string['using-plugin'] = 'You are currently using the <b>{$a}</b> Plugin';
$string['suspendusername'] = 'anonym';
$string['suspendfirstname'] = 'Anonym';
$string['sett_title'] = 'Configure suspended users';
$string['sett_suspendusername'] = 'Username for suspended';
$string['sett_suspendfirstname'] = 'Firstname for suspended';
$string['sett_suspendusername_description'] = 'Choose a username for suspended users (must be lowercase)';
$string['sett_suspendfirstname_description'] = 'Choose a firstname for suspended users';
16 changes: 16 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
get_string('pluginsettingstitle', 'tool_cleanupusers'),
"$CFG->wwwroot/$CFG->admin/tool/cleanupusers/index.php"
));
$settings = new admin_settingpage('tool_cleanupusers_settings', get_string('sett_title', 'tool_cleanupusers'));
$settings->add(new admin_setting_configtext(
'tool_cleanupusers_settings/suspendusername',
get_string('sett_suspendusername', 'tool_cleanupusers'),
get_string('sett_suspendusername_description', 'tool_cleanupusers'),
get_string('suspendusername', 'tool_cleanupusers'),
PARAM_TEXT
));
$settings->add(new admin_setting_configtext(
'tool_cleanupusers_settings/suspendfirstname',
get_string('sett_suspendfirstname', 'tool_cleanupusers'),
get_string('sett_suspendfirstname_description', 'tool_cleanupusers'),
get_string('suspendfirstname', 'tool_cleanupusers'),
PARAM_TEXT
));
$ADMIN->add('tool_cleanupusers', $settings);
// Add entry for own settings.
$ADMIN->add('tool_cleanupusers', new admin_externalpage(
'Manage never logged in',
Expand Down
23 changes: 16 additions & 7 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public function test_create_preparation() {
'lastaccess' => $timestamponeyearago, 'suspended' => '0']);
$useroneyearnotloggedin->realusername = $userneverloggedin->username;

$usersuspendedbypluginandmanually = $generator->create_user(['username' => 'anonym-x', 'suspended' => '1']);
$usersuspendedbypluginandmanually = $generator->create_user(['username' => get_config(
'tool_cleanupusers_settings',
'suspendusername'
) . '-x', 'suspended' => '1']);
$usersuspendedbypluginandmanually->realusername = 'somerealusername';
$DB->insert_record_raw('tool_cleanupusers', ['id' => $usersuspendedbypluginandmanually->id, 'archived' => 1,
'timestamp' => $tendaysago], true, false, true);
Expand All @@ -86,8 +89,11 @@ public function test_create_preparation() {
'lastaccess' => $timestamponeyearago]);
$userdeleted->realusername = $userdeleted->username;

$usersuspendedbyplugin = $generator->create_user(['username' => 'anonym-y', 'suspended' => '1',
'firstname' => 'Anonym']);
$usersuspendedbyplugin = $generator->create_user(['username' => get_config(
'tool_cleanupusers_settings',
'suspendusername'
) . '-y', 'suspended' => '1',
'firstname' => get_config('tool_cleanupusers_settings', 'suspendfirstname')]);
$usersuspendedbyplugin->realusername = 'usersuspendedbyplugin';
$DB->insert_record_raw('tool_cleanupusers', ['id' => $usersuspendedbyplugin->id, 'archived' => true,
'timestamp' => $timestamponeyearago], true, false, true);
Expand All @@ -101,7 +107,10 @@ public function test_create_preparation() {
);

$userinconsistentsuspended = $generator->create_user(['username' => 'userinconsistentarchivedbyplugin',
'suspended' => '1', 'firstname' => 'Anonym', 'lastaccess' => $timestamponeyearago]);
'suspended' => '1', 'firstname' => get_config(
'tool_cleanupusers_settings',
'suspendfirstname'
), 'lastaccess' => $timestamponeyearago]);
$userinconsistentsuspended->realusername = $userinconsistentsuspended->username;
$DB->insert_record_raw(
'tool_cleanupusers_archive',
Expand All @@ -113,11 +122,11 @@ public function test_create_preparation() {
);

$userduplicatedname = $generator->create_user(['username' => 'duplicatedname',
'suspended' => '0', 'firstname' => 'Anonym']);
'suspended' => '0', 'firstname' => get_config('tool_cleanupusers_settings', 'suspendfirstname')]);
$userduplicatedname->realusername = $userduplicatedname->username;

$originaluser = $generator->create_user(['username' => 'anonym-z',
'suspended' => '1', 'firstname' => 'Anonym']);
$originaluser = $generator->create_user(['username' => get_config('tool_cleanupusers_settings', 'suspendusername') . '-z',
'suspended' => '1', 'firstname' => get_config('tool_cleanupusers_settings', 'suspendfirstname')]);
$originaluser->realusername = $userduplicatedname->username;
$DB->insert_record_raw(
'tool_cleanupusers_archive',
Expand Down
15 changes: 11 additions & 4 deletions tests/tool_cleanupusers_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public function test_archiveduser_archiveme() {

// Users that are archived will be marked as suspended in the user table and transfer their previous suspended
// status in the tool_cleanupusers table.
// Additionally, they will be anonymized in the user table. Firstname will be 'Anonym', Username will be 'anonym + id'.
// Additionally, they will be anonymized in the user table. Firstname will be :suspendfirstname,
// Username will be ':suspendusername + id'.

$user = new archiveduser(
$data['user']->id,
Expand All @@ -84,8 +85,11 @@ public function test_archiveduser_archiveme() {
$this->assertEquals(1, $recordusertable->suspended);
$this->assertEquals(0, $recordshadowtable->suspended);
$this->assertEquals(1, $recordtooltable->archived);
$this->assertEquals('Anonym', $recordusertable->firstname);
$this->assertEquals('anonym' . $data['user']->id, $recordusertable->username);
$this->assertEquals(get_config('tool_cleanupusers_settings', 'suspendfirstname'), $recordusertable->firstname);
$this->assertEquals(get_config(
'tool_cleanupusers_settings',
'suspendusername'
) . $data['user']->id, $recordusertable->username);

$this->resetAfterTest(true);
}
Expand Down Expand Up @@ -496,7 +500,10 @@ public function test_cronjob() {
$this->assert_user_equals($data['useroneyearnotloggedin'], $recordtooltable2);
$this->assertEquals(1, $recordusertable->suspended);
$this->assertEquals(0, $recordtooltable2->suspended);
$this->assertEquals('anonym' . $data['useroneyearnotloggedin']->id, $recordusertable->username);
$this->assertEquals(get_config(
'tool_cleanupusers_settings',
'suspendusername'
) . $data['useroneyearnotloggedin']->id, $recordusertable->username);
$this->assertEquals(0, $recordusertable->deleted);

// User is deleted.
Expand Down
24 changes: 12 additions & 12 deletions userstatus/timechecker/classes/timechecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ public function get_to_suspend() {
*/
public function get_never_logged_in() {
global $DB;
$select = 'lastaccess=0 AND deleted=0 AND firstname!=\'Anonym\'';
$arrayofuser = $DB->get_records_select('user', $select);
$arrayofuser = $DB->get_records_sql(
"SELECT u.id, u.suspended, u.lastaccess, u.username, u.deleted
FROM {user} u
LEFT JOIN {tool_cleanupusers} tc ON u.id = tc.id
WHERE u.auth = 'shibboleth'
AND u.lastaccess = 0
AND u.deleted = 0
AND tc.id IS NULL"
);
$neverloggedin = [];
foreach ($arrayofuser as $key => $user) {
if (empty($user->lastaccess) && $user->deleted == 0) {
$informationuser = new archiveduser(
$user->id,
$user->suspended,
$user->lastaccess,
$user->username,
$user->deleted
);
$neverloggedin[$key] = $informationuser;
}
$informationuser = new archiveduser($user->id, $user->suspended,
$user->lastaccess, $user->username, $user->deleted);
$neverloggedin[$key] = $informationuser;
}
return $neverloggedin;
}
Expand Down
5 changes: 4 additions & 1 deletion userstatus/timechecker/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public function test_create_preparation() {

// User suspended by the plugin.
$tendaysago = $mytimestamp - 864000;
$reactivate = $generator->create_user(['username' => 'anonym', 'suspended' => 1]);
$reactivate = $generator->create_user(['username' => get_config(
'tool_cleanupusers_settings',
'suspendusername'
), 'suspended' => 1]);
$DB->insert_record_raw('tool_cleanupusers', ['id' => $reactivate->id, 'archived' => true,
'timestamp' => $tendaysago], true, false, true);
$DB->insert_record_raw('tool_cleanupusers_archive', ['id' => $reactivate->id, 'username' => 'reactivate',
Expand Down
2 changes: 1 addition & 1 deletion userstatus/timechecker/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

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

$plugin->version = 2017010500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2024061800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2016052300; // Requires 31 Moodle version.
$plugin->component = 'userstatus_timechecker'; // Full name of the plugin (used for diagnostics).
$plugin->release = 'v1.0-r0';
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 = 2023120800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2024061800; // 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 3f496d9

Please sign in to comment.