diff --git a/src/Console/DeleteUserCommand.php b/src/Console/DeleteUserCommand.php index 1b852ca..8f2bc33 100644 --- a/src/Console/DeleteUserCommand.php +++ b/src/Console/DeleteUserCommand.php @@ -29,10 +29,11 @@ class DeleteUserCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { - $question = $this->getHelper('question'); $host = $input->getArgument('host'); + $name = $input->getArgument('name'); - if (!$name = $input->getArgument('name')) { + if (!$name) { + $question = $this->getHelper('question'); $selectQuestion = new ChoiceQuestion('User name: ', array_map( fn($user) => $user['name'], $this->db->listUsers()) diff --git a/src/Console/ListPermissionsCommand.php b/src/Console/ListPermissionsCommand.php new file mode 100644 index 0000000..ab38bb9 --- /dev/null +++ b/src/Console/ListPermissionsCommand.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/ListUsersCommand.php b/src/Console/ListUsersCommand.php index 9a975c5..06070f5 100644 --- a/src/Console/ListUsersCommand.php +++ b/src/Console/ListUsersCommand.php @@ -5,11 +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:user:list')] +#[AsCommand('app:permission:list')] class ListUsersCommand extends Command { public function __construct( @@ -19,19 +21,37 @@ 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); - $users = $this->db->listUsers(); + $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(['Name', 'Host', 'Plugin']) - ->setHeaderTitle('List of users') + ->setHeaders(['Permission']) + ->setHeaderTitle(sprintf('List of users for %s@%s', $name, $host)) ; - foreach ($users as $user) { - $table->addRow([$user['name'], $user['host'], $user['plugin'] ?? '']); + foreach ($permissions as $permission) { + $table->addRow([$permission]); } $table->render(); diff --git a/src/Service/BackupService.php b/src/Service/BackupService.php index 619f14e..f6667bc 100644 --- a/src/Service/BackupService.php +++ b/src/Service/BackupService.php @@ -2,8 +2,6 @@ namespace App\Service; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Process\InputStream; use Symfony\Component\Process\Process; class BackupService diff --git a/src/Service/DatabaseCredentials.php b/src/Service/DatabaseCredentials.php index 5db8834..cf069d9 100644 --- a/src/Service/DatabaseCredentials.php +++ b/src/Service/DatabaseCredentials.php @@ -5,12 +5,10 @@ namespace App\Service; class DatabaseCredentials { public function __construct( - private string $hostname, - private string $user, - private string $password, - ) - { - } + private readonly string $hostname, + private readonly string $user, + private readonly string $password, + ) {} public function getHostname(): string { diff --git a/src/Service/DatabaseService.php b/src/Service/DatabaseService.php index 5345a68..d63b732 100644 --- a/src/Service/DatabaseService.php +++ b/src/Service/DatabaseService.php @@ -110,4 +110,16 @@ class DatabaseService throw new Exception(sprintf('User delete error: %s', $this->conn->error)); } } + + public function listPermissions(string $user, ?string $host = 'localhost'): array + { + $results = $this->conn->query(sprintf("SHOW GRANTS FOR '%s'@'%s'", $user, $host), ); + + $permissions = []; + while ($result = $results->fetch_array()) { + $permissions[] = $result[0]; + } + + return $permissions; + } } \ No newline at end of file