From a8ad2278025467f7cc9c9974d7e82be5752fb697 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 24 May 2009 15:09:24 +0200 Subject: Interface des UserManager ?berarbeitet --- .../RequestHandlers/GroupListRequestHandler.cpp | 14 ++-- .../RequestHandlers/UserInfoRequestHandler.cpp | 13 ++-- .../RequestHandlers/UserListRequestHandler.cpp | 14 ++-- src/Server/UserBackend.h | 22 +++--- src/Server/UserManager.cpp | 78 +++++++++++++++------- src/Server/UserManager.h | 12 ++-- src/modules/UserBackendMysql/UserBackendMysql.cpp | 20 +++--- src/modules/UserBackendMysql/UserBackendMysql.h | 6 +- 8 files changed, 112 insertions(+), 67 deletions(-) (limited to 'src') diff --git a/src/Server/RequestHandlers/GroupListRequestHandler.cpp b/src/Server/RequestHandlers/GroupListRequestHandler.cpp index 1ca2b92..356e105 100644 --- a/src/Server/RequestHandlers/GroupListRequestHandler.cpp +++ b/src/Server/RequestHandlers/GroupListRequestHandler.cpp @@ -42,11 +42,13 @@ void GroupListRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication - boost::shared_ptr > info = UserManager::get()->getGroupList(); + Common::XmlPacket ret; - if(info) { + try { + boost::shared_ptr > info = UserManager::get()->getGroupList(); + ret.setType("OK"); ret.addList("groups"); @@ -58,10 +60,12 @@ void GroupListRequestHandler::handlePacket(const Common::XmlPacket &packet) { entry.add("name", group->second.getName()); } } - - else { + catch(Net::Exception e) { ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + ret.add("ErrorCode", e.getErrorCode()); + ret.add("SubCode", e.getSubCode()); + ret.add("SubSubCode", e.getSubSubCode()); + ret.add("Where", e.getWhere()); } sendPacket(ret); diff --git a/src/Server/RequestHandlers/UserInfoRequestHandler.cpp b/src/Server/RequestHandlers/UserInfoRequestHandler.cpp index 287d026..e4ef447 100644 --- a/src/Server/RequestHandlers/UserInfoRequestHandler.cpp +++ b/src/Server/RequestHandlers/UserInfoRequestHandler.cpp @@ -42,11 +42,11 @@ void UserInfoRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication - boost::shared_ptr info = UserManager::get()->getUserInfo(packet["uid"]); - Common::XmlPacket ret; - if(info) { + try { + boost::shared_ptr info = UserManager::get()->getUserInfo(packet["uid"]); + ret.setType("OK"); ret.add("uid", info->getUid()); @@ -54,9 +54,12 @@ void UserInfoRequestHandler::handlePacket(const Common::XmlPacket &packet) { ret.add("username", info->getUsername()); ret.add("fullName", info->getFullName()); } - else { + catch(Net::Exception e) { ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + ret.add("ErrorCode", e.getErrorCode()); + ret.add("SubCode", e.getSubCode()); + ret.add("SubSubCode", e.getSubSubCode()); + ret.add("Where", e.getWhere()); } sendPacket(ret); diff --git a/src/Server/RequestHandlers/UserListRequestHandler.cpp b/src/Server/RequestHandlers/UserListRequestHandler.cpp index 713753b..37db6d5 100644 --- a/src/Server/RequestHandlers/UserListRequestHandler.cpp +++ b/src/Server/RequestHandlers/UserListRequestHandler.cpp @@ -42,11 +42,11 @@ void UserListRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication - boost::shared_ptr > info = UserManager::get()->getUserList(); - Common::XmlPacket ret; - if(info) { + try { + boost::shared_ptr > info = UserManager::get()->getUserList(); + ret.setType("OK"); ret.addList("users"); @@ -60,10 +60,12 @@ void UserListRequestHandler::handlePacket(const Common::XmlPacket &packet) { entry.add("fullName", user->second.getFullName()); } } - - else { + catch(Net::Exception e) { ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + ret.add("ErrorCode", e.getErrorCode()); + ret.add("SubCode", e.getSubCode()); + ret.add("SubSubCode", e.getSubSubCode()); + ret.add("Where", e.getWhere()); } sendPacket(ret); diff --git a/src/Server/UserBackend.h b/src/Server/UserBackend.h index 6d59615..2a51d1b 100644 --- a/src/Server/UserBackend.h +++ b/src/Server/UserBackend.h @@ -25,6 +25,8 @@ #include #include +#include + #include #include @@ -42,25 +44,25 @@ class UserBackend { UserBackend() {} - virtual boost::shared_ptr > getUserList() { - return boost::shared_ptr >(); + virtual boost::shared_ptr > getUserList() throw(Net::Exception) { + throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); } - virtual boost::shared_ptr getUserInfo(unsigned long uid _UNUSED_PARAMETER_) { - return boost::shared_ptr(); + virtual boost::shared_ptr getUserInfo(unsigned long uid _UNUSED_PARAMETER_) throw(Net::Exception) { + throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); } - virtual boost::shared_ptr > getGroupList() { - return boost::shared_ptr >(); + virtual boost::shared_ptr > getGroupList() throw(Net::Exception) { + throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); } // TODO Better interface... - virtual bool setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) { - return false; + virtual void setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) throw(Net::Exception) { + throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); } - virtual bool addUser(const Common::UserInfo &userInfo _UNUSED_PARAMETER_) { - return false; + virtual void addUser(const Common::UserInfo &userInfo _UNUSED_PARAMETER_) throw(Net::Exception) { + throw(Net::Exception(Net::Exception::NOT_IMPLEMENTED)); } virtual int getPriority() const { diff --git a/src/Server/UserManager.cpp b/src/Server/UserManager.cpp index e32cb57..7101b2c 100644 --- a/src/Server/UserManager.cpp +++ b/src/Server/UserManager.cpp @@ -34,52 +34,84 @@ bool UserManager::Compare::operator() (boost::shared_ptr b1, boost: } -boost::shared_ptr > UserManager::getUserList() { +boost::shared_ptr > UserManager::getUserList() throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - boost::shared_ptr > ret = (*backend)->getUserList(); - if(ret) - return ret; + try { + return (*backend)->getUserList(); + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return boost::shared_ptr >(); + throw e; } -boost::shared_ptr UserManager::getUserInfo(unsigned long uid) { +boost::shared_ptr UserManager::getUserInfo(unsigned long uid) throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - boost::shared_ptr ret = (*backend)->getUserInfo(uid); - if(ret) - return ret; + try { + return (*backend)->getUserInfo(uid); + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return boost::shared_ptr(); + throw e; } -boost::shared_ptr > UserManager::getGroupList() { +boost::shared_ptr > UserManager::getGroupList() throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - boost::shared_ptr > ret = (*backend)->getGroupList(); - if(ret) - return ret; + try { + return (*backend)->getGroupList(); + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return boost::shared_ptr >(); + throw e; } -bool UserManager::setPassword(unsigned long uid, const std::string &password) { +void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->setPassword(uid, password)) - return true; + try { + return (*backend)->setPassword(uid, password); + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } -bool UserManager::addUser(const Common::UserInfo &userInfo) { +void UserManager::addUser(const Common::UserInfo &userInfo) throw(Net::Exception) { + Net::Exception e(Net::Exception::NOT_IMPLEMENTED); + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->addUser(userInfo)) - return true; + try { + return (*backend)->addUser(userInfo); + } + catch(Net::Exception e2) { + if(e.getErrorCode() == Net::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Net::Exception::NOT_IMPLEMENTED) + e = e2; + } } - return false; + throw e; } } diff --git a/src/Server/UserManager.h b/src/Server/UserManager.h index e33b678..3377727 100644 --- a/src/Server/UserManager.h +++ b/src/Server/UserManager.h @@ -23,6 +23,8 @@ #include #include +#include + #include #include @@ -55,14 +57,14 @@ class UserManager : boost::noncopyable { backends.erase(backend); } - boost::shared_ptr > getUserList(); - boost::shared_ptr getUserInfo(unsigned long uid); + boost::shared_ptr > getUserList() throw(Net::Exception); + boost::shared_ptr getUserInfo(unsigned long uid) throw(Net::Exception); - boost::shared_ptr > getGroupList(); + boost::shared_ptr > getGroupList() throw(Net::Exception); - bool setPassword(unsigned long uid, const std::string &password); + void setPassword(unsigned long uid, const std::string &password) throw(Net::Exception); - bool addUser(const Common::UserInfo &userInfo); + void addUser(const Common::UserInfo &userInfo) throw(Net::Exception); static UserManager *get() { return &userManager; diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index 139a416..9b960fd 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -132,7 +132,7 @@ void UserBackendMysql::configFinished() { } -boost::shared_ptr > UserBackendMysql::getUserList() { +boost::shared_ptr > UserBackendMysql::getUserList() throw(Net::Exception) { Net::ThreadManager::get()->detach(); mysql_ping(mysql); @@ -140,8 +140,8 @@ boost::shared_ptr > UserBackendMysql:: mysql_real_query(mysql, queryListUsers.c_str(), queryListUsers.length()); MYSQL_RES *result = mysql_use_result(mysql); - if(mysql_num_fields(result) < 4) - return boost::shared_ptr >(); // TODO Error + if(!result || mysql_num_fields(result) < 4) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); boost::shared_ptr > users(new std::map()); @@ -157,7 +157,7 @@ boost::shared_ptr > UserBackendMysql:: return users; } -boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long uid) { +boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long uid) throw(Net::Exception) { Net::ThreadManager::get()->detach(); mysql_ping(mysql); @@ -174,8 +174,8 @@ boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long mysql_real_query(mysql, query.c_str(), query.length()); MYSQL_RES *result = mysql_use_result(mysql); - if(mysql_num_fields(result) < 4) - return boost::shared_ptr(); // TODO Error + if(!result || mysql_num_fields(result) < 4) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); MYSQL_ROW row = mysql_fetch_row(result); @@ -190,10 +190,10 @@ boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long return user; } - return boost::shared_ptr(); + throw Net::Exception(Net::Exception::NOT_AVAILABLE); } -boost::shared_ptr > UserBackendMysql::getGroupList() { +boost::shared_ptr > UserBackendMysql::getGroupList() throw(Net::Exception) { Net::ThreadManager::get()->detach(); mysql_ping(mysql); @@ -201,8 +201,8 @@ boost::shared_ptr > UserBackendMysql: mysql_real_query(mysql, queryListGroups.c_str(), queryListGroups.length()); MYSQL_RES *result = mysql_use_result(mysql); - if(mysql_num_fields(result) < 2) - return boost::shared_ptr >(); // TODO Error + if(!result || mysql_num_fields(result) < 2) + throw Net::Exception(Net::Exception::NOT_AVAILABLE); boost::shared_ptr > groups(new std::map()); diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 8a75a6b..1bf0f2b 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -50,10 +50,10 @@ class UserBackendMysql : public Server::UserBackend, private Common::Configurabl virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool); virtual void configFinished(); - virtual boost::shared_ptr > getUserList(); - virtual boost::shared_ptr getUserInfo(unsigned long uid); + virtual boost::shared_ptr > getUserList() throw(Net::Exception); + virtual boost::shared_ptr getUserInfo(unsigned long uid) throw(Net::Exception); - virtual boost::shared_ptr > getGroupList(); + virtual boost::shared_ptr > getGroupList() throw(Net::Exception); public: virtual ~UserBackendMysql() { -- cgit v1.2.3