Permalink
Browse files

SERVER-7091 Iterator over authenticated principal names in Authorizat…

…ionManager.
  • Loading branch information...
1 parent ea65558 commit bef110071c0d2641670ec55f621ea1d23de13aab @andy10gen andy10gen committed Dec 13, 2012
View
4 src/mongo/db/auth/authorization_manager.cpp
@@ -202,6 +202,10 @@ namespace mongo {
_authenticatedPrincipals.removeByDBName(dbname);
}
+ PrincipalSet::NameIterator AuthorizationManager::getAuthenticatedPrincipalNames() {
+ return _authenticatedPrincipals.getNames();
+ }
+
Status AuthorizationManager::acquirePrivilege(const Privilege& privilege,
const PrincipalName& authorizingPrincipal) {
if (!_authenticatedPrincipals.lookup(authorizingPrincipal)) {
View
3 src/mongo/db/auth/authorization_manager.h
@@ -68,6 +68,9 @@ namespace mongo {
// Ownership of the returned Principal remains with _authenticatedPrincipals
Principal* lookupPrincipal(const PrincipalName& name);
+ // Gets an iterator over the names of all authenticated principals stored in this manager.
+ PrincipalSet::NameIterator getAuthenticatedPrincipalNames();
+
// Removes any authenticated principals whose authorization credentials came from the given
// database, and revokes any privileges that were granted via that principal.
void logoutDatabase(const std::string& dbname);
View
34 src/mongo/db/auth/principal_set.h
@@ -33,6 +33,36 @@ namespace mongo {
class PrincipalSet {
MONGO_DISALLOW_COPYING(PrincipalSet);
public:
+ /**
+ * Forward iterator over the names of the principals stored in a PrincpalSet.
+ *
+ * Instances are valid until the underlying vector<Principal*> is modified.
+ */
+ class NameIterator {
+ public:
+ explicit NameIterator(const std::vector<Principal*>& principals) :
+ _curr(principals.begin()),
+ _end(principals.end()) {
+ }
+
+ NameIterator() {}
+
+ bool more() { return _curr != _end; }
+ const PrincipalName& next() {
+ ++_curr;
+ return get();
+ }
+
+ const PrincipalName& get() const { return (*(_curr - 1))->getName(); }
+
+ const PrincipalName& operator*() const { return get(); }
+ const PrincipalName* operator->() const { return &get(); }
+
+ private:
+ std::vector<Principal*>::const_iterator _curr;
+ std::vector<Principal*>::const_iterator _end;
+ };
+
PrincipalSet();
~PrincipalSet();
@@ -54,6 +84,10 @@ namespace mongo {
// exist. There should be at most one such principal.
Principal* lookupByDBName(const StringData& dbname) const;
+ // Gets an iterator over the names of the principals stored in the set. The iterator is
+ // valid until the next non-const method is called on the PrincipalSet.
+ NameIterator getNames() const { return NameIterator(_principals); }
+
private:
// The PrincipalSet maintains ownership of the Principals in it, and is responsible for
// deleting them when done with them.
View
18 src/mongo/db/auth/principal_set_test.cpp
@@ -27,6 +27,10 @@
namespace mongo {
namespace {
+ static inline std::ostream& operator<<(std::ostream& os, const PrincipalName& pname) {
+ return os << pname.toString();
+ }
+
TEST(PrincipalSetTest, BasicTest) {
PrincipalSet set;
@@ -74,5 +78,19 @@ namespace {
ASSERT_EQUALS(p3, set.lookupByDBName("test2"));
}
+ TEST(PrincipalSetTest, IterateNames) {
+ PrincipalSet pset;
+ PrincipalSet::NameIterator iter = pset.getNames();
+ ASSERT(!iter.more());
+
+ pset.add(new Principal(PrincipalName("bob", "test")));
+
+ iter = pset.getNames();
+ ASSERT(iter.more());
+ ASSERT_EQUALS(iter.next(), PrincipalName("bob", "test"));
+ ASSERT_EQUALS(*iter, PrincipalName("bob", "test"));
+ ASSERT(!iter.more());
+ }
+
} // namespace
} // namespace mongo

0 comments on commit bef1100

Please sign in to comment.