Fix list permission and user command

This commit is contained in:
Tim 2024-09-24 00:12:58 +02:00
parent bb31bb34b8
commit 1aad1a59e4
4 changed files with 73 additions and 32 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace App\Console;
use App\Service\DatabaseService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand('app:permissions:list')]
class CreatePermissionCommand extends Command
{
public function __construct(
private readonly DatabaseService $db,
)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$dbs = $this->db->listDatabases();
$output->writeln('List of permissions for user:');
foreach ($dbs as $db) {
$output->writeln(sprintf(' -%s', $db));
}
return Command::SUCCESS;
}
}

View File

@ -5,10 +5,13 @@ namespace App\Console;
use App\Service\DatabaseService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand('app:permissions:list')]
#[AsCommand('app:permission:list')]
class ListPermissionsCommand extends Command
{
public function __construct(
@ -18,15 +21,40 @@ class ListPermissionsCommand extends Command
parent::__construct();
}
public function configure(): void
{
$this->addArgument('name', InputArgument::OPTIONAL, 'User name');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$dbs = $this->db->listDatabases();
$style = new SymfonyStyle($input, $output);
$output->writeln('List of permissions for user:');
foreach ($dbs as $db) {
$output->writeln(sprintf(' -%s', $db));
$name = $input->getArgument('name');
$host = $input->getArgument('host');
if (!$name) {
$question = $this->getHelper('question');
$selectQuestion = new ChoiceQuestion('User name: ', array_map(
fn($user) => $user['name'],
$this->db->listUsers())
);
$name = $question->ask($input, $output, $selectQuestion);
}
$permissions = $this->db->listPermissions($name, $host);
$table = $style
->createTable()
->setHeaders(['Permission'])
->setHeaderTitle(sprintf('List of users for %s@%s', $name, $host))
;
foreach ($permissions as $permission) {
$table->addRow([$permission]);
}
$table->render();
return Command::SUCCESS;
}
}

View File

@ -5,13 +5,11 @@ namespace App\Console;
use App\Service\DatabaseService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand('app:permission:list')]
#[AsCommand('app:user:list')]
class ListUsersCommand extends Command
{
public function __construct(
@ -21,37 +19,19 @@ class ListUsersCommand extends Command
parent::__construct();
}
public function configure(): void
{
$this->addArgument('name', InputArgument::OPTIONAL, 'User name');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host', 'localhost');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
$name = $input->getArgument('name');
$host = $input->getArgument('host');
if (!$name) {
$question = $this->getHelper('question');
$selectQuestion = new ChoiceQuestion('User name: ', array_map(
fn($user) => $user['name'],
$this->db->listUsers())
);
$name = $question->ask($input, $output, $selectQuestion);
}
$permissions = $this->db->listPermissions($name, $host);
$users = $this->db->listUsers();
$table = $style
->createTable()
->setHeaders(['Permission'])
->setHeaderTitle(sprintf('List of users for %s@%s', $name, $host))
->setHeaders(['Name', 'Host', 'Plugin'])
->setHeaderTitle('List of users')
;
foreach ($permissions as $permission) {
$table->addRow([$permission]);
foreach ($users as $user) {
$table->addRow([$user['name'], $user['host'], $user['plugin'] ?? '']);
}
$table->render();

View File

@ -111,8 +111,9 @@ class DatabaseService
}
}
public function listPermissions(string $user, ?string $host = 'localhost'): array
public function listPermissions(string $user, ?string $host = null): array
{
$host ??= 'localhost';
$results = $this->conn->query(sprintf("SHOW GRANTS FOR '%s'@'%s'", $user, $host), );
$permissions = [];