Add list permissions command

This commit is contained in:
Tim 2024-09-24 00:04:40 +02:00
parent 62e8f87ec8
commit bb31bb34b8
6 changed files with 77 additions and 16 deletions

View File

@ -29,10 +29,11 @@ class DeleteUserCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$question = $this->getHelper('question');
$host = $input->getArgument('host'); $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( $selectQuestion = new ChoiceQuestion('User name: ', array_map(
fn($user) => $user['name'], fn($user) => $user['name'],
$this->db->listUsers()) $this->db->listUsers())

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 ListPermissionsCommand 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,11 +5,13 @@ namespace App\Console;
use App\Service\DatabaseService; use App\Service\DatabaseService;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand('app:user:list')] #[AsCommand('app:permission:list')]
class ListUsersCommand extends Command class ListUsersCommand extends Command
{ {
public function __construct( public function __construct(
@ -19,19 +21,37 @@ class ListUsersCommand extends Command
parent::__construct(); 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 protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$style = new SymfonyStyle($input, $output); $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 $table = $style
->createTable() ->createTable()
->setHeaders(['Name', 'Host', 'Plugin']) ->setHeaders(['Permission'])
->setHeaderTitle('List of users') ->setHeaderTitle(sprintf('List of users for %s@%s', $name, $host))
; ;
foreach ($users as $user) { foreach ($permissions as $permission) {
$table->addRow([$user['name'], $user['host'], $user['plugin'] ?? '']); $table->addRow([$permission]);
} }
$table->render(); $table->render();

View File

@ -2,8 +2,6 @@
namespace App\Service; namespace App\Service;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\InputStream;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
class BackupService class BackupService

View File

@ -5,12 +5,10 @@ namespace App\Service;
class DatabaseCredentials class DatabaseCredentials
{ {
public function __construct( public function __construct(
private string $hostname, private readonly string $hostname,
private string $user, private readonly string $user,
private string $password, private readonly string $password,
) ) {}
{
}
public function getHostname(): string public function getHostname(): string
{ {

View File

@ -110,4 +110,16 @@ class DatabaseService
throw new Exception(sprintf('User delete error: %s', $this->conn->error)); 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;
}
} }