summaryrefslogtreecommitdiff
path: root/src/mongo/db/auth/user.cpp
diff options
context:
space:
mode:
authorSpencer T Brody <spencer@10gen.com>2013-05-16 18:04:07 -0400
committerSpencer T Brody <spencer@10gen.com>2013-05-23 12:09:58 -0400
commitc95bf3ff714f9dad17288e3cb8dcfab7d7ebe6fc (patch)
tree6e0d4b26840c53a5d184abeb41c6e3f85beef917 /src/mongo/db/auth/user.cpp
parent23a185462b83a4f0f7ea5f3ada89ff819313c992 (diff)
downloadmongo-c95bf3ff714f9dad17288e3cb8dcfab7d7ebe6fc.tar.gz
SERVER-9518 Initial implementation of new User class
Diffstat (limited to 'src/mongo/db/auth/user.cpp')
-rw-r--r--src/mongo/db/auth/user.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/mongo/db/auth/user.cpp b/src/mongo/db/auth/user.cpp
new file mode 100644
index 00000000000..b4c87738b9f
--- /dev/null
+++ b/src/mongo/db/auth/user.cpp
@@ -0,0 +1,108 @@
+/* Copyright 2013 10gen Inc.
+
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mongo/db/auth/user.h"
+
+#include <vector>
+
+#include "mongo/db/auth/principal_name.h"
+#include "mongo/db/auth/privilege.h"
+#include "mongo/db/auth/role_name.h"
+#include "mongo/platform/atomic_word.h"
+
+namespace mongo {
+
+ User::User(const UserName& name) : _name(name), _refCount(0), _isValid(1) {}
+ User::~User() {}
+
+ const UserName& User::getName() const {
+ return _name;
+ }
+
+ const RoleNameIterator User::getRoles() const {
+ return RoleNameIterator(new RoleNameSetIterator(_roles.begin(), _roles.end()));
+ }
+
+ bool User::isValid() const {
+ return _isValid.loadRelaxed() == 1;
+ }
+
+ uint32_t User::getRefCount() const {
+ return _refCount;
+ }
+
+ const ActionSet User::getActionsForResource(const std::string& resource) const {
+ unordered_map<string, Privilege>::const_iterator it = _privileges.find(resource);
+ if (it == _privileges.end()) {
+ return ActionSet();
+ }
+ return it->second.getActions();
+ }
+
+ void User::copyFrom(const User& other) {
+ _name = other._name;
+ _privileges = other._privileges;
+ _roles = other._roles;
+ _credentials = other._credentials;
+ _refCount = other._refCount;
+ _isValid= other._isValid;
+ }
+
+ void User::setCredentials(const CredentialData& credentials) {
+ _credentials = credentials;
+ }
+
+ void User::addRole(const RoleName& role) {
+ _roles.insert(role);
+ }
+
+ void User::addRoles(const std::vector<RoleName>& roles) {
+ for (std::vector<RoleName>::const_iterator it = roles.begin(); it != roles.end(); ++it) {
+ _roles.insert(*it);
+ }
+ }
+
+ void User::addPrivilege(const Privilege& privilegeToAdd) {
+ ResourcePrivilegeMap::iterator it = _privileges.find(privilegeToAdd.getResource());
+ if (it == _privileges.end()) {
+ // No privilege exists yet for this resource
+ _privileges.insert(std::make_pair(privilegeToAdd.getResource(), privilegeToAdd));
+ } else {
+ dassert(it->first == privilegeToAdd.getResource());
+ it->second.addActions(privilegeToAdd.getActions());
+ }
+ }
+
+ void User::addPrivileges(const PrivilegeVector& privileges) {
+ for (PrivilegeVector::const_iterator it = privileges.begin();
+ it != privileges.end(); ++it) {
+ addPrivilege(*it);
+ }
+ }
+
+ void User::invalidate() {
+ _isValid.store(0);
+ }
+
+ void User::incrementRefCount() {
+ ++_refCount;
+ }
+
+ void User::decrementRefCount() {
+ dassert(_refCount > 0);
+ --_refCount;
+ }
+} // namespace mongo