Implement create permission command

This commit is contained in:
Tim 2024-09-24 00:42:03 +02:00
parent 153f04ccd7
commit 9887282b62
7 changed files with 68 additions and 53 deletions

View File

@ -4,16 +4,19 @@ namespace App\Console;
use App\Service\DatabaseService; use App\Service\DatabaseService;
use App\Service\Traits\SelectDatabaseQuestion; use App\Service\Traits\SelectDatabaseQuestion;
use App\Service\Traits\SelectUserQuestion;
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\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;
#[AsCommand('app:permissions:list')] #[AsCommand('app:permission:create')]
class CreatePermissionCommand extends Command class CreatePermissionCommand extends Command
{ {
use SelectDatabaseQuestion; use SelectDatabaseQuestion;
use SelectUserQuestion;
public function __construct( public function __construct(
private readonly DatabaseService $db, private readonly DatabaseService $db,
@ -24,18 +27,31 @@ class CreatePermissionCommand extends Command
public function configure(): void public function configure(): void
{ {
$this->addArgument('name', InputArgument::OPTIONAL, 'User name'); $this->addArgument('user', InputArgument::OPTIONAL, 'User name');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host');
$this->addArgument('database', InputArgument::OPTIONAL, 'Database'); $this->addArgument('database', InputArgument::OPTIONAL, 'Database');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host');
} }
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$dbs = $this->db->listDatabases(); $question = $this->getHelper('question');
$output->writeln('List of permissions for user:'); $user = $this->getUserName($input, $output);
foreach ($dbs as $db) { $host = $input->getArgument('host');
$output->writeln(sprintf(' -%s', $db));
$permissionTypeQuestion = new ChoiceQuestion('Permission type: ', [
'admin' => 'Admin',
'db_admin' => 'Database admin',
]);
$permissionType = $question->ask($input, $output, $permissionTypeQuestion);
if ($permissionType === 'admin') {
throw new \Exception('Not implemented');
} else {
$db = $this->getDatabaseName($input, $output);
$this->db->grantDatabaseAdminPermission($user, $db, $host);
$output->writeln(sprintf('Database admin permission granted to "%s" on "%s"', $user, $db));
} }
return Command::SUCCESS; return Command::SUCCESS;

View File

@ -22,7 +22,7 @@ class CreateUserCommand extends Command
public function configure(): void public function configure(): void
{ {
$this->addArgument('name', InputArgument::OPTIONAL, 'User name'); $this->addArgument('user', InputArgument::OPTIONAL, 'User name');
$this->addArgument('password', InputArgument::OPTIONAL, 'User password'); $this->addArgument('password', InputArgument::OPTIONAL, 'User password');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host'); $this->addArgument('host', InputArgument::OPTIONAL, 'User host');
} }
@ -31,12 +31,12 @@ class CreateUserCommand extends Command
{ {
$question = $this->getHelper('question'); $question = $this->getHelper('question');
$name = $input->getArgument('name'); $user = $input->getArgument('user');
$password = $input->getArgument('password'); $password = $input->getArgument('password');
$host = $input->getArgument('host'); $host = $input->getArgument('host');
if (!$name) { if (!$user) {
$name = $question->ask($input, $output, new Question('User name: ')); $user = $question->ask($input, $output, new Question('User name: '));
} }
if (!$password) { if (!$password) {
$passwordQuestion = new Question('User password: '); $passwordQuestion = new Question('User password: ');
@ -44,9 +44,9 @@ class CreateUserCommand extends Command
$password = $question->ask($input, $output, $passwordQuestion); $password = $question->ask($input, $output, $passwordQuestion);
} }
$this->db->createUser($name, $password, $host); $this->db->createUser($user, $password, $host);
$output->writeln(sprintf('User "%s" successfully created', $name)); $output->writeln(sprintf('User "%s" successfully created', $user));
return Command::SUCCESS; return Command::SUCCESS;
} }

View File

@ -3,6 +3,7 @@
namespace App\Console; namespace App\Console;
use App\Service\DatabaseService; use App\Service\DatabaseService;
use App\Service\Traits\SelectUserQuestion;
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\InputArgument;
@ -14,6 +15,8 @@ use Symfony\Component\Console\Question\Question;
#[AsCommand('app:user:delete')] #[AsCommand('app:user:delete')]
class DeleteUserCommand extends Command class DeleteUserCommand extends Command
{ {
use SelectUserQuestion;
public function __construct( public function __construct(
private readonly DatabaseService $db, private readonly DatabaseService $db,
) )
@ -23,27 +26,18 @@ class DeleteUserCommand extends Command
public function configure(): void public function configure(): void
{ {
$this->addArgument('name', InputArgument::OPTIONAL, 'User name'); $this->addArgument('user', InputArgument::OPTIONAL, 'User name');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host'); $this->addArgument('host', InputArgument::OPTIONAL, 'User host');
} }
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$host = $input->getArgument('host'); $host = $input->getArgument('host');
$name = $input->getArgument('name'); $user = $this->getUserName($input, $output);
if (!$name) { $this->db->deleteUser($user, $host);
$question = $this->getHelper('question');
$selectQuestion = new ChoiceQuestion('User name: ', array_map(
fn($user) => $user['name'],
$this->db->listUsers())
);
$name = $question->ask($input, $output, $selectQuestion);
}
$this->db->deleteUser($name, $host); $output->writeln(sprintf('User "%s" successfully deleted', $user));
$output->writeln(sprintf('User "%s" successfully deleted', $name));
return Command::SUCCESS; return Command::SUCCESS;
} }

View File

@ -3,17 +3,19 @@
namespace App\Console; namespace App\Console;
use App\Service\DatabaseService; use App\Service\DatabaseService;
use App\Service\Traits\SelectUserQuestion;
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\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:permission:list')] #[AsCommand('app:permission:list')]
class ListPermissionsCommand extends Command class ListPermissionsCommand extends Command
{ {
use SelectUserQuestion;
public function __construct( public function __construct(
private readonly DatabaseService $db, private readonly DatabaseService $db,
) )
@ -23,7 +25,7 @@ class ListPermissionsCommand extends Command
public function configure(): void public function configure(): void
{ {
$this->addArgument('name', InputArgument::OPTIONAL, 'User name'); $this->addArgument('user', InputArgument::OPTIONAL, 'User name');
$this->addArgument('host', InputArgument::OPTIONAL, 'User host'); $this->addArgument('host', InputArgument::OPTIONAL, 'User host');
} }
@ -31,24 +33,15 @@ class ListPermissionsCommand extends Command
{ {
$style = new SymfonyStyle($input, $output); $style = new SymfonyStyle($input, $output);
$name = $input->getArgument('name');
$host = $input->getArgument('host'); $host = $input->getArgument('host');
$user = $this->getUserName($input, $output);
if (!$name) { $permissions = $this->db->listPermissions($user, $host);
$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(['Permission']) ->setHeaders(['Permission'])
->setHeaderTitle(sprintf('List of users for %s@%s', $name, $host)) ->setHeaderTitle(sprintf('List of users for %s@%s', $user, $host))
; ;
foreach ($permissions as $permission) { foreach ($permissions as $permission) {
$table->addRow([$permission]); $table->addRow([$permission]);

View File

@ -114,7 +114,7 @@ class DatabaseService
public function listPermissions(string $user, ?string $host = null): array public function listPermissions(string $user, ?string $host = null): array
{ {
$host ??= 'localhost'; $host ??= 'localhost';
$results = $this->conn->query(sprintf("SHOW GRANTS FOR '%s'@'%s'", $user, $host), ); $results = $this->conn->query(sprintf("SHOW GRANTS FOR '%s'@'%s'", $user, $host));
$permissions = []; $permissions = [];
while ($result = $results->fetch_array()) { while ($result = $results->fetch_array()) {
@ -123,4 +123,13 @@ class DatabaseService
return $permissions; return $permissions;
} }
public function grantDatabaseAdminPermission(string $user, string $db, ?string $host = null): void
{
$host ??= 'localhost';
$result = $this->conn->query(sprintf("GRANT ALL PRIVILEGES ON `%s`.* TO '%s'@'%s'", $db, $user, $host));
if (!$result) {
throw new Exception(sprintf('Permission grant error: %s', $this->conn->error));
}
}
} }

View File

@ -10,13 +10,13 @@ trait SelectDatabaseQuestion
{ {
private function getDatabaseName(InputInterface $input, OutputInterface $output): string private function getDatabaseName(InputInterface $input, OutputInterface $output): string
{ {
$question = $this->getHelper('question'); if (!$database = $input->getArgument('database')) {
$question = $this->getHelper('question');
if (!$name = $input->getArgument('database')) { $databases = $this->db->listDatabases();
$selectQuestion = new ChoiceQuestion('Database name: ', $this->db->listDatabases()); $selectQuestion = new ChoiceQuestion('Database name: ', array_combine($databases, $databases));
$name = $question->ask($input, $output, $selectQuestion); $database = $question->ask($input, $output, $selectQuestion);
} }
return $name; return $database;
} }
} }

View File

@ -10,13 +10,16 @@ trait SelectUserQuestion
{ {
private function getUserName(InputInterface $input, OutputInterface $output): string private function getUserName(InputInterface $input, OutputInterface $output): string
{ {
$question = $this->getHelper('question'); $user = $input->getArgument('user');
if (!$user) {
if (!$name = $input->getArgument('name')) { $question = $this->getHelper('question');
$selectQuestion = new ChoiceQuestion('Database name: ', $this->db->listDatabases()); $users = array_map(
$name = $question->ask($input, $output, $selectQuestion); fn($user) => $user['name'],
$this->db->listUsers());
$selectQuestion = new ChoiceQuestion('User name: ', array_combine($users, $users));
$user = $question->ask($input, $output, $selectQuestion);
} }
return $name; return $user;
} }
} }