2017-09-16 03:13:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Console\Commands\User;
|
|
|
|
|
2020-09-13 19:21:44 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2017-09-16 03:13:33 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Pterodactyl\Services\Users\UserDeletionService;
|
|
|
|
|
|
|
|
class DeleteUserCommand extends Command
|
|
|
|
{
|
|
|
|
protected $description = 'Deletes a user from the Panel if no servers are attached to their account.';
|
|
|
|
|
|
|
|
protected $signature = 'p:user:delete {--user=}';
|
|
|
|
|
2017-09-16 04:46:22 +00:00
|
|
|
/**
|
|
|
|
* DeleteUserCommand constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private UserDeletionService $deletionService)
|
2021-01-23 20:09:16 +00:00
|
|
|
{
|
2017-09-16 03:13:33 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(): int
|
2017-09-16 03:13:33 +00:00
|
|
|
{
|
|
|
|
$search = $this->option('user') ?? $this->ask(trans('command/messages.user.search_users'));
|
2020-09-13 19:21:44 +00:00
|
|
|
Assert::notEmpty($search, 'Search term should be an email address, got: %s.');
|
|
|
|
|
|
|
|
$results = User::query()
|
2020-10-17 17:26:34 +00:00
|
|
|
->where('id', 'LIKE', "$search%")
|
|
|
|
->orWhere('username', 'LIKE', "$search%")
|
|
|
|
->orWhere('email', 'LIKE', "$search%")
|
2020-09-13 19:21:44 +00:00
|
|
|
->get();
|
2017-09-16 03:13:33 +00:00
|
|
|
|
|
|
|
if (count($results) < 1) {
|
|
|
|
$this->error(trans('command/messages.user.no_users_found'));
|
2017-09-16 06:45:56 +00:00
|
|
|
if ($this->input->isInteractive()) {
|
2017-09-16 03:13:33 +00:00
|
|
|
return $this->handle();
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
return 1;
|
2017-09-16 03:13:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-16 06:45:56 +00:00
|
|
|
if ($this->input->isInteractive()) {
|
2017-09-16 03:13:33 +00:00
|
|
|
$tableValues = [];
|
|
|
|
foreach ($results as $user) {
|
|
|
|
$tableValues[] = [$user->id, $user->email, $user->name];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->table(['User ID', 'Email', 'Name'], $tableValues);
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$deleteUser = $this->ask(trans('command/messages.user.select_search_user'))) {
|
2017-09-16 03:13:33 +00:00
|
|
|
return $this->handle();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (count($results) > 1) {
|
|
|
|
$this->error(trans('command/messages.user.multiple_found'));
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
return 1;
|
2017-09-16 03:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$deleteUser = $results->first();
|
|
|
|
}
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if ($this->confirm(trans('command/messages.user.confirm_delete')) || !$this->input->isInteractive()) {
|
2017-09-16 03:13:33 +00:00
|
|
|
$this->deletionService->handle($deleteUser);
|
|
|
|
$this->info(trans('command/messages.user.deleted'));
|
|
|
|
}
|
2022-10-14 16:59:20 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-09-16 03:13:33 +00:00
|
|
|
}
|
|
|
|
}
|