-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteUser.php
More file actions
65 lines (47 loc) · 1.76 KB
/
DeleteUser.php
File metadata and controls
65 lines (47 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Backstage\Laravel\Users\Console\Commands;
use Backstage\Laravel\Users\Eloquent\Models\User;
use Illuminate\Console\Command;
use function Laravel\Prompts\error;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\warning;
class DeleteUser extends Command
{
protected $signature = 'users:delete {--force-delete}';
protected $description = 'List all users in the system.';
public function handle()
{
if ($this->option('force-delete') && posix_geteuid() !== 0) {
error('This command must be run as root. Try: sudo php artisan '.str($this->signature)->replace(['{', '}'], '')->toString());
return Command::FAILURE;
}
$userCollection = User::all();
if ($userCollection->isEmpty()) {
warning('No users found. Please create a user first.');
return Command::FAILURE;
}
$users = multiselect(
label: 'Select the user(s) to delete',
options: $userCollection->pluck('name', 'id')->map(fn ($name, $id) => $name.' (ID: '.$id.')')->toArray(),
required: true,
);
if (empty($users)) {
error('No users selected.');
return Command::FAILURE;
}
$users = User::whereIn('id', $users)->get();
if ($users->isEmpty()) {
error('No users found.');
return Command::FAILURE;
}
$this->info('Deleting users...');
foreach ($users as $user) {
if ($this->option('force-delete')) {
$user->forceDelete();
} else {
$user->delete();
}
$this->info($this->option('force-delete') ? 'Force deleted' : 'Deleted'.' user: '.$user->name);
}
}
}