summaryrefslogtreecommitdiffstats
path: root/src/modules/UserBackendMysql/UserBackendMysql.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/UserBackendMysql/UserBackendMysql.cpp')
-rw-r--r--src/modules/UserBackendMysql/UserBackendMysql.cpp71
1 files changed, 66 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;
+}
+
}
}
}