Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add queue task to purge inactive users #3525

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/includes/updates/212.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return new class() extends UpgradeScript {
public function run(): void
{
$this->runMigrations();

PurgeInactiveUsers::schedule(new Language('core', 'en_UK'));

$this->setVersion('2.2.0');
}
};
9 changes: 9 additions & 0 deletions custom/panel_templates/Default/core/registration.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@
{/foreach}
</select>
</div>
<div class="form-group">
<label for="InputPurgeUsers">{$PURGE_USERS_AFTER}</label>
<span class="badge badge-info">
<i class="fas fa-question-circle" data-container="body" data-toggle="popover"
title="{$INFO}" data-placement="top" data-content="{$PURGE_USERS_AFTER_INFO}"></i>
</span>
<input type="number" min="0" name="purge_users" class="form-control" id="InputPurgeUsers"
value="{$PURGE_USERS_AFTER_VALUE}">
</div>
<div class="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
<input type="submit" class="btn btn-primary" value="{$SUBMIT}">
Expand Down
57 changes: 57 additions & 0 deletions modules/Core/classes/Tasks/PurgeInactiveUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

class PurgeInactiveUsers extends Task {

public function run(): string {
$language = $this->_container->get(Language::class);

$errors = [];

$cutoff = (int) Settings::get('purge_inactive_users_cutoff', '0');
if (!$cutoff) {
$result = ['cutoff' => $cutoff, 'total' => 'Task disabled.'];
} else {
$cutoff_timestamp = strtotime("$cutoff days ago");
$total = DB::getInstance()->query(
'SELECT COUNT(*) c FROM nl2_users WHERE active = 0 AND joined < ?',
[$cutoff_timestamp]
)->first()->c;

if ($total) {
$users = DB::getInstance()->query(
'SELECT id FROM nl2_users WHERE active = 0 AND joined < ?',
[$cutoff_timestamp]
)->results();

foreach ($users as $user) {
EventHandler::executeEvent(new UserDeletedEvent(new User($user->id)));
}
}

$result = [
'cutoff' => $cutoff,
'cutoff_timestamp' => $cutoff_timestamp,
'total' => $total,
'users' => $users ?? [],
];
}

$this->setOutput(['result' => $result, 'errors' => $errors]);
$this->reschedule($language);

return Task::STATUS_COMPLETED;
}

private function reschedule(Language $language) {
Queue::schedule((new PurgeInactiveUsers())->fromNew(
Module::getIdFromName('Core'),
$language->get('admin', 'purge_inactive_users'),
[],
Date::next()->getTimestamp()
));
}

public static function schedule(Language $language) {
(new self())->reschedule($language);
}
}
3 changes: 3 additions & 0 deletions modules/Core/language/en_UK.json
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@
"admin/profile_widgets": "Profile Widgets",
"admin/public": "Public",
"admin/purge_errors": "Purge Errors",
"admin/purge_inactive_users": "Purge Inactive Users",
"admin/purge_inactive_users_after": "Purge inactive users after (days)",
"admin/purge_inactive_users_info": "When set to a value greater than 0, this will automatically purge users that have not validated their accounts after the specified number of days",
"admin/query_error_deleted_successfully": "Query error deleted successfully.",
"admin/query_errors": "Query Errors",
"admin/query_errors_purged_successfully": "Query errors purged successfully.",
Expand Down
8 changes: 8 additions & 0 deletions modules/Core/pages/panel/registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
$validation_action = json_decode(Settings::get('validate_user_action'), true);
$new_value = json_encode(['action' => $validation_action['action'] ?? 'promote', 'group' => $_POST['promote_group']]);
Settings::set('validate_user_action', $new_value);

if (is_numeric(Input::get('purge_users')) && (int) Input::get('purge_users') >= 0) {
// Purge users after x days
Settings::set('purge_inactive_users_cutoff', Input::get('purge_users'));
}
}

if (!count($errors)) {
Expand Down Expand Up @@ -168,6 +173,9 @@
'SUBMIT' => $language->get('general', 'submit'),
'ENABLE_REGISTRATION' => $language->get('admin', 'enable_registration'),
'REGISTRATION_ENABLED' => $registration_enabled,
'PURGE_USERS_AFTER' => $language->get('admin', 'purge_inactive_users_after'),
'PURGE_USERS_AFTER_INFO' => $language->get('admin', 'purge_inactive_users_info'),
'PURGE_USERS_AFTER_VALUE' => Settings::get('purge_inactive_users_cutoff', '0'),
]);

$template->onPageLoad();
Expand Down
Loading