Skip to content

Commit 76e51b2

Browse files
committed
feat(NODE-3728): Allow to pass authorizedCollections option to the db.listCollections method
1 parent fa2f8d0 commit 76e51b2

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/operations/list_collections.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const LIST_COLLECTIONS_WIRE_VERSION = 3;
1515
export interface ListCollectionsOptions extends CommandOperationOptions {
1616
/** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */
1717
nameOnly?: boolean;
18+
/** Since 4.0: If true and nameOnly is true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. */
19+
authorizedCollections?: boolean;
1820
/** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */
1921
batchSize?: number;
2022
}
@@ -25,6 +27,7 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
2527
db: Db;
2628
filter: Document;
2729
nameOnly: boolean;
30+
authorizedCollections: boolean;
2831
batchSize?: number;
2932

3033
constructor(db: Db, filter: Document, options?: ListCollectionsOptions) {
@@ -34,6 +37,7 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
3437
this.db = db;
3538
this.filter = filter;
3639
this.nameOnly = !!this.options.nameOnly;
40+
this.authorizedCollections = !!this.options.authorizedCollections;
3741

3842
if (typeof this.options.batchSize === 'number') {
3943
this.batchSize = this.options.batchSize;
@@ -94,7 +98,8 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
9498
listCollections: 1,
9599
filter: this.filter,
96100
cursor: this.batchSize ? { batchSize: this.batchSize } : {},
97-
nameOnly: this.nameOnly
101+
nameOnly: this.nameOnly,
102+
authorizedCollections: this.authorizedCollections
98103
};
99104

100105
return super.executeCommand(server, session, command, callback);

test/functional/unit_db_list_collections.test.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,27 @@ describe('db.listCollections', function () {
3434
{
3535
description: 'should always send nameOnly option, defaulting to false',
3636
command: db => db.listCollections().toArray(() => {}),
37-
listCollectionsValue: false
37+
listCollectionsOptions: { nameOnly: false }
3838
},
3939
{
4040
description: 'should propagate the nameOnly option',
4141
command: db => db.listCollections({}, { nameOnly: true }).toArray(() => {}),
42-
listCollectionsValue: true
42+
listCollectionsOptions: { nameOnly: true }
43+
},
44+
{
45+
description: 'should always send authorizedCollections option, defaulting to false',
46+
command: db => db.listCollections().toArray(() => {}),
47+
listCollectionsOptions: { authorizedCollections: false }
48+
},
49+
{
50+
description: 'should propagate the authorizedCollections option',
51+
command: db => db.listCollections({}, { authorizedCollections: true }).toArray(() => {}),
52+
listCollectionsOptions: { authorizedCollections: true }
4353
},
4454
{
4555
description: 'should send nameOnly: true for db.collections',
4656
command: db => db.collections(() => {}),
47-
listCollectionsValue: true
57+
listCollectionsOptions: { nameOnly: true }
4858
}
4959
].forEach(config => {
5060
function testFn(done) {
@@ -59,7 +69,9 @@ describe('db.listCollections', function () {
5969
client.on('commandStarted', e => {
6070
if (e.commandName === 'listCollections') {
6171
try {
62-
expect(e).to.have.nested.property('command.nameOnly', config.listCollectionsValue);
72+
for (const [name, value] of Object.entries(config.listCollectionsOptions)) {
73+
expect(e).to.have.nested.property(`command.${name}`, value);
74+
}
6375
client.close(done);
6476
} catch (err) {
6577
client.close(() => done(err));

0 commit comments

Comments
 (0)