misc_pterodactyl-panel/app/Console/Commands/User/DeleteUserCommand.php

93 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Console\Commands\User;
use Pterodactyl\Models\User;
use Webmozart\Assert\Assert;
use Illuminate\Console\Command;
use Pterodactyl\Services\Users\UserDeletionService;
class DeleteUserCommand extends Command
{
/**
* @var \Pterodactyl\Services\Users\UserDeletionService
*/
protected $deletionService;
/**
* @var string
*/
protected $description = 'Deletes a user from the Panel if no servers are attached to their account.';
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
protected $repository;
/**
* @var string
*/
protected $signature = 'p:user:delete {--user=}';
/**
* DeleteUserCommand constructor.
*/
public function __construct(UserDeletionService $deletionService)
{
parent::__construct();
$this->deletionService = $deletionService;
}
/**
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function handle(): int
{
$search = $this->option('user') ?? $this->ask(trans('command/messages.user.search_users'));
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%")
->get();
if (count($results) < 1) {
$this->error(trans('command/messages.user.no_users_found'));
if ($this->input->isInteractive()) {
return $this->handle();
}
return 1;
}
if ($this->input->isInteractive()) {
$tableValues = [];
foreach ($results as $user) {
$tableValues[] = [$user->id, $user->email, $user->name_first];
}
$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'))) {
return $this->handle();
}
} else {
if (count($results) > 1) {
$this->error(trans('command/messages.user.multiple_found'));
return 1;
}
$deleteUser = $results->first();
}
2021-01-23 20:33:34 +00:00
if ($this->confirm(trans('command/messages.user.confirm_delete')) || !$this->input->isInteractive()) {
$this->deletionService->handle($deleteUser);
$this->info(trans('command/messages.user.deleted'));
}
return 0;
}
}