From caeb73a2cdf6db24490bbe3657372fb1ba52b78e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 15 Jul 2009 23:11:54 +0200 Subject: =?UTF-8?q?UserManager=20=C3=BCberarbeitet=20UserConfigBackend=20e?= =?UTF-8?q?ingef=C3=BChrt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Backends/NetworkUserBackend.cpp | 2 + src/Common/Backends/NetworkUserBackend.h | 10 +- src/Common/CMakeLists.txt | 3 +- src/Common/UserBackend.h | 135 --------- src/Common/UserCache.h | 13 +- src/Common/UserConfigBackend.h | 54 ++++ src/Common/UserDBBackend.h | 137 +++++++++ src/Common/UserManager.cpp | 351 ++++++---------------- src/Common/UserManager.h | 51 +++- src/modules/UserBackendMysql/Module.h | 2 +- src/modules/UserBackendMysql/UserBackendMysql.cpp | 1 + src/modules/UserBackendMysql/UserBackendMysql.h | 12 +- 12 files changed, 361 insertions(+), 410 deletions(-) delete mode 100644 src/Common/UserBackend.h create mode 100644 src/Common/UserConfigBackend.h create mode 100644 src/Common/UserDBBackend.h diff --git a/src/Common/Backends/NetworkUserBackend.cpp b/src/Common/Backends/NetworkUserBackend.cpp index 6296581..39f4e5b 100644 --- a/src/Common/Backends/NetworkUserBackend.cpp +++ b/src/Common/Backends/NetworkUserBackend.cpp @@ -26,6 +26,8 @@ namespace Mad { namespace Common { namespace Backends { +const std::string NetworkUserBackend::name("NetworkUserBackend"); + void NetworkUserBackend::SimpleUserRequest::sendRequest() { XmlPacket packet; packet.setType(type); diff --git a/src/Common/Backends/NetworkUserBackend.h b/src/Common/Backends/NetworkUserBackend.h index 0567514..6c0e94d 100644 --- a/src/Common/Backends/NetworkUserBackend.h +++ b/src/Common/Backends/NetworkUserBackend.h @@ -20,14 +20,14 @@ #ifndef MAD_COMMON_BACKENDS_NETWORKUSERBACKEND_H_ #define MAD_COMMON_BACKENDS_NETWORKUSERBACKEND_H_ -#include "../UserBackend.h" +#include "../UserDBBackend.h" #include "../Request.h" namespace Mad { namespace Common { namespace Backends { -class NetworkUserBackend : public UserBackend { +class NetworkUserBackend : public UserDBBackend { private: class SimpleUserRequest : public Request { private: @@ -110,6 +110,8 @@ class NetworkUserBackend : public UserBackend { : Request(application), type(type0), uid(uid0), gid(gid0) {} }; + static const std::string name; + Application *application; Connection *connection; @@ -137,6 +139,10 @@ class NetworkUserBackend : public UserBackend { public: NetworkUserBackend(Application *application0, Connection *connection0) : application(application0), connection(connection0) {} + + virtual const std::string& getName() { + return name; + } }; } diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index 1ac7e69..e8e3152 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -20,7 +20,8 @@ add_library(Common RequestManager.cpp RequestManager.h SystemBackend.h SystemManager.cpp SystemManager.h - UserBackend.h + UserConfigBackend.h + UserDBBackend.h UserCache.cpp UserCache.h UserInfo.h UserManager.cpp UserManager.h diff --git a/src/Common/UserBackend.h b/src/Common/UserBackend.h deleted file mode 100644 index 9eb59d3..0000000 --- a/src/Common/UserBackend.h +++ /dev/null @@ -1,135 +0,0 @@ -/* - * UserBackend.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_SERVER_USERBACKEND_H_ -#define MAD_SERVER_USERBACKEND_H_ - -#include - -#include - -#include "UserInfo.h" -#include "GroupInfo.h" - -#include -#include -#include - -#include -#include - - -namespace Mad { -namespace Common { - -class UserCache; -class UserManager; - -class UserBackend { - protected: - friend class UserCache; - friend class UserManager; - - UserBackend() {} - - virtual boost::shared_ptr > getUserList(boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual boost::shared_ptr getUserInfo(unsigned long uid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual boost::shared_ptr getUserInfoByName(const std::string &name _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual boost::shared_ptr > getUserGroupList(unsigned long uid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - - virtual boost::shared_ptr > getGroupList(boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual boost::shared_ptr getGroupInfo(unsigned long gid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual boost::shared_ptr getGroupInfoByName(const std::string &name _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual boost::shared_ptr > getGroupUserList(unsigned long gid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual boost::shared_ptr > getFullUserGroupList(boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) 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)); - } - - virtual void addUser(const UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual void updateUser(unsigned long uid _UNUSED_PARAMETER_, const UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual void deleteUser(unsigned long uid _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual void addGroup(const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual void updateGroup(unsigned long gid _UNUSED_PARAMETER_, const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual void deleteGroup(unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual void addUserToGroup(unsigned long uid _UNUSED_PARAMETER_, unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - virtual void deleteUserFromGroup(unsigned long uid _UNUSED_PARAMETER_, unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - } - - public: - virtual ~UserBackend() {} - - virtual int getPriority() const { - return 0; - } -}; - -} -} - -#endif /* MAD_SERVER_USERBACKEND_H_ */ diff --git a/src/Common/UserCache.h b/src/Common/UserCache.h index 1425908..ff70520 100644 --- a/src/Common/UserCache.h +++ b/src/Common/UserCache.h @@ -20,7 +20,7 @@ #ifndef MAD_COMMON_USERCACHE_H_ #define MAD_COMMON_USERCACHE_H_ -#include "UserBackend.h" +#include "UserDBBackend.h" #include @@ -32,13 +32,13 @@ namespace Common { class Application; class UserManager; -class UserCache : public UserBackend, private boost::noncopyable { +class UserCache : public UserDBBackend, private boost::noncopyable { private: friend class UserManager; Application *application; - boost::shared_ptr backend; + boost::shared_ptr backend; boost::recursive_mutex mutex; @@ -106,9 +106,14 @@ class UserCache : public UserBackend, private boost::noncopyable { backend->deleteUserFromGroup(uid, gid); } - UserCache(Application *application0, boost::shared_ptr backend0) : application(application0), backend(backend0), + UserCache(Application *application0, boost::shared_ptr backend0) : application(application0), backend(backend0), userTime(boost::posix_time::not_a_date_time), groupTime(boost::posix_time::not_a_date_time), userGroupTime(boost::posix_time::not_a_date_time) {} + + public: + virtual const std::string& getName() { + return backend->getName(); + } }; } diff --git a/src/Common/UserConfigBackend.h b/src/Common/UserConfigBackend.h new file mode 100644 index 0000000..0036e9c --- /dev/null +++ b/src/Common/UserConfigBackend.h @@ -0,0 +1,54 @@ +/* + * UserConfigBackend.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_COMMON_USERCONFIGBACKEND_H_ +#define MAD_COMMON_USERCONFIGBACKEND_H_ + +#include + +#include + +#include "UserInfo.h" +#include "GroupInfo.h" + +namespace Mad { +namespace Common { + +class UserManager; + +class UserConfigBackend { + protected: + friend class UserManager; + + virtual void addUser(const UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} + virtual void updateUser(const UserInfo &oldUserInfo _UNUSED_PARAMETER_, const UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} + virtual void deleteUser(const UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} + + virtual void addGroup(const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} + virtual void updateGroup(const GroupInfo &oldGroupInfo _UNUSED_PARAMETER_, const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} + virtual void deleteGroup(const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} + + virtual void addUserToGroup(const UserInfo &userInfo _UNUSED_PARAMETER_, const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} + virtual void deleteUserFromGroup(const UserInfo &userInfo _UNUSED_PARAMETER_, const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) {} +}; + +} +} + +#endif /* MAD_COMMON_USERCONFIGBACKEND_H_ */ diff --git a/src/Common/UserDBBackend.h b/src/Common/UserDBBackend.h new file mode 100644 index 0000000..5267ef3 --- /dev/null +++ b/src/Common/UserDBBackend.h @@ -0,0 +1,137 @@ +/* + * UserDBBackend.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_SERVER_USERDBBACKEND_H_ +#define MAD_SERVER_USERDBBACKEND_H_ + +#include + +#include + +#include "UserInfo.h" +#include "GroupInfo.h" + +#include +#include +#include + +#include +#include + + +namespace Mad { +namespace Common { + +class UserCache; +class UserManager; + +class UserDBBackend { + protected: + friend class UserCache; + friend class UserManager; + + UserDBBackend() {} + + virtual boost::shared_ptr > getUserList(boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual boost::shared_ptr getUserInfo(unsigned long uid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual boost::shared_ptr getUserInfoByName(const std::string &name _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual boost::shared_ptr > getUserGroupList(unsigned long uid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + + virtual boost::shared_ptr > getGroupList(boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual boost::shared_ptr getGroupInfo(unsigned long gid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual boost::shared_ptr getGroupInfoByName(const std::string &name _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + + virtual boost::shared_ptr > getGroupUserList(unsigned long gid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual boost::shared_ptr > getFullUserGroupList(boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) 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)); + } + + + virtual void addUser(const UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual void updateUser(unsigned long uid _UNUSED_PARAMETER_, const UserInfo &userInfo _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual void deleteUser(unsigned long uid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + + virtual void addGroup(const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual void updateGroup(unsigned long gid _UNUSED_PARAMETER_, const GroupInfo &groupInfo _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual void deleteGroup(unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual void addUserToGroup(unsigned long uid _UNUSED_PARAMETER_, unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + virtual void deleteUserFromGroup(unsigned long uid _UNUSED_PARAMETER_, unsigned long gid _UNUSED_PARAMETER_) throw(Core::Exception) { + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + } + + public: + virtual ~UserDBBackend() {} + + virtual const std::string& getName() = 0; +}; + +} +} + +#endif /* MAD_SERVER_USERDBBACKEND_H_ */ diff --git a/src/Common/UserManager.cpp b/src/Common/UserManager.cpp index 928289b..d528137 100644 --- a/src/Common/UserManager.cpp +++ b/src/Common/UserManager.cpp @@ -20,344 +20,191 @@ #include "UserManager.h" #include "UserCache.h" -#include - namespace Mad { namespace Common { -bool UserManager::Compare::operator() (boost::shared_ptr b1, boost::shared_ptr b2) { - if(b1->getPriority() == b2->getPriority()) - return (b1.get() > b2.get()); - else - return (b1->getPriority() > b2->getPriority()); +void UserManager::registerBackend(boost::shared_ptr backend) { + boost::lock_guard lock(mutex); + + const std::string &name = backend->getName(); + + dbBackends.insert(std::make_pair(backend, std::make_pair(backend, name))); + dbBackendNames.insert(std::make_pair(name, backend)); + + if(!dbBackend) + dbBackend = backend; } +void UserManager::registerBackendCached(boost::shared_ptr backend) { + boost::lock_guard lock(mutex); + + const std::string &name = backend->getName(); + + boost::shared_ptr cache(new UserCache(application, backend)); -void UserManager::registerBackendCached(boost::shared_ptr backend) { - { - boost::lock_guard lock(mutex); - backends.insert(std::make_pair(backend, boost::shared_ptr(new UserCache(application, backend)))); - } + dbBackends.insert(std::make_pair(backend, std::make_pair(cache, name))); + dbBackendNames.insert(std::make_pair(name, backend)); + + if(!dbBackend) + dbBackend = cache; } boost::shared_ptr > UserManager::getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getUserList(timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getUserList(timestamp); } boost::shared_ptr UserManager::getUserInfo(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getUserInfo(uid, timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getUserInfo(uid, timestamp); } boost::shared_ptr UserManager::getUserInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getUserInfoByName(name, timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getUserInfoByName(name, timestamp); } boost::shared_ptr > UserManager::getUserGroupList(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getUserGroupList(uid, timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getUserGroupList(uid, timestamp); } boost::shared_ptr > UserManager::getGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); - - boost::shared_ptr > ret; - - { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getGroupList(timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - } + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - throw e; + return dbBackend->getGroupList(timestamp); } boost::shared_ptr UserManager::getGroupInfo(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getGroupInfo(gid, timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getGroupInfo(gid, timestamp); } boost::shared_ptr UserManager::getGroupInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getGroupInfoByName(name, timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getGroupInfoByName(name, timestamp); } boost::shared_ptr > UserManager::getGroupUserList(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getGroupUserList(gid, timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getGroupUserList(gid, timestamp); } boost::shared_ptr > UserManager::getFullUserGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock lock(mutex); - boost::lock_guard lock(mutex); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - return backend->second->getFullUserGroupList(timestamp); - } - catch(Core::Exception e2) { - if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - e = e2; - } - } - - throw e; + return dbBackend->getFullUserGroupList(timestamp); } -void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { - boost::lock_guard lock(mutex); - - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->setPassword(uid, password); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); +void UserManager::setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_AVAILABLE); } void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->addUser(userInfo); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + dbBackend->addUser(userInfo); } void UserManager::updateUser(unsigned long uid, const UserInfo &userInfo) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->updateUser(uid, userInfo); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + dbBackend->updateUser(uid, userInfo); } void UserManager::deleteUser(unsigned long uid) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->deleteUser(uid); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + dbBackend->deleteUser(uid); } void UserManager::addGroup(const GroupInfo &groupInfo) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->addGroup(groupInfo); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + dbBackend->addGroup(groupInfo); } void UserManager::updateGroup(unsigned long gid, const GroupInfo &groupInfo) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); + + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->updateGroup(gid, groupInfo); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + dbBackend->updateGroup(gid, groupInfo); } void UserManager::deleteGroup(unsigned long gid) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); + + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->deleteGroup(gid); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + dbBackend->deleteGroup(gid); } void UserManager::addUserToGroup(unsigned long uid, unsigned long gid) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); + + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->addUserToGroup(uid, gid); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + dbBackend->addUserToGroup(uid, gid); } void UserManager::deleteUserFromGroup(unsigned long uid, unsigned long gid) throw(Core::Exception) { - boost::lock_guard lock(mutex); + boost::shared_lock lock(mutex); + + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); - for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - try { - backend->second->deleteUserFromGroup(uid, gid); - return; - } - catch(Core::Exception e) { - if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - throw e; - } - } - - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + dbBackend->deleteUserFromGroup(uid, gid); } } diff --git a/src/Common/UserManager.h b/src/Common/UserManager.h index d2529e7..5cf14db 100644 --- a/src/Common/UserManager.h +++ b/src/Common/UserManager.h @@ -35,38 +35,65 @@ namespace Mad { namespace Common { class Application; -class UserBackend; +class UserConfigBackend; +class UserDBBackend; class UserCache; class UserManager : private boost::noncopyable { private: friend class Application; - struct Compare { - bool operator() (boost::shared_ptr b1, boost::shared_ptr b2); - }; - - typedef std::map, boost::shared_ptr, Compare> BackendMap; + typedef std::map, std::pair, std::string> > BackendMap; Application *application; - BackendMap backends; + std::set > configBackends; + + BackendMap dbBackends; + std::map > dbBackendNames; + + boost::shared_ptr dbBackend; boost::shared_mutex mutex; UserManager(Application *application0) : application(application0) {} public: - void registerBackend(boost::shared_ptr backend) { + void registerBackend(boost::shared_ptr backend); + void registerBackendCached(boost::shared_ptr backend); + + void unregisterBackend(boost::shared_ptr backend) { boost::lock_guard lock(mutex); - backends.insert(std::make_pair(backend, backend)); + + BackendMap::iterator it = dbBackends.find(backend); + if(it == dbBackends.end()) + return; + + boost::shared_ptr oldBackend = it->second.first; + + std::map >::iterator it2 = dbBackendNames.find(it->second.second); + if(it2->second == backend) + dbBackendNames.erase(it2); + + dbBackends.erase(it); + + if(dbBackend == oldBackend) { + if(dbBackends.empty()) + dbBackend.reset(); + else + dbBackend = dbBackends.begin()->second.first; + } } - void registerBackendCached(boost::shared_ptr backend); - void unregisterBackend(boost::shared_ptr backend) { + void registerBackend(boost::shared_ptr backend) { + boost::lock_guard lock(mutex); + configBackends.insert(backend); + } + + void unregisterBackend(boost::shared_ptr backend) { boost::lock_guard lock(mutex); - backends.erase(backend); + configBackends.erase(backend); } boost::shared_ptr > getUserList(boost::posix_time::ptime *timestamp = 0) throw(Core::Exception); diff --git a/src/modules/UserBackendMysql/Module.h b/src/modules/UserBackendMysql/Module.h index 7cd963a..93396a7 100644 --- a/src/modules/UserBackendMysql/Module.h +++ b/src/modules/UserBackendMysql/Module.h @@ -22,8 +22,8 @@ #include "UserBackendMysql.h" -#include #include +#include namespace Mad { namespace Modules { diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index e076b10..e7c656b 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -36,6 +36,7 @@ namespace Mad { namespace Modules { namespace UserBackendMysql { +const std::string UserBackendMysql::name("UserBackendMysql"); bool UserBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { if(handled) diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 5ed8480..85bc678 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -20,7 +20,7 @@ #ifndef MAD_MODULES_USERBACKENDMYSQL_USERBACKENDMYSQL_H_ #define MAD_MODULES_USERBACKENDMYSQL_USERBACKENDMYSQL_H_ -#include +#include #include #include @@ -35,7 +35,7 @@ namespace Mad { namespace Modules { namespace UserBackendMysql { -class UserBackendMysql : public Common::UserBackend, private Core::Configurable, private boost::noncopyable { +class UserBackendMysql : public Common::UserDBBackend, private Core::Configurable, private boost::noncopyable { private: typedef std::map > ArgumentMap; @@ -84,6 +84,8 @@ class UserBackendMysql : public Common::UserBackend, private Core::Configurable, } }; + static const std::string name; + Common::Application *application; std::string host, username, passwd, db, unixSocket; @@ -104,7 +106,7 @@ class UserBackendMysql : public Common::UserBackend, private Core::Configurable, boost::mutex mutex; protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool); + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); virtual void configFinished(); virtual boost::shared_ptr > getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception); @@ -139,6 +141,10 @@ class UserBackendMysql : public Common::UserBackend, private Core::Configurable, mysql = 0; } } + + virtual const std::string& getName() { + return name; + } }; } -- cgit v1.2.3