diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2020-10-26 17:26:31 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-11-09 18:16:52 +0000 |
commit | 5e0d73d0d8e559e34203740af93b6ca03d573ea5 (patch) | |
tree | 3ddc4e4ddde8af5fb575ffe819d0bb4b3acee271 /src/mongo/db/auth/privilege_format.h | |
parent | fa826f6a5b77eb059fe03d411276c3ee7eb303d5 (diff) | |
download | mongo-5e0d73d0d8e559e34203740af93b6ca03d573ea5.tar.gz |
SERVER-51864 IDLify usersInfo and rolesInfo commands
Diffstat (limited to 'src/mongo/db/auth/privilege_format.h')
-rw-r--r-- | src/mongo/db/auth/privilege_format.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/mongo/db/auth/privilege_format.h b/src/mongo/db/auth/privilege_format.h index 1b6ce493c59..e66d5010f38 100644 --- a/src/mongo/db/auth/privilege_format.h +++ b/src/mongo/db/auth/privilege_format.h @@ -29,8 +29,12 @@ #pragma once +#include "mongo/base/string_data.h" +#include "mongo/bson/bsonelement.h" +#include "mongo/bson/bsonobjbuilder.h" namespace mongo { + /** * How user management functions should structure the BSON representation of privileges and roles. */ @@ -40,4 +44,67 @@ enum class PrivilegeFormat { kShowAsUserFragment // Privileges and roles should all be collapsed together, and presented as // a fragment of a user document. }; + +namespace auth { + +/** + * Proxy for PrivilegeFormat to parse into and out of IDL formats. + */ +class ParsedPrivilegeFormat { +public: + static constexpr StringData kAsUserFragment = "asUserFragment"_sd; + + static PrivilegeFormat fromBool(bool fmt) { + return fmt ? PrivilegeFormat::kShowSeparate : PrivilegeFormat::kOmit; + } + + ParsedPrivilegeFormat() : _format(PrivilegeFormat::kOmit) {} + explicit ParsedPrivilegeFormat(bool fmt) : _format(fromBool(fmt)) {} + ParsedPrivilegeFormat(PrivilegeFormat fmt) : _format(fmt) {} + ParsedPrivilegeFormat& operator=(bool fmt) { + _format = fromBool(fmt); + return *this; + } + + PrivilegeFormat operator*() const { + return _format; + } + + static ParsedPrivilegeFormat parseFromBSON(const BSONElement& elem) { + if (elem.eoo()) { + return ParsedPrivilegeFormat(); + } + if (elem.isNumber() || elem.isBoolean()) { + return ParsedPrivilegeFormat(elem.trueValue()); + } + if ((elem.type() == String) && (elem.String() == kAsUserFragment)) { + return ParsedPrivilegeFormat(PrivilegeFormat::kShowAsUserFragment); + } + uasserted(ErrorCodes::BadValue, + str::stream() << "Failed to parse 'showPrivileges'. 'showPrivileges' should " + "either be a boolean or the string 'asUserFragment', given: " + << elem.toString()); + } + + void serializeToBSON(BSONArrayBuilder* bab) const { + if (_format == PrivilegeFormat::kShowAsUserFragment) { + bab->append(kAsUserFragment); + } else { + bab->append(_format == PrivilegeFormat::kShowSeparate); + } + } + + void serializeToBSON(StringData fieldName, BSONObjBuilder* bob) const { + if (_format == PrivilegeFormat::kShowAsUserFragment) { + bob->append(fieldName, kAsUserFragment); + } else { + bob->append(fieldName, _format == PrivilegeFormat::kShowSeparate); + } + } + +private: + PrivilegeFormat _format; +}; + +} // namespace auth } // namespace mongo |