mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-05 07:20:58 +08:00
improve user destroy
This commit is contained in:
@@ -17,6 +17,7 @@ use App\Models\UsernameChangeLog;
|
||||
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;
|
||||
@@ -623,13 +624,21 @@ class UserRepository extends BaseRepository
|
||||
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',
|
||||
@@ -641,6 +650,8 @@ class UserRepository extends BaseRepository
|
||||
'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();
|
||||
@@ -655,8 +666,10 @@ class UserRepository extends BaseRepository
|
||||
];
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user