From c9c2e1401bae1938fe392f6ee0903939b63b050c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 30 Jun 2009 22:57:29 +0200 Subject: =?UTF-8?q?UserManager=20erweitert=20Erm=C3=B6gliche=20Caching=20d?= =?UTF-8?q?er=20User-Group-Zuordnungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Backends/NetworkUserBackend.cpp | 20 ++++++++++ src/Common/Backends/NetworkUserBackend.h | 2 + src/Common/UserBackend.h | 4 ++ src/Common/UserCache.cpp | 59 ++++++++++++++++++++++++++++-- src/Common/UserCache.h | 8 ++-- src/Common/UserManager.cpp | 18 +++++++++ src/Common/UserManager.h | 2 + 7 files changed, 107 insertions(+), 6 deletions(-) (limited to 'src/Common') 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 > NetworkUserBackend::getGroupUs return userList; } +boost::shared_ptr > NetworkUserBackend::getFullUserGroupList() throw(Core::Exception) { + boost::shared_ptr request(new SimpleUserRequest(application, "GetFullUserGroupList")); + application->getRequestManager()->sendRequest(connection, request); + request->wait(); + + std::pair, Core::Exception> result = request->getResult(); + if(!result.first || result.second) + throw result.second; + + boost::shared_ptr > ret(new std::multimap); + + 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("uid"), entry->get("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 getGroupInfoByName(const std::string &name) throw(Core::Exception); virtual boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Core::Exception); + virtual boost::shared_ptr > 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 > 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 UserCache::getUserInfoByName(const std::string } boost::shared_ptr > UserCache::getUserGroupList(unsigned long uid) throw(Core::Exception) { - return backend->getUserGroupList(uid); + getFullUserGroupList(); + + boost::lock_guard lock(mutex); + + if(!userGroups) + throw userGroupException; + + std::pair::const_iterator, std::multimap::const_iterator> range = userGroups->equal_range(uid); + + boost::shared_ptr > groups(new std::set); + + for(std::multimap::const_iterator group = range.first; group != range.second; ++group) + groups->insert(group->second); + + return groups; } @@ -145,8 +159,47 @@ boost::shared_ptr UserCache::getGroupInfoByName(const std::stri return boost::shared_ptr(new GroupInfo(group->second)); } -boost::shared_ptr > UserCache::getGroupUserList(unsigned long uid) throw(Core::Exception) { - return backend->getGroupUserList(uid); +boost::shared_ptr > UserCache::getGroupUserList(unsigned long gid) throw(Core::Exception) { + getFullUserGroupList(); + + boost::lock_guard lock(mutex); + + if(!userGroups) + throw userGroupException; + + std::pair::iterator, std::multimap::iterator> range = groupUsers->equal_range(gid); + + boost::shared_ptr > users(new std::set); + + for(std::multimap::iterator user = range.first; user != range.second; ++user) + users->insert(user->second); + + return users; +} + +boost::shared_ptr > UserCache::getFullUserGroupList() throw(Core::Exception) { + boost::lock_guard 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); + for(std::multimap::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 > groupNames; Core::Exception groupException; - /*boost::shared_ptr > userGroups; - boost::shared_ptr > groupUsers; - Core::Exception userGroupException;*/ + boost::shared_ptr > userGroups; + boost::shared_ptr > groupUsers; + Core::Exception userGroupException; protected: virtual boost::shared_ptr > getUserList() throw(Core::Exception); @@ -62,6 +62,8 @@ class UserCache : public UserBackend, private boost::noncopyable { virtual boost::shared_ptr getGroupInfoByName(const std::string &name) throw(Core::Exception); virtual boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Core::Exception); + virtual boost::shared_ptr > 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 > UserManager::getGroupUserList( throw e; } +boost::shared_ptr > UserManager::getFullUserGroupList() throw(Core::Exception) { + Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + + boost::lock_guard 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 getGroupInfoByName(const std::string &name) throw(Core::Exception); boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Core::Exception); + boost::shared_ptr > getFullUserGroupList() throw(Core::Exception); + void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception); void addUser(const UserInfo &userInfo) throw(Core::Exception); -- cgit v1.2.3