Do not remove users with API tokens (#12162)

* Do not remove users with API tokens

* Use eloquent

* Fix missing namespace and convert fully to Eloquent

* fix bug in purge

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
gerhardqux 2020-10-01 01:16:28 +02:00 committed by GitHub
parent a08b092e4b
commit 09ae9e49bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -310,12 +310,13 @@ if ($options['f'] === 'purgeusers') {
$purge = \LibreNMS\Config::get('active_directory.users_purge');
}
if ($purge > 0) {
foreach (dbFetchRows('SELECT DISTINCT(`user`) FROM `authlog` WHERE `datetime` >= DATE_SUB(NOW(), INTERVAL ? DAY)', [$purge]) as $user) {
$users[] = $user['user'];
}
$users = \App\Models\AuthLog::where('datetime', '>=', \Carbon\Carbon::now()->subDays($purge))
->distinct()->pluck('user')
->merge(\App\Models\User::has('apiToken')->pluck('username')) // don't purge users with api tokens
->unique();
if (dbDelete('users', 'username NOT IN ' . dbGenPlaceholders(count($users)), $users)) {
echo "Removed users that haven't logged in for $purge days";
if (\App\Models\User::thisAuth()->whereNotIn('username', $users)->delete()) {
echo "Removed users that haven't logged in for $purge days\n";
}
}
} catch (LockException $e) {