diff options
Diffstat (limited to 'src/mongo/shell/mongo.js')
-rw-r--r-- | src/mongo/shell/mongo.js | 103 |
1 files changed, 53 insertions, 50 deletions
diff --git a/src/mongo/shell/mongo.js b/src/mongo/shell/mongo.js index 4b3f95db88d..2f25605536d 100644 --- a/src/mongo/shell/mongo.js +++ b/src/mongo/shell/mongo.js @@ -89,63 +89,66 @@ Mongo.prototype.getDBs = function(driverSession = this._getDefaultSession(), filter = undefined, nameOnly = undefined, authorizedDatabases = undefined) { - 'use strict'; - let cmdObj = {listDatabases: 1}; - if (filter !== undefined) { - cmdObj.filter = filter; - } - if (nameOnly !== undefined) { - cmdObj.nameOnly = nameOnly; - } - if (authorizedDatabases !== undefined) { - cmdObj.authorizedDatabases = authorizedDatabases; - } + return function(driverSession, filter, nameOnly, authorizedDatabases) { + 'use strict'; - if (driverSession._isExplicit || !jsTest.options().disableImplicitSessions) { - cmdObj = driverSession._serverSession.injectSessionId(cmdObj); - } + let cmdObj = {listDatabases: 1}; + if (filter !== undefined) { + cmdObj.filter = filter; + } + if (nameOnly !== undefined) { + cmdObj.nameOnly = nameOnly; + } + if (authorizedDatabases !== undefined) { + cmdObj.authorizedDatabases = authorizedDatabases; + } - const res = this.adminCommand(cmdObj); - if (!res.ok) { - // If "Unauthorized" was returned by the back end and we haven't explicitly - // asked for anything difficult to provide from userspace, then we can - // fallback on inspecting the user's permissions. - // This means that: - // * filter must be undefined, as reimplementing that logic is out of scope. - // * nameOnly must not be false as we can't infer size information. - // * authorizedDatabases must not be false as those are the only DBs we can infer. - // Note that if the above are valid and we get Unauthorized, that also means - // that we MUST be talking to a pre-4.0 mongod. - // - // Like the server response mode, this path will return a simple list of - // names if nameOnly is specified as true. - // If nameOnly is undefined, we come as close as we can to what the - // server would return by supplying the databases key of the returned - // object. Other information is unavailable. - if ((res.code === ErrorCodes.Unauthorized) && (filter === undefined) && - (nameOnly !== false) && (authorizedDatabases !== false)) { - const names = this._getDatabaseNamesFromPrivileges(); - if (nameOnly === true) { - return names; - } else { - return { - databases: names.map(function(x) { - return {name: x}; - }), - }; + if (driverSession._isExplicit || !jsTest.options().disableImplicitSessions) { + cmdObj = driverSession._serverSession.injectSessionId(cmdObj); + } + + const res = this.adminCommand(cmdObj); + if (!res.ok) { + // If "Unauthorized" was returned by the back end and we haven't explicitly + // asked for anything difficult to provide from userspace, then we can + // fallback on inspecting the user's permissions. + // This means that: + // * filter must be undefined, as reimplementing that logic is out of scope. + // * nameOnly must not be false as we can't infer size information. + // * authorizedDatabases must not be false as those are the only DBs we can infer. + // Note that if the above are valid and we get Unauthorized, that also means + // that we MUST be talking to a pre-4.0 mongod. + // + // Like the server response mode, this path will return a simple list of + // names if nameOnly is specified as true. + // If nameOnly is undefined, we come as close as we can to what the + // server would return by supplying the databases key of the returned + // object. Other information is unavailable. + if ((res.code === ErrorCodes.Unauthorized) && (filter === undefined) && + (nameOnly !== false) && (authorizedDatabases !== false)) { + const names = this._getDatabaseNamesFromPrivileges(); + if (nameOnly === true) { + return names; + } else { + return { + databases: names.map(function(x) { + return {name: x}; + }), + }; + } } + throw _getErrorWithCode(res, "listDatabases failed:" + tojson(res)); } - throw _getErrorWithCode(res, "listDatabases failed:" + tojson(res)); - } - if (nameOnly) { - return res.databases.map(function(db) { - return db.name; - }); - } + if (nameOnly) { + return res.databases.map(function(db) { + return db.name; + }); + } - return res; + return res; + }.call(this, driverSession, filter, nameOnly, authorizedDatabases); }; Mongo.prototype.adminCommand = function(cmd) { |