From d78f52b9656d61917c2ed3df2bab31ef377668a1 Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 19 Sep 2021 21:12:09 +0200 Subject: [PATCH] Add list users command --- src/Console/CreateDatabaseCommand.php | 1 - src/Console/ListDatabasesCommand.php | 6 ----- src/Console/ListUsersCommand.php | 37 +++++++++++++++++++++++++++ src/Service/DatabaseManager.php | 18 ++++++++++++- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/Console/ListUsersCommand.php diff --git a/src/Console/CreateDatabaseCommand.php b/src/Console/CreateDatabaseCommand.php index d6545f7..090993a 100644 --- a/src/Console/CreateDatabaseCommand.php +++ b/src/Console/CreateDatabaseCommand.php @@ -11,7 +11,6 @@ use Symfony\Component\Console\Question\Question; class CreateDatabaseCommand extends Command { - // the name of the command (the part after "bin/console") protected static $defaultName = 'app:db:create'; public function __construct( diff --git a/src/Console/ListDatabasesCommand.php b/src/Console/ListDatabasesCommand.php index d705485..558f2ab 100644 --- a/src/Console/ListDatabasesCommand.php +++ b/src/Console/ListDatabasesCommand.php @@ -9,7 +9,6 @@ use Symfony\Component\Console\Output\OutputInterface; class ListDatabasesCommand extends Command { - // the name of the command (the part after "bin/console") protected static $defaultName = 'app:db:list'; public function __construct( @@ -19,11 +18,6 @@ class ListDatabasesCommand extends Command parent::__construct(); } - protected function configure(): void - { - // ... - } - protected function execute(InputInterface $input, OutputInterface $output): int { $dbs = $this->db->listDatabases(); diff --git a/src/Console/ListUsersCommand.php b/src/Console/ListUsersCommand.php new file mode 100644 index 0000000..d34aab2 --- /dev/null +++ b/src/Console/ListUsersCommand.php @@ -0,0 +1,37 @@ +db->listUsers(); + + $output->writeln('List of users:'); + foreach ($users as $user) { + $line = sprintf(' -%s @%s', $user['name'], $user['host']); + if (!empty($plugin = $user['plugin'])) { + $line .= sprintf(' (%s)', $plugin); + } + + $output->writeln($line); + } + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Service/DatabaseManager.php b/src/Service/DatabaseManager.php index c102186..e15c5a1 100644 --- a/src/Service/DatabaseManager.php +++ b/src/Service/DatabaseManager.php @@ -29,7 +29,7 @@ class DatabaseManager public function createDatabase(string $name) { $dbs = $this->listDatabases(); - if(in_array($name, $dbs)) { + if (in_array($name, $dbs)) { throw new \Exception(sprintf('Database "%s" already exists', $name)); } @@ -38,4 +38,20 @@ class DatabaseManager throw new \Exception(sprintf('Database create error: %s', $this->conn->error)); } } + + public function listUsers(): array + { + $results = $this->conn->query('select * from mysql.user'); + + $users = []; + while ($result = $results->fetch_object()) { + $users[] = [ + 'name' => $result->User, + 'host' => $result->Host, + 'plugin' => $result->plugin, + ]; + } + + return $users; + } } \ No newline at end of file