Add list users command

This commit is contained in:
Tim 2021-09-19 21:12:09 +02:00
parent 2e8469b11a
commit d78f52b965
4 changed files with 54 additions and 8 deletions

View File

@ -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(

View File

@ -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();

View File

@ -0,0 +1,37 @@
<?php
namespace App\Console;
use App\Service\DatabaseManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ListUsersCommand extends Command
{
protected static $defaultName = 'app:users:list';
public function __construct(
private DatabaseManager $db,
)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$users = $this->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;
}
}

View File

@ -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;
}
}