Implement create permission command
This commit is contained in:
@ -114,7 +114,7 @@ class DatabaseService
|
||||
public function listPermissions(string $user, ?string $host = null): array
|
||||
{
|
||||
$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 = [];
|
||||
while ($result = $results->fetch_array()) {
|
||||
@ -123,4 +123,13 @@ class DatabaseService
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
@ -10,13 +10,13 @@ trait SelectDatabaseQuestion
|
||||
{
|
||||
private function getDatabaseName(InputInterface $input, OutputInterface $output): string
|
||||
{
|
||||
$question = $this->getHelper('question');
|
||||
|
||||
if (!$name = $input->getArgument('database')) {
|
||||
$selectQuestion = new ChoiceQuestion('Database name: ', $this->db->listDatabases());
|
||||
$name = $question->ask($input, $output, $selectQuestion);
|
||||
if (!$database = $input->getArgument('database')) {
|
||||
$question = $this->getHelper('question');
|
||||
$databases = $this->db->listDatabases();
|
||||
$selectQuestion = new ChoiceQuestion('Database name: ', array_combine($databases, $databases));
|
||||
$database = $question->ask($input, $output, $selectQuestion);
|
||||
}
|
||||
|
||||
return $name;
|
||||
return $database;
|
||||
}
|
||||
}
|
@ -10,13 +10,16 @@ trait SelectUserQuestion
|
||||
{
|
||||
private function getUserName(InputInterface $input, OutputInterface $output): string
|
||||
{
|
||||
$question = $this->getHelper('question');
|
||||
|
||||
if (!$name = $input->getArgument('name')) {
|
||||
$selectQuestion = new ChoiceQuestion('Database name: ', $this->db->listDatabases());
|
||||
$name = $question->ask($input, $output, $selectQuestion);
|
||||
$user = $input->getArgument('user');
|
||||
if (!$user) {
|
||||
$question = $this->getHelper('question');
|
||||
$users = array_map(
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user