Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Console/Commands/User/DeleteUserCommand.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Console\Commands\User;
4
5
use Pterodactyl\Models\User;
6
use Webmozart\Assert\Assert;
7
use Illuminate\Console\Command;
8
use Pterodactyl\Services\Users\UserDeletionService;
9
10
class DeleteUserCommand extends Command
11
{
12
protected $description = 'Deletes a user from the Panel if no servers are attached to their account.';
13
14
protected $signature = 'p:user:delete {--user=}';
15
16
/**
17
* DeleteUserCommand constructor.
18
*/
19
public function __construct(private UserDeletionService $deletionService)
20
{
21
parent::__construct();
22
}
23
24
public function handle(): int
25
{
26
$search = $this->option('user') ?? $this->ask(trans('command/messages.user.search_users'));
27
Assert::notEmpty($search, 'Search term should be an email address, got: %s.');
28
29
$results = User::query()
30
->where('id', 'LIKE', "$search%")
31
->orWhere('username', 'LIKE', "$search%")
32
->orWhere('email', 'LIKE', "$search%")
33
->get();
34
35
if (count($results) < 1) {
36
$this->error(trans('command/messages.user.no_users_found'));
37
if ($this->input->isInteractive()) {
38
return $this->handle();
39
}
40
41
return 1;
42
}
43
44
if ($this->input->isInteractive()) {
45
$tableValues = [];
46
foreach ($results as $user) {
47
$tableValues[] = [$user->id, $user->email, $user->name];
48
}
49
50
$this->table(['User ID', 'Email', 'Name'], $tableValues);
51
if (!$deleteUser = $this->ask(trans('command/messages.user.select_search_user'))) {
52
return $this->handle();
53
}
54
} else {
55
if (count($results) > 1) {
56
$this->error(trans('command/messages.user.multiple_found'));
57
58
return 1;
59
}
60
61
$deleteUser = $results->first();
62
}
63
64
if ($this->confirm(trans('command/messages.user.confirm_delete')) || !$this->input->isInteractive()) {
65
$this->deletionService->handle($deleteUser);
66
$this->info(trans('command/messages.user.deleted'));
67
}
68
69
return 0;
70
}
71
}
72
73