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/modules/UserBackendMysql/UserBackendMysql.cpp | 71 +++++++++++++++++++++-- src/modules/UserBackendMysql/UserBackendMysql.h | 7 +++ 2 files changed, 73 insertions(+), 5 deletions(-) (limited to 'src/modules/UserBackendMysql') 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 #include - +#include 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 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 > UserBackendMysql::getUserList() throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock 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 > UserBackendM boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long uid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -173,6 +184,8 @@ boost::shared_ptr 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 UserBackendMysql::getUserInfo(unsigned return user; } - throw Core::Exception(Core::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_FOUND); } boost::shared_ptr UserBackendMysql::getUserInfoByName(const std::string &name) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -207,6 +222,8 @@ boost::shared_ptr 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 UserBackendMysql::getUserInfoByName(co return user; } - throw Core::Exception(Core::Exception::NOT_AVAILABLE); + throw Core::Exception(Core::Exception::NOT_FOUND); } boost::shared_ptr > UserBackendMysql::getUserGroupList(unsigned long uid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -242,6 +261,8 @@ boost::shared_ptr > 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 > UserBackendMysql::getUserGroup boost::shared_ptr > UserBackendMysql::getGroupList() throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock 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 > UserBackend boost::shared_ptr UserBackendMysql::getGroupInfo(unsigned long gid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -295,6 +322,8 @@ boost::shared_ptr 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 UserBackendMysql::getGroupInfo(unsign if(row) return boost::shared_ptr(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 UserBackendMysql::getGroupInfoByName(const std::string &name) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -321,6 +352,8 @@ boost::shared_ptr 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 UserBackendMysql::getGroupInfoByName( if(row) return boost::shared_ptr(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 > UserBackendMysql::getGroupUserList(unsigned long gid) throw(Core::Exception) { application->getThreadManager()->detach(); + boost::unique_lock lock(mutex); + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); @@ -348,6 +383,8 @@ boost::shared_ptr > 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 > UserBackendMysql::getGroupUser return users; } +boost::shared_ptr > UserBackendMysql::getFullUserGroupList() throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::unique_lock 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 > usergroups(new std::multimap); + + 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 +#include + 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 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); + public: UserBackendMysql(Common::Application *application0) : application(application0), port(0), mysql(0) { application->getConfigManager()->registerConfigurable(this); -- cgit v1.2.3