Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://docs.mongodb.com/manual/reference/command/listDatabases/#dbcmd.listDatabases>`_.
`listDatabases command documentation <https://docs.mongodb.com/manual/reference/command/listDatabases/>`_.
For servers < 4.0.5, this option is ignored.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://docs.mongodb.com/manual/reference/command/listCollections/>`_.
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: |
Expand Down
19 changes: 13 additions & 6 deletions src/Command/ListCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
Expand Down Expand Up @@ -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());
Expand Down
14 changes: 4 additions & 10 deletions src/Command/ListDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions src/Operation/ListCollectionNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Operation/ListCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/Command/ListCollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function provideInvalidConstructorOptions()
{
$options = [];

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['authorizedCollections' => $value];
}

foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['filter' => $value];
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Operation/ListCollectionNamesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<')) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Operation/ListCollectionsFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<')) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/ListDatabaseNamesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Operation/ListDatabasesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function (): void {
},
function (array $event): void {
$this->assertObjectHasAttribute('authorizedDatabases', $event['started']->getCommand());
$this->assertSame(true, $event['started']->getCommand()->authorizedDatabases);
}
);
}
Expand Down