diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Common/Backends/NetworkUserBackend.cpp | 2 | ||||
-rw-r--r-- | src/Common/Backends/NetworkUserBackend.h | 10 | ||||
-rw-r--r-- | src/Common/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/Common/UserCache.h | 13 | ||||
-rw-r--r-- | src/Common/UserConfigBackend.h | 54 | ||||
-rw-r--r-- | src/Common/UserDBBackend.h (renamed from src/Common/UserBackend.h) | 22 | ||||
-rw-r--r-- | src/Common/UserManager.cpp | 351 | ||||
-rw-r--r-- | src/Common/UserManager.h | 51 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/Module.h | 2 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.cpp | 1 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.h | 12 |
11 files changed, 236 insertions, 285 deletions
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/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 <limits> @@ -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<UserBackend> backend; + boost::shared_ptr<UserDBBackend> 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<UserBackend> backend0) : application(application0), backend(backend0), + UserCache(Application *application0, boost::shared_ptr<UserDBBackend> 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 <matthias@gamezock.de> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_COMMON_USERCONFIGBACKEND_H_ +#define MAD_COMMON_USERCONFIGBACKEND_H_ + +#include <config.h> + +#include <Core/Exception.h> + +#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/UserBackend.h b/src/Common/UserDBBackend.h index 9eb59d3..5267ef3 100644 --- a/src/Common/UserBackend.h +++ b/src/Common/UserDBBackend.h @@ -1,5 +1,5 @@ /* - * UserBackend.h + * UserDBBackend.h * * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> * @@ -17,8 +17,8 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef MAD_SERVER_USERBACKEND_H_ -#define MAD_SERVER_USERBACKEND_H_ +#ifndef MAD_SERVER_USERDBBACKEND_H_ +#define MAD_SERVER_USERDBBACKEND_H_ #include <config.h> @@ -41,12 +41,12 @@ namespace Common { class UserCache; class UserManager; -class UserBackend { +class UserDBBackend { protected: friend class UserCache; friend class UserManager; - UserBackend() {} + UserDBBackend() {} virtual boost::shared_ptr<const std::map<unsigned long, UserInfo> > getUserList(boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); @@ -77,6 +77,7 @@ class UserBackend { throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } + virtual boost::shared_ptr<const std::set<unsigned long> > getGroupUserList(unsigned long gid _UNUSED_PARAMETER_, boost::posix_time::ptime *timestamp _UNUSED_PARAMETER_) throw(Core::Exception) { throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } @@ -85,10 +86,12 @@ class UserBackend { 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)); } @@ -101,6 +104,7 @@ class UserBackend { 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)); } @@ -122,14 +126,12 @@ class UserBackend { } public: - virtual ~UserBackend() {} + virtual ~UserDBBackend() {} - virtual int getPriority() const { - return 0; - } + virtual const std::string& getName() = 0; }; } } -#endif /* MAD_SERVER_USERBACKEND_H_ */ +#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 <iostream> - namespace Mad { namespace Common { -bool UserManager::Compare::operator() (boost::shared_ptr<UserBackend> b1, boost::shared_ptr<UserBackend> b2) { - if(b1->getPriority() == b2->getPriority()) - return (b1.get() > b2.get()); - else - return (b1->getPriority() > b2->getPriority()); +void UserManager::registerBackend(boost::shared_ptr<UserDBBackend> backend) { + boost::lock_guard<boost::shared_mutex> 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<UserDBBackend> backend) { + boost::lock_guard<boost::shared_mutex> lock(mutex); + + const std::string &name = backend->getName(); + + boost::shared_ptr<UserCache> cache(new UserCache(application, backend)); -void UserManager::registerBackendCached(boost::shared_ptr<UserBackend> backend) { - { - boost::lock_guard<boost::shared_mutex> lock(mutex); - backends.insert(std::make_pair(backend, boost::shared_ptr<UserCache>(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<const std::map<unsigned long, UserInfo> > UserManager::getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<const UserInfo> UserManager::getUserInfo(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<const UserInfo> UserManager::getUserInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<const std::set<unsigned long> > UserManager::getUserGroupList(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<const std::map<unsigned long, GroupInfo> > UserManager::getGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); - - boost::shared_ptr<const std::map<unsigned long, GroupInfo> > ret; - - { - boost::lock_guard<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<const GroupInfo> UserManager::getGroupInfo(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<const GroupInfo> UserManager::getGroupInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<const std::set<unsigned long> > UserManager::getGroupUserList(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<const std::multimap<unsigned long, unsigned long> > UserManager::getFullUserGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { - Core::Exception e(Core::Exception::NOT_IMPLEMENTED); + boost::shared_lock<boost::shared_mutex> lock(mutex); - boost::lock_guard<boost::shared_mutex> 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<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<boost::shared_mutex> lock(mutex); + boost::shared_lock<boost::shared_mutex> 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<UserBackend> b1, boost::shared_ptr<UserBackend> b2); - }; - - typedef std::map<boost::shared_ptr<UserBackend>, boost::shared_ptr<UserBackend>, Compare> BackendMap; + typedef std::map<boost::shared_ptr<UserDBBackend>, std::pair<boost::shared_ptr<UserDBBackend>, std::string> > BackendMap; Application *application; - BackendMap backends; + std::set<boost::shared_ptr<UserConfigBackend> > configBackends; + + BackendMap dbBackends; + std::map<std::string, boost::shared_ptr<UserDBBackend> > dbBackendNames; + + boost::shared_ptr<UserDBBackend> dbBackend; boost::shared_mutex mutex; UserManager(Application *application0) : application(application0) {} public: - void registerBackend(boost::shared_ptr<UserBackend> backend) { + void registerBackend(boost::shared_ptr<UserDBBackend> backend); + void registerBackendCached(boost::shared_ptr<UserDBBackend> backend); + + void unregisterBackend(boost::shared_ptr<UserDBBackend> backend) { boost::lock_guard<boost::shared_mutex> lock(mutex); - backends.insert(std::make_pair(backend, backend)); + + BackendMap::iterator it = dbBackends.find(backend); + if(it == dbBackends.end()) + return; + + boost::shared_ptr<UserDBBackend> oldBackend = it->second.first; + + std::map<std::string, boost::shared_ptr<UserDBBackend> >::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<UserBackend> backend); - void unregisterBackend(boost::shared_ptr<UserBackend> backend) { + void registerBackend(boost::shared_ptr<UserConfigBackend> backend) { + boost::lock_guard<boost::shared_mutex> lock(mutex); + configBackends.insert(backend); + } + + void unregisterBackend(boost::shared_ptr<UserConfigBackend> backend) { boost::lock_guard<boost::shared_mutex> lock(mutex); - backends.erase(backend); + configBackends.erase(backend); } boost::shared_ptr<const std::map<unsigned long, UserInfo> > 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 <Common/UserManager.h> #include <Common/Module.h> +#include <Common/UserManager.h> 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 <Common/UserBackend.h> +#include <Common/UserDBBackend.h> #include <Common/Application.h> #include <Core/Configurable.h> @@ -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<std::string, boost::variant<std::string, unsigned long> > 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<const std::map<unsigned long, Common::UserInfo> > 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; + } }; } |