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/modules/UserBackendMysql | |
parent | 68ec8067feec09a6fecf8bf2fbffb5d757db3d4a (diff) | |
download | mad-c9c2e1401bae1938fe392f6ee0903939b63b050c.tar mad-c9c2e1401bae1938fe392f6ee0903939b63b050c.zip |
UserManager erweitert
Ermögliche Caching der User-Group-Zuordnungen
Diffstat (limited to 'src/modules/UserBackendMysql')
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.cpp | 71 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.h | 7 |
2 files changed, 73 insertions, 5 deletions
diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index e6e6dda..6d64a51 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -28,7 +28,7 @@ #include <boost/bind.hpp> #include <boost/regex.hpp> - +#include <boost/thread/locks.hpp> namespace Mad { namespace Modules { @@ -106,6 +106,10 @@ bool UserBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool ha if(entry[3].empty()) queryGroupByName = entry[2][0]; } + else if(entry[2].getKey().matches("UserGroupTable")) { + if(entry[3].empty()) + queryUserGroupTable = entry[2][0]; + } else if(!entry[2].empty()) return false; } @@ -124,6 +128,7 @@ void UserBackendMysql::configFinished() { return; } + boost::lock_guard<boost::mutex> lock(mutex); mysql = mysql_init(0); mysql_real_connect(mysql, host.c_str(), username.c_str(), passwd.c_str(), db.c_str(), port, unixSocket.empty() ? 0 : unixSocket.c_str(), 0); } @@ -132,12 +137,16 @@ void UserBackendMysql::configFinished() { boost::shared_ptr<const std::map<unsigned long, Common::UserInfo> > UserBackendMysql::getUserList() throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListUsers.c_str(), queryListUsers.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 4) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -158,6 +167,8 @@ boost::shared_ptr<const std::map<unsigned long, Common::UserInfo> > UserBackendM boost::shared_ptr<const Common::UserInfo> UserBackendMysql::getUserInfo(unsigned long uid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -173,6 +184,8 @@ boost::shared_ptr<const Common::UserInfo> UserBackendMysql::getUserInfo(unsigned mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 4) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -189,12 +202,14 @@ boost::shared_ptr<const Common::UserInfo> UserBackendMysql::getUserInfo(unsigned return user; } - throw Core::Exception(Core::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_FOUND); } boost::shared_ptr<const Common::UserInfo> UserBackendMysql::getUserInfoByName(const std::string &name) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -207,6 +222,8 @@ boost::shared_ptr<const Common::UserInfo> UserBackendMysql::getUserInfoByName(co mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 4) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -223,12 +240,14 @@ boost::shared_ptr<const Common::UserInfo> UserBackendMysql::getUserInfoByName(co return user; } - throw Core::Exception(Core::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_FOUND); } boost::shared_ptr<const std::set<unsigned long> > UserBackendMysql::getUserGroupList(unsigned long uid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -242,6 +261,8 @@ boost::shared_ptr<const std::set<unsigned long> > UserBackendMysql::getUserGroup mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 1) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -257,12 +278,16 @@ boost::shared_ptr<const std::set<unsigned long> > UserBackendMysql::getUserGroup boost::shared_ptr<const std::map<unsigned long, Common::GroupInfo> > UserBackendMysql::getGroupList() throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListGroups.c_str(), queryListGroups.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 2) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -280,6 +305,8 @@ boost::shared_ptr<const std::map<unsigned long, Common::GroupInfo> > UserBackend boost::shared_ptr<const Common::GroupInfo> UserBackendMysql::getGroupInfo(unsigned long gid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -295,6 +322,8 @@ boost::shared_ptr<const Common::GroupInfo> UserBackendMysql::getGroupInfo(unsign mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 2) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -303,12 +332,14 @@ boost::shared_ptr<const Common::GroupInfo> UserBackendMysql::getGroupInfo(unsign if(row) return boost::shared_ptr<Common::GroupInfo>(new Common::GroupInfo(strtoul(row[0], 0, 10), row[1])); - throw Core::Exception(Core::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_FOUND); } boost::shared_ptr<const Common::GroupInfo> UserBackendMysql::getGroupInfoByName(const std::string &name) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -321,6 +352,8 @@ boost::shared_ptr<const Common::GroupInfo> UserBackendMysql::getGroupInfoByName( mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 2) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -329,12 +362,14 @@ boost::shared_ptr<const Common::GroupInfo> UserBackendMysql::getGroupInfoByName( if(row) return boost::shared_ptr<Common::GroupInfo>(new Common::GroupInfo(strtoul(row[0], 0, 10), row[1])); - throw Core::Exception(Core::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_FOUND); } boost::shared_ptr<const std::set<unsigned long> > UserBackendMysql::getGroupUserList(unsigned long gid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock<boost::mutex> lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -348,6 +383,8 @@ boost::shared_ptr<const std::set<unsigned long> > UserBackendMysql::getGroupUser mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_store_result(mysql); + lock.unlock(); + if(!result || mysql_num_fields(result) < 1) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -359,6 +396,30 @@ boost::shared_ptr<const std::set<unsigned long> > UserBackendMysql::getGroupUser return users; } +boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > UserBackendMysql::getFullUserGroupList() throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::unique_lock<boost::mutex> lock(mutex); + + if(!mysql || mysql_ping(mysql)) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + mysql_real_query(mysql, queryUserGroupTable.c_str(), queryUserGroupTable.length()); + MYSQL_RES *result = mysql_store_result(mysql); + + lock.unlock(); + + if(!result || mysql_num_fields(result) < 2) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + boost::shared_ptr<std::multimap<unsigned long, unsigned long> > usergroups(new std::multimap<unsigned long, unsigned long>); + + while(MYSQL_ROW row = mysql_fetch_row(result)) + usergroups->insert(std::make_pair(strtoul(row[0], 0, 10), strtoul(row[1], 0, 10))); + + return usergroups; +} + } } } diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 7832e95..eadbf3b 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -28,6 +28,8 @@ #include <mysql/mysql.h> +#include <boost/thread/mutex.hpp> + namespace Mad { namespace Modules { namespace UserBackendMysql { @@ -43,9 +45,12 @@ class UserBackendMysql : public Common::UserBackend, private Core::Configurable std::string queryListUserGroups, queryListGroupUsers; std::string queryUserById, queryUserByName; std::string queryGroupById, queryGroupByName; + std::string queryUserGroupTable; MYSQL *mysql; + boost::mutex mutex; + protected: virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool); virtual void configFinished(); @@ -60,6 +65,8 @@ class UserBackendMysql : public Common::UserBackend, private Core::Configurable virtual boost::shared_ptr<const Common::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); + public: UserBackendMysql(Common::Application *application0) : application(application0), port(0), mysql(0) { application->getConfigManager()->registerConfigurable(this); |