From 1aad1a59e4a683583c5a3ab5742b0ad57780868a Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 24 Sep 2024 00:12:58 +0200 Subject: [PATCH] Fix list permission and user command --- src/Console/CreatePermissionCommand.php | 32 +++++++++++++++++++++ src/Console/ListPermissionsCommand.php | 38 +++++++++++++++++++++---- src/Console/ListUsersCommand.php | 32 ++++----------------- src/Service/DatabaseService.php | 3 +- 4 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 src/Console/CreatePermissionCommand.php diff --git a/src/Console/CreatePermissionCommand.php b/src/Console/CreatePermissionCommand.php new file mode 100644 index 0000000..6998f08 --- /dev/null +++ b/src/Console/CreatePermissionCommand.php @@ -0,0 +1,32 @@ +db->listDatabases(); + + $output->writeln('List of permissions for user:'); + foreach ($dbs as $db) { + $output->writeln(sprintf(' -%s', $db)); + } + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Console/ListPermissionsCommand.php b/src/Console/ListPermissionsCommand.php index ab38bb9..9b5f706 100644 --- a/src/Console/ListPermissionsCommand.php +++ b/src/Console/ListPermissionsCommand.php @@ -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; } } \ No newline at end of file diff --git a/src/Console/ListUsersCommand.php b/src/Console/ListUsersCommand.php index 06070f5..9a975c5 100644 --- a/src/Console/ListUsersCommand.php +++ b/src/Console/ListUsersCommand.php @@ -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(); diff --git a/src/Service/DatabaseService.php b/src/Service/DatabaseService.php index d63b732..5da4a2a 100644 --- a/src/Service/DatabaseService.php +++ b/src/Service/DatabaseService.php @@ -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 = [];