Permalink
Browse files

Use PrincipalName::getDB() to find database name in introspect.cpp

  • Loading branch information...
1 parent a7169fd commit a615d7dd1e3201e03d8fc8ed2f6a31d73e192cc1 Andrew Schwerin committed Dec 22, 2012
Showing with 16 additions and 19 deletions.
  1. +6 −4 jstests/auth/profile.js
  2. +10 −15 src/mongo/db/introspect.cpp
View
10 jstests/auth/profile.js
@@ -32,7 +32,8 @@ db1.foo.findOne();
var last = lastOp(db1);
assert.eq(principalName(username, db1), last.user);
assert.eq(1, last.allUsers.length);
-assert.eq(principalName(username, db1), last.allUsers[0]);
+assert.eq(username, last.allUsers[0].user);
+assert.eq(db1, last.allUsers[0].userSource);
db2.auth(username, "password");
@@ -41,9 +42,10 @@ var last = lastOp(db1);
// Which user gets put in "user" and the ordering of users in "allUsers" is undefined.
assert((principalName(username, db1) == last.user) || (principalName(username, db2) == last.user));
assert.eq(2, last.allUsers.length);
-assert.gte(last.allUsers.indexOf(principalName(username, db1)), 0);
-assert.gte(last.allUsers.indexOf(principalName(username, db2)), 0);
-
+assert.eq(username, last.allUsers[0].user);
+assert.eq(username, last.allUsers[1].user);
+assert((db1 == last.allUsers[0].userSource && db2 == last.allUsers[1].userSource) ||
+ (db2 == last.allUsers[0].userSource && db1 == last.allUsers[1].userSource));
db1.setProfilingLevel(0);
db1.dropDatabase();
View
25 src/mongo/db/introspect.cpp
@@ -38,31 +38,26 @@ namespace {
void _appendUserInfo(const Client& c, BSONObjBuilder& builder, AuthorizationManager* authManager) {
PrincipalSet::NameIterator nameIter = authManager->getAuthenticatedPrincipalNames();
- string bestUser;
+ PrincipalName bestUser;
+ if (nameIter.more())
+ bestUser = *nameIter;
StringData opdb( nsToDatabaseSubstring( c.ns() ) );
BSONArrayBuilder allUsers(builder.subarrayStart("allUsers"));
for ( ; nameIter.more(); nameIter.next()) {
- const string& name = nameIter->getFullName();
- allUsers.append(name);
+ BSONObjBuilder nextUser(allUsers.subobjStart());
+ nextUser.append(AuthorizationManager::USER_NAME_FIELD_NAME, nameIter->getUser());
+ nextUser.append(AuthorizationManager::USER_SOURCE_FIELD_NAME, nameIter->getDB());
+ nextUser.doneFast();
- if ( bestUser.size() == 0 ) {
- bestUser = name;
- }
- else {
- size_t idx = name.find( '@' );
- if ( idx >= 0 ) {
- StringData db = StringData(name).substr( idx + 1 );
- log() << "eliot : " << db << " " << opdb << endl;
- if ( opdb == db )
- bestUser = name;
- }
+ if (nameIter->getDB() == opdb) {
+ bestUser = *nameIter;
}
}
allUsers.doneFast();
- builder.append("user", bestUser );
+ builder.append("user", bestUser.getUser().empty() ? "" : bestUser.getFullName());
}
} // namespace

0 comments on commit a615d7d

Please sign in to comment.