2017-09-15 22:13:33 -05:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-09-15 22:13:33 -05:00
|
|
|
namespace Pterodactyl\Console\Commands\User;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Console\Attributes\Description;
|
|
|
|
|
use Illuminate\Console\Attributes\Signature;
|
2017-09-15 22:13:33 -05:00
|
|
|
use Illuminate\Console\Command;
|
2026-10-05 18:10:18 -04:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use Pterodactyl\Contracts\Users\DeletesUsers;
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
|
use Pterodactyl\Support\JsonValueGuard;
|
2017-09-15 22:13:33 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Description('Deletes a user from the Panel if no servers are attached to their account.')]
|
|
|
|
|
#[Signature('p:user:delete {--user=}')]
|
2017-09-15 22:13:33 -05:00
|
|
|
class DeleteUserCommand extends Command
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
public function handle(DeletesUsers $users): int
|
2017-09-15 22:13:33 -05:00
|
|
|
{
|
|
|
|
|
$search = $this->option('user') ?? $this->ask(trans('command/messages.user.search_users'));
|
2026-10-05 18:10:18 -04:00
|
|
|
if (empty($search)) {
|
|
|
|
|
throw new InvalidArgumentException(sprintf('Search term should be an email address, got: %s.', get_debug_type($search)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$search = JsonValueGuard::string($search);
|
2020-09-13 12:21:44 -07:00
|
|
|
|
|
|
|
|
$results = User::query()
|
2020-10-17 18:26:34 +01:00
|
|
|
->where('id', 'LIKE', "$search%")
|
|
|
|
|
->orWhere('username', 'LIKE', "$search%")
|
|
|
|
|
->orWhere('email', 'LIKE', "$search%")
|
2020-09-13 12:21:44 -07:00
|
|
|
->get();
|
2017-09-15 22:13:33 -05:00
|
|
|
|
|
|
|
|
if (count($results) < 1) {
|
|
|
|
|
$this->error(trans('command/messages.user.no_users_found'));
|
2017-09-16 01:45:56 -05:00
|
|
|
if ($this->input->isInteractive()) {
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->handle($users);
|
2017-09-15 22:13:33 -05:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
return 1;
|
2017-09-15 22:13:33 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-16 01:45:56 -05:00
|
|
|
if ($this->input->isInteractive()) {
|
2017-09-15 22:13:33 -05:00
|
|
|
$tableValues = [];
|
|
|
|
|
foreach ($results as $user) {
|
|
|
|
|
$tableValues[] = [$user->id, $user->email, $user->name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->table(['User ID', 'Email', 'Name'], $tableValues);
|
2026-10-05 18:10:18 -04:00
|
|
|
if (! $deleteUser = $this->ask(trans('command/messages.user.select_search_user'))) {
|
|
|
|
|
return $this->handle($users);
|
2017-09-15 22:13:33 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (count($results) > 1) {
|
|
|
|
|
$this->error(trans('command/messages.user.multiple_found'));
|
|
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
return 1;
|
2017-09-15 22:13:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$deleteUser = $results->first();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if ($this->confirm(trans('command/messages.user.confirm_delete')) || ! $this->input->isInteractive()) {
|
|
|
|
|
$user = $deleteUser instanceof User ? $deleteUser : User::query()->findOrFail(JsonValueGuard::integer($deleteUser));
|
|
|
|
|
$users->delete($user);
|
2017-09-15 22:13:33 -05:00
|
|
|
$this->info(trans('command/messages.user.deleted'));
|
|
|
|
|
}
|
2022-10-14 10:59:20 -06:00
|
|
|
|
|
|
|
|
return 0;
|
2017-09-15 22:13:33 -05:00
|
|
|
}
|
|
|
|
|
}
|