Skip to content

Commit 0351114

Browse files
committed
feat: add ability to select which databases to give a user access to
1 parent 107cf02 commit 0351114

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/ApiClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ public function createDatabaseServer(string $name, int $networkId, string $type,
133133
/**
134134
* Create a new user on the given database server.
135135
*/
136-
public function createDatabaseUser(int $databaseId, string $username): Collection
136+
public function createDatabaseUser(int $databaseId, string $username, array $databases = []): Collection
137137
{
138138
return $this->request('post', "/database-servers/{$databaseId}/users", [
139+
'databases' => $databases,
139140
'username' => $username,
140141
]);
141142
}

src/Command/Database/CreateDatabaseUserCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,18 @@ protected function configure()
4444
protected function perform(InputInterface $input, ConsoleOutput $output)
4545
{
4646
$databaseId = $this->determineDatabaseServer('On which database server would you like to create the new database user?', $input, $output);
47+
$databases = [];
4748
$username = $this->getStringArgument($input, 'username');
4849

4950
if (empty($username) && $input->isInteractive()) {
5051
$username = $output->ask('What is the username of the new database user');
5152
}
5253

53-
$user = $this->apiClient->createDatabaseUser($databaseId, $username);
54+
if (!$output->confirm('Do you want to the new user to have access to all databases?', false)) {
55+
$databases = $output->multichoice('Please enter the comma-separated list of databases that you want the user to have access to', $this->apiClient->getDatabases($databaseId)->all());
56+
}
57+
58+
$user = $this->apiClient->createDatabaseUser($databaseId, $username, $databases);
5459

5560
$output->horizontalTable(
5661
['Username', 'Password'],

src/Console/ConsoleOutput.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ public function infoWithWarning(string $message, string $warning, bool $newline
116116
$this->write(sprintf('<info>%s</info> (<comment>%s</comment>)', $message, $warning), $newline);
117117
}
118118

119+
/**
120+
* Ask a multiselect choice question.
121+
*/
122+
public function multichoice($question, array $choices, $default = null): array
123+
{
124+
if (null !== $default) {
125+
$values = array_flip($choices);
126+
$default = $values[$default];
127+
}
128+
129+
$question = new ChoiceQuestion($question, $choices, $default);
130+
$question->setMultiselect(true);
131+
132+
return (array) $this->askQuestion($question);
133+
}
134+
119135
/**
120136
* Write out a warning message.
121137
*/

0 commit comments

Comments
 (0)