Skip to content

Commit bac7cfa

Browse files
committed
PHPLIB-545: Support the authorizedDatabases options for listDatabases
1 parent 9440373 commit bac7cfa

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

docs/includes/apiargs-MongoDBClient-method-listDatabases-option.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
arg_name: option
2+
name: authorizedDatabases
3+
type: boolean
4+
description: |
5+
A flag that determines which databases are returned based on the user
6+
privileges when access control is enabled. For more information, see the
7+
`listDatabases command documentation <https://docs.mongodb.com/manual/reference/command/listDatabases/#dbcmd.listDatabases>`_.
8+
9+
For servers < 4.0.5, this option is ignored.
10+
11+
.. versionadded:: 1.7
12+
---
13+
arg_name: option
214
name: filter
315
type: array|object
416
description: |

src/Operation/ListDatabases.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use MongoDB\Model\DatabaseInfoLegacyIterator;
2828
use function current;
2929
use function is_array;
30+
use function is_bool;
3031
use function is_integer;
3132
use function is_object;
3233

@@ -47,6 +48,11 @@ class ListDatabases implements Executable
4748
*
4849
* Supported options:
4950
*
51+
* * authorizedDatabases (boolean): Determines which databases are returned
52+
* based on the user privileges.
53+
*
54+
* For servers < 4.0.5, this option is ignored.
55+
*
5056
* * filter (document): Query by which to filter databases.
5157
*
5258
* For servers < 3.6, this option is ignored.
@@ -63,6 +69,10 @@ class ListDatabases implements Executable
6369
*/
6470
public function __construct(array $options = [])
6571
{
72+
if (isset($options['authorizedDatabases']) && ! is_bool($options['authorizedDatabases'])) {
73+
throw InvalidArgumentException::invalidType('"authorizedDatabases" option', $options['authorizedDatabases'], 'boolean');
74+
}
75+
6676
if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
6777
throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object');
6878
}
@@ -91,6 +101,10 @@ public function execute(Server $server)
91101
{
92102
$cmd = ['listDatabases' => 1];
93103

104+
if (isset($this->options['authorizedDatabases'])) {
105+
$cmd['authorizedDatabases'] = $this->options['authorizedDatabases'];
106+
}
107+
94108
if (! empty($this->options['filter'])) {
95109
$cmd['filter'] = (object) $this->options['filter'];
96110
}

tests/Operation/ListDatabasesFunctionalTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,17 @@ public function testListDatabases()
1919
$writeResult = $insertOne->execute($server);
2020
$this->assertEquals(1, $writeResult->getInsertedCount());
2121

22-
$operation = new ListDatabases();
23-
$databases = $operation->execute($server);
22+
$databases = null;
23+
(new CommandObserver())->observe(
24+
function () use (&$databases, $server) {
25+
$operation = new ListDatabases();
26+
27+
$databases = $operation->execute($server);
28+
},
29+
function (array $event) {
30+
$this->assertObjectNotHasAttribute('authorizedDatabases', $event['started']->getCommand());
31+
}
32+
);
2433

2534
$this->assertInstanceOf(DatabaseInfoIterator::class, $databases);
2635

@@ -29,6 +38,22 @@ public function testListDatabases()
2938
}
3039
}
3140

41+
public function testAuthorizedDatabasesOption()
42+
{
43+
(new CommandObserver())->observe(
44+
function () {
45+
$operation = new ListDatabases(
46+
['authorizedDatabases' => true]
47+
);
48+
49+
$operation->execute($this->getPrimaryServer());
50+
},
51+
function (array $event) {
52+
$this->assertObjectHasAttribute('authorizedDatabases', $event['started']->getCommand());
53+
}
54+
);
55+
}
56+
3257
public function testFilterOption()
3358
{
3459
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {

tests/Operation/ListDatabasesTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function provideInvalidConstructorOptions()
2020
{
2121
$options = [];
2222

23+
foreach ($this->getInvalidBooleanValues() as $value) {
24+
$options[][] = ['authorizedDatabases' => $value];
25+
}
26+
2327
foreach ($this->getInvalidDocumentValues() as $value) {
2428
$options[][] = ['filter' => $value];
2529
}

0 commit comments

Comments
 (0)