diff options
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.cpp | 155 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.h | 5 |
2 files changed, 156 insertions, 4 deletions
diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index 9b960fd..6cc5b2f 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -135,7 +135,8 @@ void UserBackendMysql::configFinished() { boost::shared_ptr<std::map<unsigned long, Common::UserInfo> > UserBackendMysql::getUserList() throw(Net::Exception) { Net::ThreadManager::get()->detach(); - mysql_ping(mysql); + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListUsers.c_str(), queryListUsers.length()); MYSQL_RES *result = mysql_use_result(mysql); @@ -160,7 +161,8 @@ boost::shared_ptr<std::map<unsigned long, Common::UserInfo> > UserBackendMysql:: boost::shared_ptr<Common::UserInfo> UserBackendMysql::getUserInfo(unsigned long uid) throw(Net::Exception) { Net::ThreadManager::get()->detach(); - mysql_ping(mysql); + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); std::string query = queryUserById; @@ -169,7 +171,7 @@ boost::shared_ptr<Common::UserInfo> UserBackendMysql::getUserInfo(unsigned long tmp << uid; tmp << '"'; - query = boost::regex_replace(query, boost::regex("\\{ID\\}"), tmp.str(), boost::match_default); + query = boost::regex_replace(query, boost::regex("\\{UID\\}"), tmp.str(), boost::match_default); mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_use_result(mysql); @@ -193,10 +195,73 @@ boost::shared_ptr<Common::UserInfo> UserBackendMysql::getUserInfo(unsigned long throw Net::Exception(Net::Exception::NOT_AVAILABLE); } +boost::shared_ptr<Common::UserInfo> UserBackendMysql::getUserInfoByName(const std::string &name) throw(Net::Exception) { + Net::ThreadManager::get()->detach(); + + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + boost::scoped_array<char> escapedName(new char[name.length()*2+1]); + + mysql_real_escape_string(mysql, escapedName.get(), name.c_str(), name.length()); + + std::string query = boost::regex_replace(queryUserByName, boost::regex("\\{USER\\}"), "\""+std::string(escapedName.get())+"\"", boost::match_default); + + mysql_real_query(mysql, query.c_str(), query.length()); + MYSQL_RES *result = mysql_use_result(mysql); + + if(!result || mysql_num_fields(result) < 4) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + MYSQL_ROW row = mysql_fetch_row(result); + + if(row) { + boost::shared_ptr<Common::UserInfo> user(new Common::UserInfo(strtoul(row[0], 0, 10), row[2])); + + user->setGid(strtoul(row[1], 0, 10)); + user->setFullName(row[3]); + + while((row = mysql_fetch_row(result)) != 0) {} + + return user; + } + + throw Net::Exception(Net::Exception::NOT_AVAILABLE); +} + +boost::shared_ptr<std::set<unsigned long> > UserBackendMysql::getUserGroups(unsigned long uid) throw(Net::Exception) { + Net::ThreadManager::get()->detach(); + + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + std::ostringstream tmp; + tmp << '"'; + tmp << uid; + tmp << '"'; + + std::string query = boost::regex_replace(queryListUserGroups, boost::regex("\\{UID\\}"), tmp.str(), boost::match_default); + + mysql_real_query(mysql, query.c_str(), query.length()); + MYSQL_RES *result = mysql_use_result(mysql); + + if(!result || mysql_num_fields(result) < 1) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + boost::shared_ptr<std::set<unsigned long> > groups(new std::set<unsigned long>); + + while(MYSQL_ROW row = mysql_fetch_row(result)) + groups->insert(strtoul(row[0], 0, 10)); + + return groups; +} + + boost::shared_ptr<std::map<unsigned long, Common::GroupInfo> > UserBackendMysql::getGroupList() throw(Net::Exception) { Net::ThreadManager::get()->detach(); - mysql_ping(mysql); + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListGroups.c_str(), queryListGroups.length()); MYSQL_RES *result = mysql_use_result(mysql); @@ -215,6 +280,88 @@ boost::shared_ptr<std::map<unsigned long, Common::GroupInfo> > UserBackendMysql: return groups; } +std::string UserBackendMysql::getGroupName(unsigned long gid) throw(Net::Exception) { + Net::ThreadManager::get()->detach(); + + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + std::string query = queryGroupById; + + std::ostringstream tmp; + tmp << '"'; + tmp << gid; + tmp << '"'; + + query = boost::regex_replace(query, boost::regex("\\{GID\\}"), tmp.str(), boost::match_default); + + mysql_real_query(mysql, query.c_str(), query.length()); + MYSQL_RES *result = mysql_use_result(mysql); + + if(!result || mysql_num_fields(result) < 2) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + MYSQL_ROW row = mysql_fetch_row(result); + + if(row) + return row[1]; + + throw Net::Exception(Net::Exception::NOT_AVAILABLE); +} + +unsigned long UserBackendMysql::getGroupId(const std::string &name) throw(Net::Exception) { + Net::ThreadManager::get()->detach(); + + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + boost::scoped_array<char> escapedName(new char[name.length()*2+1]); + + mysql_real_escape_string(mysql, escapedName.get(), name.c_str(), name.length()); + + std::string query = boost::regex_replace(queryGroupByName, boost::regex("\\{GROUP\\}"), "\""+std::string(escapedName.get())+"\"", boost::match_default); + + mysql_real_query(mysql, query.c_str(), query.length()); + MYSQL_RES *result = mysql_use_result(mysql); + + if(!result || mysql_num_fields(result) < 2) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + MYSQL_ROW row = mysql_fetch_row(result); + + if(row) + return strtoul(row[0], 0, 10); + + throw Net::Exception(Net::Exception::NOT_AVAILABLE); +} + +boost::shared_ptr<std::set<unsigned long> > UserBackendMysql::getGroupUsers(unsigned long gid) throw(Net::Exception) { + Net::ThreadManager::get()->detach(); + + if(mysql_ping(mysql)) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + std::ostringstream tmp; + tmp << '"'; + tmp << gid; + tmp << '"'; + + std::string query = boost::regex_replace(queryListGroupUsers, boost::regex("\\{GID\\}"), tmp.str(), boost::match_default); + + mysql_real_query(mysql, query.c_str(), query.length()); + MYSQL_RES *result = mysql_use_result(mysql); + + if(!result || mysql_num_fields(result) < 1) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); + + boost::shared_ptr<std::set<unsigned long> > users(new std::set<unsigned long>); + + while(MYSQL_ROW row = mysql_fetch_row(result)) + users->insert(strtoul(row[0], 0, 10)); + + return users; +} + void UserBackendMysql::registerBackend() { if(backend) diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 1bf0f2b..71ee93f 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -52,8 +52,13 @@ class UserBackendMysql : public Server::UserBackend, private Common::Configurabl virtual boost::shared_ptr<std::map<unsigned long, Common::UserInfo> > getUserList() throw(Net::Exception); virtual boost::shared_ptr<Common::UserInfo> getUserInfo(unsigned long uid) throw(Net::Exception); + virtual boost::shared_ptr<Common::UserInfo> getUserInfoByName(const std::string &name) throw(Net::Exception); + virtual boost::shared_ptr<std::set<unsigned long> > getUserGroups(unsigned long uid) throw(Net::Exception); virtual boost::shared_ptr<std::map<unsigned long, Common::GroupInfo> > getGroupList() throw(Net::Exception); + virtual std::string getGroupName(unsigned long gid) throw(Net::Exception); + virtual unsigned long getGroupId(const std::string &name) throw(Net::Exception); + virtual boost::shared_ptr<std::set<unsigned long> > getGroupUsers(unsigned long gid) throw(Net::Exception); public: virtual ~UserBackendMysql() { |