Skip to content

Commit

Permalink
improve user destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Mar 21, 2024
1 parent 5371363 commit 9f211df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
23 changes: 18 additions & 5 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
Expand Down Expand Up @@ -623,13 +624,21 @@ public function confirmUser($id): bool
return true;
}

public function destroy($id, $reasonKey = 'user.destroy_by_admin')
public function destroy(Collection|int $id, $reasonKey = 'user.destroy_by_admin')
{
if (!isRunningInConsole()) {
user_can('user-delete', true);
}
$uidArr = Arr::wrap($id);
$users = User::query()->with('language')->whereIn('id', $uidArr)->get(['id', 'username', 'lang']);
if (is_int($id)) {
$uidArr = Arr::wrap($id);
$users = User::query()->with('language')->whereIn('id', $uidArr)->get(['id', 'username', 'lang']);
} else {
$users = $id;
$uidArr = $users->pluck('id')->toArray();
}
if (empty($uidArr)) {
return;
}
$tables = [
'users' => 'id',
'hit_and_runs' => 'uid',
Expand All @@ -641,6 +650,8 @@ public function destroy($id, $reasonKey = 'user.destroy_by_admin')
'attendance' => 'uid',
'attendance_logs' => 'uid',
'login_logs' => 'uid',
'oauth_access_tokens' => 'user_id',
'oauth_auth_codes' => 'user_id',
];
foreach ($tables as $table => $key) {
\Nexus\Database\NexusDB::table($table)->whereIn($key, $uidArr)->delete();
Expand All @@ -655,8 +666,10 @@ public function destroy($id, $reasonKey = 'user.destroy_by_admin')
];
}
UserBanLog::query()->insert($userBanLogs);
do_action("user_delete", $id);
fire_event("user_destroyed", $id);
if (is_int($id)) {
do_action("user_delete", $id);
fire_event("user_destroyed", $id);
}
return true;
}

Expand Down
13 changes: 7 additions & 6 deletions include/cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,19 @@ function docleanup($forceAll = 0, $printProgress = false) {
}

//destroy disabled accounts
$userRep = new \App\Repositories\UserRepository();
$destroyDisabledDays = get_setting('account.destroy_disabled');
if ($destroyDisabledDays > 0) {
$secs = $destroyDisabledDays*24*60*60;
$dt = date("Y-m-d H:i:s",(TIMENOW - $secs));
$users = \App\Models\User::query()
\App\Models\User::query()
->where('enabled', 'no')
->where("last_access","<", $dt)
->get(['id']);
if ($users->isNotEmpty()) {
$userRep = new \App\Repositories\UserRepository();
$userRep->destroy($users->pluck('id')->toArray(), 'cleanup.destroy_disabled_account');
}
->select(['id', 'username', 'lang'])
->orderBy("id", "asc")
->chunk(2000, function (\Illuminate\Support\Collection $users) use ($userRep) {
$userRep->destroy($users, 'cleanup.destroy_disabled_account');
});
}
$log = "destroy disabled accounts";
do_log($log);
Expand Down
4 changes: 2 additions & 2 deletions include/constants.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.9');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-03-18');
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.10');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-03-21');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
Expand Down

0 comments on commit 9f211df

Please sign in to comment.