From 033145b65c543d1d6c0c05ee84c1031fcd5ba3c7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 26 May 2009 16:13:27 +0200 Subject: UserBackend-Interface erweitert und im Mysql-Backend implementiert --- src/modules/UserBackendMysql/UserBackendMysql.cpp | 155 +++++++++++++++++++++- 1 file changed, 151 insertions(+), 4 deletions(-) (limited to 'src/modules/UserBackendMysql/UserBackendMysql.cpp') 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 > 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 > UserBackendMysql:: boost::shared_ptr 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 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 UserBackendMysql::getUserInfo(unsigned long throw Net::Exception(Net::Exception::NOT_AVAILABLE); } +boost::shared_ptr 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 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 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 > 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 > groups(new std::set); + + while(MYSQL_ROW row = mysql_fetch_row(result)) + groups->insert(strtoul(row[0], 0, 10)); + + return groups; +} + + boost::shared_ptr > 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 > 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 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 > 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 > users(new std::set); + + while(MYSQL_ROW row = mysql_fetch_row(result)) + users->insert(strtoul(row[0], 0, 10)); + + return users; +} + void UserBackendMysql::registerBackend() { if(backend) -- cgit v1.2.3