Add list users command
This commit is contained in:
parent
2e8469b11a
commit
d78f52b965
@ -11,7 +11,6 @@ use Symfony\Component\Console\Question\Question;
|
|||||||
|
|
||||||
class CreateDatabaseCommand extends Command
|
class CreateDatabaseCommand extends Command
|
||||||
{
|
{
|
||||||
// the name of the command (the part after "bin/console")
|
|
||||||
protected static $defaultName = 'app:db:create';
|
protected static $defaultName = 'app:db:create';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -9,7 +9,6 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
|
|
||||||
class ListDatabasesCommand extends Command
|
class ListDatabasesCommand extends Command
|
||||||
{
|
{
|
||||||
// the name of the command (the part after "bin/console")
|
|
||||||
protected static $defaultName = 'app:db:list';
|
protected static $defaultName = 'app:db:list';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@ -19,11 +18,6 @@ class ListDatabasesCommand extends Command
|
|||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure(): void
|
|
||||||
{
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$dbs = $this->db->listDatabases();
|
$dbs = $this->db->listDatabases();
|
||||||
|
37
src/Console/ListUsersCommand.php
Normal file
37
src/Console/ListUsersCommand.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,7 @@ class DatabaseManager
|
|||||||
public function createDatabase(string $name)
|
public function createDatabase(string $name)
|
||||||
{
|
{
|
||||||
$dbs = $this->listDatabases();
|
$dbs = $this->listDatabases();
|
||||||
if(in_array($name, $dbs)) {
|
if (in_array($name, $dbs)) {
|
||||||
throw new \Exception(sprintf('Database "%s" already exists', $name));
|
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));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user