diff --git a/docs/includes/apiargs-MongoDBClient-method-listDatabases-option.yaml b/docs/includes/apiargs-MongoDBClient-method-listDatabases-option.yaml index 4e3d616d8..b9a9c23e2 100644 --- a/docs/includes/apiargs-MongoDBClient-method-listDatabases-option.yaml +++ b/docs/includes/apiargs-MongoDBClient-method-listDatabases-option.yaml @@ -4,7 +4,7 @@ type: boolean description: | A flag that determines which databases are returned based on the user privileges when access control is enabled. For more information, see the - `listDatabases command documentation `_. + `listDatabases command documentation `_. For servers < 4.0.5, this option is ignored. diff --git a/docs/includes/apiargs-MongoDBDatabase-method-listCollections-option.yaml b/docs/includes/apiargs-MongoDBDatabase-method-listCollections-option.yaml index 0d9ab968d..de05bf339 100644 --- a/docs/includes/apiargs-MongoDBDatabase-method-listCollections-option.yaml +++ b/docs/includes/apiargs-MongoDBDatabase-method-listCollections-option.yaml @@ -1,4 +1,19 @@ arg_name: option +name: authorizedCollections +type: boolean +description: | + A flag that determines which collections are returned based on the user + privileges when access control is enabled. For more information, see the + `listCollections command documentation `_. + + For servers < 4.0, this option is ignored. + + .. versionadded:: 1.12 +interface: phpmethod +operation: ~ +optional: true +--- +arg_name: option name: filter type: array|object description: | diff --git a/src/Command/ListCollections.php b/src/Command/ListCollections.php index 4302e4f09..e3a28d374 100644 --- a/src/Command/ListCollections.php +++ b/src/Command/ListCollections.php @@ -49,6 +49,11 @@ class ListCollections implements Executable * * Supported options: * + * * authorizedCollections (boolean): Determines which collections are + * returned based on the user privileges. + * + * For servers < 4.0, this option is ignored. + * * * filter (document): Query by which to filter collections. * * * maxTimeMS (integer): The maximum amount of time to allow the query to @@ -68,6 +73,10 @@ class ListCollections implements Executable */ public function __construct($databaseName, array $options = []) { + if (isset($options['authorizedCollections']) && ! is_bool($options['authorizedCollections'])) { + throw InvalidArgumentException::invalidType('"authorizedCollections" option', $options['authorizedCollections'], 'boolean'); + } + if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) { throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object'); } @@ -104,12 +113,10 @@ public function execute(Server $server) $cmd['filter'] = (object) $this->options['filter']; } - if (isset($this->options['maxTimeMS'])) { - $cmd['maxTimeMS'] = $this->options['maxTimeMS']; - } - - if (isset($this->options['nameOnly'])) { - $cmd['nameOnly'] = $this->options['nameOnly']; + foreach (['authorizedCollections', 'maxTimeMS', 'nameOnly'] as $option) { + if (isset($this->options[$option])) { + $cmd[$option] = $this->options[$option]; + } } $cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions()); diff --git a/src/Command/ListDatabases.php b/src/Command/ListDatabases.php index 0a18d4961..cebddf781 100644 --- a/src/Command/ListDatabases.php +++ b/src/Command/ListDatabases.php @@ -108,20 +108,14 @@ public function execute(Server $server) { $cmd = ['listDatabases' => 1]; - if (isset($this->options['authorizedDatabases'])) { - $cmd['authorizedDatabases'] = $this->options['authorizedDatabases']; - } - if (! empty($this->options['filter'])) { $cmd['filter'] = (object) $this->options['filter']; } - if (isset($this->options['maxTimeMS'])) { - $cmd['maxTimeMS'] = $this->options['maxTimeMS']; - } - - if (isset($this->options['nameOnly'])) { - $cmd['nameOnly'] = $this->options['nameOnly']; + foreach (['authorizedDatabases', 'maxTimeMS', 'nameOnly'] as $option) { + if (isset($this->options[$option])) { + $cmd[$option] = $this->options[$option]; + } } $cursor = $server->executeReadCommand('admin', new Command($cmd), $this->createOptions()); diff --git a/src/Operation/ListCollectionNames.php b/src/Operation/ListCollectionNames.php index 4c7c03b34..e21af9e3d 100644 --- a/src/Operation/ListCollectionNames.php +++ b/src/Operation/ListCollectionNames.php @@ -41,6 +41,11 @@ class ListCollectionNames implements Executable * * Supported options: * + * * authorizedCollections (boolean): Determines which collections are + * returned based on the user privileges. + * + * For servers < 4.0, this option is ignored. + * * * filter (document): Query by which to filter collections. * * * maxTimeMS (integer): The maximum amount of time to allow the query to diff --git a/src/Operation/ListCollections.php b/src/Operation/ListCollections.php index 45cba5463..898091c98 100644 --- a/src/Operation/ListCollections.php +++ b/src/Operation/ListCollections.php @@ -44,6 +44,11 @@ class ListCollections implements Executable * * Supported options: * + * * authorizedCollections (boolean): Determines which collections are + * returned based on the user privileges. + * + * For servers < 4.0, this option is ignored. + * * * filter (document): Query by which to filter collections. * * * maxTimeMS (integer): The maximum amount of time to allow the query to diff --git a/tests/Command/ListCollectionsTest.php b/tests/Command/ListCollectionsTest.php index 85cabab3b..8ea941a75 100644 --- a/tests/Command/ListCollectionsTest.php +++ b/tests/Command/ListCollectionsTest.php @@ -21,6 +21,10 @@ public function provideInvalidConstructorOptions() { $options = []; + foreach ($this->getInvalidBooleanValues() as $value) { + $options[][] = ['authorizedCollections' => $value]; + } + foreach ($this->getInvalidDocumentValues() as $value) { $options[][] = ['filter' => $value]; } diff --git a/tests/Operation/ListCollectionNamesFunctionalTest.php b/tests/Operation/ListCollectionNamesFunctionalTest.php index 8f222fa5a..15f121c64 100644 --- a/tests/Operation/ListCollectionNamesFunctionalTest.php +++ b/tests/Operation/ListCollectionNamesFunctionalTest.php @@ -31,6 +31,24 @@ public function testListCollectionNamesForNewlyCreatedDatabase(): void } } + public function testAuthorizedCollectionsOption(): void + { + (new CommandObserver())->observe( + function (): void { + $operation = new ListCollectionNames( + $this->getDatabaseName(), + ['authorizedCollections' => true] + ); + + $operation->execute($this->getPrimaryServer()); + }, + function (array $event): void { + $this->assertObjectHasAttribute('authorizedCollections', $event['started']->getCommand()); + $this->assertSame(true, $event['started']->getCommand()->authorizedCollections); + } + ); + } + public function testSessionOption(): void { if (version_compare($this->getServerVersion(), '3.6.0', '<')) { diff --git a/tests/Operation/ListCollectionsFunctionalTest.php b/tests/Operation/ListCollectionsFunctionalTest.php index 4b1fdb7e6..abe184b25 100644 --- a/tests/Operation/ListCollectionsFunctionalTest.php +++ b/tests/Operation/ListCollectionsFunctionalTest.php @@ -80,6 +80,24 @@ public function testListCollectionsForNonexistentDatabase(): void $this->assertCount(0, $collections); } + public function testAuthorizedCollectionsOption(): void + { + (new CommandObserver())->observe( + function (): void { + $operation = new ListCollections( + $this->getDatabaseName(), + ['authorizedCollections' => true] + ); + + $operation->execute($this->getPrimaryServer()); + }, + function (array $event): void { + $this->assertObjectHasAttribute('authorizedCollections', $event['started']->getCommand()); + $this->assertSame(true, $event['started']->getCommand()->authorizedCollections); + } + ); + } + public function testSessionOption(): void { if (version_compare($this->getServerVersion(), '3.6.0', '<')) { diff --git a/tests/Operation/ListDatabaseNamesFunctionalTest.php b/tests/Operation/ListDatabaseNamesFunctionalTest.php index 8536fcbea..81d2e83f0 100644 --- a/tests/Operation/ListDatabaseNamesFunctionalTest.php +++ b/tests/Operation/ListDatabaseNamesFunctionalTest.php @@ -48,7 +48,7 @@ function (): void { }, function (array $event): void { $this->assertObjectHasAttribute('authorizedDatabases', $event['started']->getCommand()); - $this->assertSame(true, $event['started']->getCommand()->nameOnly); + $this->assertSame(true, $event['started']->getCommand()->authorizedDatabases); } ); } diff --git a/tests/Operation/ListDatabasesFunctionalTest.php b/tests/Operation/ListDatabasesFunctionalTest.php index 1346a5387..a14d913a1 100644 --- a/tests/Operation/ListDatabasesFunctionalTest.php +++ b/tests/Operation/ListDatabasesFunctionalTest.php @@ -51,6 +51,7 @@ function (): void { }, function (array $event): void { $this->assertObjectHasAttribute('authorizedDatabases', $event['started']->getCommand()); + $this->assertSame(true, $event['started']->getCommand()->authorizedDatabases); } ); }