summaryrefslogtreecommitdiffstats
path: root/src/Common/UserManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common/UserManager.cpp')
-rw-r--r--src/Common/UserManager.cpp246
1 files changed, 43 insertions, 203 deletions
diff --git a/src/Common/UserManager.cpp b/src/Common/UserManager.cpp
index 342d4bf..253466e 100644
--- a/src/Common/UserManager.cpp
+++ b/src/Common/UserManager.cpp
@@ -18,7 +18,7 @@
*/
#include "UserManager.h"
-#include "UserBackend.h"
+#include "UserCache.h"
#include <iostream>
@@ -33,94 +33,40 @@ bool UserManager::Compare::operator() (boost::shared_ptr<UserBackend> b1, boost:
}
-boost::shared_ptr<const std::map<unsigned long, UserInfo> > UserManager::getUserList() throw(Core::Exception) {
+void UserManager::registerBackendCached(boost::shared_ptr<UserBackend> backend) {
{
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(users)
- return users;
- else if(userException)
- throw *userException;
- }
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+ backends.insert(std::make_pair(backend, boost::shared_ptr<UserCache>(new UserCache(backend))));
}
+}
- Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::shared_ptr<const std::map<unsigned long, UserInfo> > ret;
+boost::shared_ptr<const std::map<unsigned long, UserInfo> > UserManager::getUserList() throw(Core::Exception) {
+ Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- {
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
- try {
- ret = (*backend)->getUserList();
- break;
- }
- catch(Core::Exception e2) {
- if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
- e = e2;
- }
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ try {
+ return backend->second->getUserList();
}
- }
-
- {
- boost::upgrade_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(ret) {
- boost::upgrade_to_unique_lock<boost::shared_mutex> upgradeLock(lock);
- users = ret;
-
- boost::shared_ptr<std::map<std::string, unsigned long> > names(new std::map<std::string, unsigned long>);
- for(std::map<unsigned long, UserInfo>::const_iterator user = users->begin(); user != users->end(); ++user)
- names->insert(std::make_pair(user->second.getUsername(), user->first));
-
- userNames = names;
- }
- else
- userException.reset(new Core::Exception(e));
+ catch(Core::Exception e2) {
+ if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
+ e = e2;
}
}
- if(ret)
- return ret;
- else
- throw e;
+ throw e;
}
boost::shared_ptr<const UserInfo> UserManager::getUserInfo(unsigned long uid) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!users && !userException) {
- lock.unlock();
- getUserList();
- lock.lock();
- }
-
- if(caching) {
- if(users) {
- std::map<unsigned long, UserInfo>::const_iterator user = users->find(uid);
- if(user != users->end())
- return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(userException)
- throw *userException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getUserInfo(uid);
+ return backend->second->getUserInfo(uid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -132,37 +78,13 @@ boost::shared_ptr<const UserInfo> UserManager::getUserInfo(unsigned long uid) th
}
boost::shared_ptr<const UserInfo> UserManager::getUserInfoByName(const std::string &name) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!users && !userException) {
- lock.unlock();
- getUserList();
- lock.lock();
- }
-
- if(caching) {
- if(userNames) {
- std::map<std::string, unsigned long>::const_iterator user = userNames->find(name);
- if(user != userNames->end())
- return boost::shared_ptr<UserInfo>(new UserInfo(users->find(user->second)->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(userException)
- throw *userException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getUserInfoByName(name);
+ return backend->second->getUserInfoByName(name);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -176,11 +98,11 @@ boost::shared_ptr<const UserInfo> UserManager::getUserInfoByName(const std::stri
boost::shared_ptr<const std::set<unsigned long> > UserManager::getUserGroupList(unsigned long uid) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getUserGroupList(uid);
+ return backend->second->getUserGroupList(uid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -192,28 +114,16 @@ boost::shared_ptr<const std::set<unsigned long> > UserManager::getUserGroupList(
}
boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserManager::getGroupList() throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(groups)
- return groups;
- else if(groupException)
- throw *groupException;
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
boost::shared_ptr<const std::map<unsigned long, GroupInfo> > ret;
{
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- ret = (*backend)->getGroupList();
- break;
+ return backend->second->getGroupList();
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -222,63 +132,17 @@ boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserManager::getGro
}
}
- {
- boost::upgrade_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(ret) {
- boost::upgrade_to_unique_lock<boost::shared_mutex> upgradeLock(lock);
- groups = ret;
-
- boost::shared_ptr<std::map<std::string, unsigned long> > names(new std::map<std::string, unsigned long>);
- for(std::map<unsigned long, GroupInfo>::const_iterator group = groups->begin(); group != groups->end(); ++group)
- names->insert(std::make_pair(group->second.getName(), group->first));
-
- groupNames = names;
- }
- else
- groupException.reset(new Core::Exception(e));
- }
- }
-
- if(ret)
- return ret;
- else
- throw e;
+ throw e;
}
boost::shared_ptr<const GroupInfo> UserManager::getGroupInfo(unsigned long gid) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!groups && !groupException) {
- lock.unlock();
- getGroupList();
- lock.lock();
- }
-
- if(caching) {
- if(groups) {
- std::map<unsigned long, GroupInfo>::const_iterator group = groups->find(gid);
- if(group != groups->end())
- return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(groupException)
- throw *groupException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getGroupInfo(gid);
+ return backend->second->getGroupInfo(gid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -290,37 +154,13 @@ boost::shared_ptr<const GroupInfo> UserManager::getGroupInfo(unsigned long gid)
}
boost::shared_ptr<const GroupInfo> UserManager::getGroupInfoByName(const std::string &name) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!groups && !groupException) {
- lock.unlock();
- getGroupList();
- lock.lock();
- }
-
- if(caching) {
- if(groupNames) {
- std::map<std::string, unsigned long>::const_iterator group = groupNames->find(name);
- if(group != groupNames->end())
- return boost::shared_ptr<GroupInfo>(new GroupInfo(groups->find(group->second)->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(groupException)
- throw *groupException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getGroupInfoByName(name);
+ return backend->second->getGroupInfoByName(name);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -334,11 +174,11 @@ boost::shared_ptr<const GroupInfo> UserManager::getGroupInfoByName(const std::st
boost::shared_ptr<const std::set<unsigned long> > UserManager::getGroupUserList(unsigned long gid) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getGroupUserList(gid);
+ return backend->second->getGroupUserList(gid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -352,11 +192,11 @@ boost::shared_ptr<const std::set<unsigned long> > UserManager::getGroupUserList(
void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- (*backend)->setPassword(uid, password);
+ backend->second->setPassword(uid, password);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -370,11 +210,11 @@ void UserManager::setPassword(unsigned long uid, const std::string &password) th
void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- (*backend)->addUser(userInfo);
+ backend->second->addUser(userInfo);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)