Skip to content

Commit c41a46f

Browse files
authored
PHPLIB-740: Support authorizedCollections option for listCollections (#884)
* PHPLIB-740: Support authorizedCollections option for listCollections * Update tests and docs for authorizedDatabases option
1 parent 59ef50a commit c41a46f

11 files changed

+85
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type: boolean
44
description: |
55
A flag that determines which databases are returned based on the user
66
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>`_.
7+
`listDatabases command documentation <https://docs.mongodb.com/manual/reference/command/listDatabases/>`_.
88
99
For servers < 4.0.5, this option is ignored.
1010

docs/includes/apiargs-MongoDBDatabase-method-listCollections-option.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
arg_name: option
2+
name: authorizedCollections
3+
type: boolean
4+
description: |
5+
A flag that determines which collections are returned based on the user
6+
privileges when access control is enabled. For more information, see the
7+
`listCollections command documentation <https://docs.mongodb.com/manual/reference/command/listCollections/>`_.
8+
9+
For servers < 4.0, this option is ignored.
10+
11+
.. versionadded:: 1.12
12+
interface: phpmethod
13+
operation: ~
14+
optional: true
15+
---
16+
arg_name: option
217
name: filter
318
type: array|object
419
description: |

src/Command/ListCollections.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class ListCollections implements Executable
4949
*
5050
* Supported options:
5151
*
52+
* * authorizedCollections (boolean): Determines which collections are
53+
* returned based on the user privileges.
54+
*
55+
* For servers < 4.0, this option is ignored.
56+
*
5257
* * filter (document): Query by which to filter collections.
5358
*
5459
* * maxTimeMS (integer): The maximum amount of time to allow the query to
@@ -68,6 +73,10 @@ class ListCollections implements Executable
6873
*/
6974
public function __construct($databaseName, array $options = [])
7075
{
76+
if (isset($options['authorizedCollections']) && ! is_bool($options['authorizedCollections'])) {
77+
throw InvalidArgumentException::invalidType('"authorizedCollections" option', $options['authorizedCollections'], 'boolean');
78+
}
79+
7180
if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
7281
throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object');
7382
}
@@ -104,12 +113,10 @@ public function execute(Server $server)
104113
$cmd['filter'] = (object) $this->options['filter'];
105114
}
106115

107-
if (isset($this->options['maxTimeMS'])) {
108-
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
109-
}
110-
111-
if (isset($this->options['nameOnly'])) {
112-
$cmd['nameOnly'] = $this->options['nameOnly'];
116+
foreach (['authorizedCollections', 'maxTimeMS', 'nameOnly'] as $option) {
117+
if (isset($this->options[$option])) {
118+
$cmd[$option] = $this->options[$option];
119+
}
113120
}
114121

115122
$cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());

src/Command/ListDatabases.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,14 @@ public function execute(Server $server)
108108
{
109109
$cmd = ['listDatabases' => 1];
110110

111-
if (isset($this->options['authorizedDatabases'])) {
112-
$cmd['authorizedDatabases'] = $this->options['authorizedDatabases'];
113-
}
114-
115111
if (! empty($this->options['filter'])) {
116112
$cmd['filter'] = (object) $this->options['filter'];
117113
}
118114

119-
if (isset($this->options['maxTimeMS'])) {
120-
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
121-
}
122-
123-
if (isset($this->options['nameOnly'])) {
124-
$cmd['nameOnly'] = $this->options['nameOnly'];
115+
foreach (['authorizedDatabases', 'maxTimeMS', 'nameOnly'] as $option) {
116+
if (isset($this->options[$option])) {
117+
$cmd[$option] = $this->options[$option];
118+
}
125119
}
126120

127121
$cursor = $server->executeReadCommand('admin', new Command($cmd), $this->createOptions());

src/Operation/ListCollectionNames.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class ListCollectionNames implements Executable
4141
*
4242
* Supported options:
4343
*
44+
* * authorizedCollections (boolean): Determines which collections are
45+
* returned based on the user privileges.
46+
*
47+
* For servers < 4.0, this option is ignored.
48+
*
4449
* * filter (document): Query by which to filter collections.
4550
*
4651
* * maxTimeMS (integer): The maximum amount of time to allow the query to

src/Operation/ListCollections.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class ListCollections implements Executable
4444
*
4545
* Supported options:
4646
*
47+
* * authorizedCollections (boolean): Determines which collections are
48+
* returned based on the user privileges.
49+
*
50+
* For servers < 4.0, this option is ignored.
51+
*
4752
* * filter (document): Query by which to filter collections.
4853
*
4954
* * maxTimeMS (integer): The maximum amount of time to allow the query to

tests/Command/ListCollectionsTest.php

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

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

tests/Operation/ListCollectionNamesFunctionalTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ public function testListCollectionNamesForNewlyCreatedDatabase(): void
3131
}
3232
}
3333

34+
public function testAuthorizedCollectionsOption(): void
35+
{
36+
(new CommandObserver())->observe(
37+
function (): void {
38+
$operation = new ListCollectionNames(
39+
$this->getDatabaseName(),
40+
['authorizedCollections' => true]
41+
);
42+
43+
$operation->execute($this->getPrimaryServer());
44+
},
45+
function (array $event): void {
46+
$this->assertObjectHasAttribute('authorizedCollections', $event['started']->getCommand());
47+
$this->assertSame(true, $event['started']->getCommand()->authorizedCollections);
48+
}
49+
);
50+
}
51+
3452
public function testSessionOption(): void
3553
{
3654
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {

tests/Operation/ListCollectionsFunctionalTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ public function testListCollectionsForNonexistentDatabase(): void
8080
$this->assertCount(0, $collections);
8181
}
8282

83+
public function testAuthorizedCollectionsOption(): void
84+
{
85+
(new CommandObserver())->observe(
86+
function (): void {
87+
$operation = new ListCollections(
88+
$this->getDatabaseName(),
89+
['authorizedCollections' => true]
90+
);
91+
92+
$operation->execute($this->getPrimaryServer());
93+
},
94+
function (array $event): void {
95+
$this->assertObjectHasAttribute('authorizedCollections', $event['started']->getCommand());
96+
$this->assertSame(true, $event['started']->getCommand()->authorizedCollections);
97+
}
98+
);
99+
}
100+
83101
public function testSessionOption(): void
84102
{
85103
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {

tests/Operation/ListDatabaseNamesFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function (): void {
4848
},
4949
function (array $event): void {
5050
$this->assertObjectHasAttribute('authorizedDatabases', $event['started']->getCommand());
51-
$this->assertSame(true, $event['started']->getCommand()->nameOnly);
51+
$this->assertSame(true, $event['started']->getCommand()->authorizedDatabases);
5252
}
5353
);
5454
}

0 commit comments

Comments
 (0)