diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-06-30 22:57:29 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-06-30 22:57:29 +0200 |
commit | c9c2e1401bae1938fe392f6ee0903939b63b050c (patch) | |
tree | 84ed6f8fa3e46e8bd8cd45558adb5b539cd2a06c /src/Common | |
parent | 68ec8067feec09a6fecf8bf2fbffb5d757db3d4a (diff) | |
download | mad-c9c2e1401bae1938fe392f6ee0903939b63b050c.tar mad-c9c2e1401bae1938fe392f6ee0903939b63b050c.zip |
UserManager erweitert
Ermögliche Caching der User-Group-Zuordnungen
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/Backends/NetworkUserBackend.cpp | 20 | ||||
-rw-r--r-- | src/Common/Backends/NetworkUserBackend.h | 2 | ||||
-rw-r--r-- | src/Common/UserBackend.h | 4 | ||||
-rw-r--r-- | src/Common/UserCache.cpp | 59 | ||||
-rw-r--r-- | src/Common/UserCache.h | 8 | ||||
-rw-r--r-- | src/Common/UserManager.cpp | 18 | ||||
-rw-r--r-- | src/Common/UserManager.h | 2 |
7 files changed, 107 insertions, 6 deletions
diff --git a/src/Common/Backends/NetworkUserBackend.cpp b/src/Common/Backends/NetworkUserBackend.cpp index 81fc94a..f75783c 100644 --- a/src/Common/Backends/NetworkUserBackend.cpp +++ b/src/Common/Backends/NetworkUserBackend.cpp @@ -188,6 +188,26 @@ boost::shared_ptr<const std::set<unsigned long> > NetworkUserBackend::getGroupUs return userList; } +boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > NetworkUserBackend::getFullUserGroupList() throw(Core::Exception) { + boost::shared_ptr<SimpleUserRequest> request(new SimpleUserRequest(application, "GetFullUserGroupList")); + application->getRequestManager()->sendRequest(connection, request); + request->wait(); + + std::pair<boost::shared_ptr<const XmlPacket>, Core::Exception> result = request->getResult(); + if(!result.first || result.second) + throw result.second; + + boost::shared_ptr<std::multimap<unsigned long, unsigned long> > ret(new std::multimap<unsigned long, unsigned long>); + + const XmlPacket::List *list = result.first->getList("userGroupList"); + if(list) { + for(XmlPacket::List::const_iterator entry = list->begin(); entry != list->end(); ++entry) + ret->insert(std::make_pair(entry->get<unsigned long>("uid"), entry->get<unsigned long>("gid"))); + } + + return ret; +} + /*void NetworkUserBackend::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { diff --git a/src/Common/Backends/NetworkUserBackend.h b/src/Common/Backends/NetworkUserBackend.h index c569ab6..66be909 100644 --- a/src/Common/Backends/NetworkUserBackend.h +++ b/src/Common/Backends/NetworkUserBackend.h @@ -77,6 +77,8 @@ class NetworkUserBackend : public UserBackend { virtual boost::shared_ptr<const GroupInfo> getGroupInfoByName(const std::string &name) throw(Core::Exception); virtual boost::shared_ptr<const std::set<unsigned long> > getGroupUserList(unsigned long gid) throw(Core::Exception); + virtual boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > getFullUserGroupList() throw(Core::Exception); + //virtual void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception); //virtual void addUser(const UserInfo &userInfo) throw(Core::Exception); diff --git a/src/Common/UserBackend.h b/src/Common/UserBackend.h index 9983ee0..829aae7 100644 --- a/src/Common/UserBackend.h +++ b/src/Common/UserBackend.h @@ -80,6 +80,10 @@ class UserBackend { throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } + virtual boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > getFullUserGroupList() throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + virtual void setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) throw(Core::Exception) { throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } diff --git a/src/Common/UserCache.cpp b/src/Common/UserCache.cpp index 63f72dd..79f27c7 100644 --- a/src/Common/UserCache.cpp +++ b/src/Common/UserCache.cpp @@ -83,7 +83,21 @@ boost::shared_ptr<const UserInfo> UserCache::getUserInfoByName(const std::string } boost::shared_ptr<const std::set<unsigned long> > UserCache::getUserGroupList(unsigned long uid) throw(Core::Exception) { - return backend->getUserGroupList(uid); + getFullUserGroupList(); + + boost::lock_guard<boost::shared_mutex> lock(mutex); + + if(!userGroups) + throw userGroupException; + + std::pair<std::multimap<unsigned long, unsigned long>::const_iterator, std::multimap<unsigned long, unsigned long>::const_iterator> range = userGroups->equal_range(uid); + + boost::shared_ptr<std::set<unsigned long> > groups(new std::set<unsigned long>); + + for(std::multimap<unsigned long, unsigned long>::const_iterator group = range.first; group != range.second; ++group) + groups->insert(group->second); + + return groups; } @@ -145,8 +159,47 @@ boost::shared_ptr<const GroupInfo> UserCache::getGroupInfoByName(const std::stri return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second)); } -boost::shared_ptr<const std::set<unsigned long> > UserCache::getGroupUserList(unsigned long uid) throw(Core::Exception) { - return backend->getGroupUserList(uid); +boost::shared_ptr<const std::set<unsigned long> > UserCache::getGroupUserList(unsigned long gid) throw(Core::Exception) { + getFullUserGroupList(); + + boost::lock_guard<boost::shared_mutex> lock(mutex); + + if(!userGroups) + throw userGroupException; + + std::pair<std::multimap<unsigned long, unsigned long>::iterator, std::multimap<unsigned long, unsigned long>::iterator> range = groupUsers->equal_range(gid); + + boost::shared_ptr<std::set<unsigned long> > users(new std::set<unsigned long>); + + for(std::multimap<unsigned long, unsigned long>::iterator user = range.first; user != range.second; ++user) + users->insert(user->second); + + return users; +} + +boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > UserCache::getFullUserGroupList() throw(Core::Exception) { + boost::lock_guard<boost::shared_mutex> lock(mutex); + + if(userGroups) { + std::cerr << "Cached" << std::endl; + return userGroups; + } + else if(userGroupException) + throw userGroupException; + + try { + userGroups = backend->getFullUserGroupList(); + + groupUsers.reset(new std::multimap<unsigned long, unsigned long>); + for(std::multimap<unsigned long, unsigned long>::const_iterator usergroup = userGroups->begin(); usergroup != userGroups->end(); ++usergroup) + groupUsers->insert(std::make_pair(usergroup->second, usergroup->first)); + + return userGroups; + } + catch(Core::Exception e) { + userGroupException = e; + throw userGroupException; + } } } diff --git a/src/Common/UserCache.h b/src/Common/UserCache.h index 1e432fd..cec5ea5 100644 --- a/src/Common/UserCache.h +++ b/src/Common/UserCache.h @@ -47,9 +47,9 @@ class UserCache : public UserBackend, private boost::noncopyable { boost::shared_ptr<std::map<std::string, unsigned long> > groupNames; Core::Exception groupException; - /*boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > userGroups; - boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > groupUsers; - Core::Exception userGroupException;*/ + boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > userGroups; + boost::shared_ptr<std::multimap<unsigned long, unsigned long> > groupUsers; + Core::Exception userGroupException; protected: virtual boost::shared_ptr<const std::map<unsigned long, UserInfo> > getUserList() throw(Core::Exception); @@ -62,6 +62,8 @@ class UserCache : public UserBackend, private boost::noncopyable { virtual boost::shared_ptr<const GroupInfo> getGroupInfoByName(const std::string &name) throw(Core::Exception); virtual boost::shared_ptr<const std::set<unsigned long> > getGroupUserList(unsigned long gid) throw(Core::Exception); + virtual boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > getFullUserGroupList() throw(Core::Exception); + virtual void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { backend->setPassword(uid, password); } diff --git a/src/Common/UserManager.cpp b/src/Common/UserManager.cpp index 253466e..c56195d 100644 --- a/src/Common/UserManager.cpp +++ b/src/Common/UserManager.cpp @@ -189,6 +189,24 @@ boost::shared_ptr<const std::set<unsigned long> > UserManager::getGroupUserList( throw e; } +boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > UserManager::getFullUserGroupList() throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + + boost::lock_guard<boost::shared_mutex> lock(mutex); + + for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + try { + return backend->second->getFullUserGroupList(); + } + catch(Core::Exception e2) { + if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) + e = e2; + } + } + + throw e; +} + void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { Core::Exception e(Core::Exception::NOT_IMPLEMENTED); diff --git a/src/Common/UserManager.h b/src/Common/UserManager.h index 7413970..1ba051f 100644 --- a/src/Common/UserManager.h +++ b/src/Common/UserManager.h @@ -77,6 +77,8 @@ class UserManager : boost::noncopyable { boost::shared_ptr<const GroupInfo> getGroupInfoByName(const std::string &name) throw(Core::Exception); boost::shared_ptr<const std::set<unsigned long> > getGroupUserList(unsigned long gid) throw(Core::Exception); + boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > getFullUserGroupList() throw(Core::Exception); + void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception); void addUser(const UserInfo &userInfo) throw(Core::Exception); |