From f7d433e6e8559b4584263cae025d3addd1342df4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 5 Aug 2009 23:55:44 +0200 Subject: =?UTF-8?q?UserManager:=20Check-Funktionen=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Backends/NetworkUserBackend.cpp | 87 +++++++++++++++++++++- src/Common/Backends/NetworkUserBackend.h | 37 ++++++++- src/Common/UserCache.h | 11 +++ src/Common/UserConfigBackend.h | 4 + src/Common/UserDBBackend.h | 10 ++- src/Common/UserManager.cpp | 35 +++++++++ src/Common/UserManager.h | 8 +- src/Core/Exception.h | 2 +- .../RequestHandlers/UserRequestHandlerGroup.cpp | 49 ++++++++++++ .../RequestHandlers/UserRequestHandlerGroup.h | 6 ++ 10 files changed, 238 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Common/Backends/NetworkUserBackend.cpp b/src/Common/Backends/NetworkUserBackend.cpp index 39f4e5b..da964e7 100644 --- a/src/Common/Backends/NetworkUserBackend.cpp +++ b/src/Common/Backends/NetworkUserBackend.cpp @@ -62,7 +62,7 @@ void NetworkUserBackend::NameUserRequest::sendRequest() { void NetworkUserBackend::UserAddRequest::sendRequest() { XmlPacket packet; - packet.setType("AddUser"); + packet.setType(check ? "CheckUserInfo" : "AddUser"); packet.set("uid", userInfo.getUid()); packet.set("gid", userInfo.getGid()); @@ -85,6 +85,27 @@ void NetworkUserBackend::UserUpdateRequest::sendRequest() { sendPacket(packet); } +void NetworkUserBackend::GroupAddRequest::sendRequest() { + XmlPacket packet; + packet.setType(check ? "CheckGroupInfo" : "AddGroup"); + + packet.set("gid", groupInfo.getGid()); + packet.set("name", groupInfo.getName()); + + sendPacket(packet); +} + +void NetworkUserBackend::GroupUpdateRequest::sendRequest() { + XmlPacket packet; + packet.setType("UpdateGroup"); + + packet.set("origGid", gid); + packet.set("gid", groupInfo.getGid()); + packet.set("name", groupInfo.getName()); + + sendPacket(packet); +} + void NetworkUserBackend::UserGroupRequest::sendRequest() { XmlPacket packet; packet.setType(type); @@ -381,10 +402,22 @@ boost::shared_ptr > NetworkUse }*/ +void NetworkUserBackend::checkUserInfo(const UserInfo &userInfo) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::shared_ptr request(new UserAddRequest(application, userInfo, true)); + application->getRequestManager()->sendRequest(connection, request); + request->wait(); + + std::pair, Core::Exception> result = request->getResult(); + if(!result.first || result.second) + throw result.second; +} + void NetworkUserBackend::addUser(const UserInfo &userInfo) throw(Core::Exception) { application->getThreadManager()->detach(); - boost::shared_ptr request(new UserAddRequest(application, userInfo)); + boost::shared_ptr request(new UserAddRequest(application, userInfo, false)); application->getRequestManager()->sendRequest(connection, request); request->wait(); @@ -417,6 +450,56 @@ void NetworkUserBackend::deleteUser(unsigned long uid) throw(Core::Exception) { throw result.second; } + +void NetworkUserBackend::checkGroupInfo(const GroupInfo &groupInfo) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::shared_ptr request(new GroupAddRequest(application, groupInfo, true)); + application->getRequestManager()->sendRequest(connection, request); + request->wait(); + + std::pair, Core::Exception> result = request->getResult(); + if(!result.first || result.second) + throw result.second; +} + +void NetworkUserBackend::addGroup(const GroupInfo &groupInfo) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::shared_ptr request(new GroupAddRequest(application, groupInfo, false)); + application->getRequestManager()->sendRequest(connection, request); + request->wait(); + + std::pair, Core::Exception> result = request->getResult(); + if(!result.first || result.second) + throw result.second; +} + +void NetworkUserBackend::updateGroup(unsigned long gid, const GroupInfo &groupInfo) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::shared_ptr request(new GroupUpdateRequest(application, gid, groupInfo)); + application->getRequestManager()->sendRequest(connection, request); + request->wait(); + + std::pair, Core::Exception> result = request->getResult(); + if(!result.first || result.second) + throw result.second; +} + +void NetworkUserBackend::deleteGroup(unsigned long gid) throw(Core::Exception) { + application->getThreadManager()->detach(); + + boost::shared_ptr request(new IdUserRequest(application, "DeleteGroup", "gid", gid)); + application->getRequestManager()->sendRequest(connection, request); + request->wait(); + + std::pair, Core::Exception> result = request->getResult(); + if(!result.first || result.second) + throw result.second; +} + + void NetworkUserBackend::addUserToGroup(unsigned long uid, unsigned long gid) throw(Core::Exception) { application->getThreadManager()->detach(); diff --git a/src/Common/Backends/NetworkUserBackend.h b/src/Common/Backends/NetworkUserBackend.h index 6c0e94d..a3fa1a6 100644 --- a/src/Common/Backends/NetworkUserBackend.h +++ b/src/Common/Backends/NetworkUserBackend.h @@ -74,13 +74,14 @@ class NetworkUserBackend : public UserDBBackend { class UserAddRequest : public Request { private: UserInfo userInfo; + bool check; protected: virtual void sendRequest(); public: - UserAddRequest(Application *application, const UserInfo &userInfo0) - : Request(application), userInfo(userInfo0) {} + UserAddRequest(Application *application, const UserInfo &userInfo0, bool checkOnly) + : Request(application), userInfo(userInfo0), check(checkOnly) {} }; class UserUpdateRequest : public Request { @@ -96,6 +97,32 @@ class NetworkUserBackend : public UserDBBackend { : Request(application), uid(uid0), userInfo(userInfo0) {} }; + class GroupAddRequest : public Request { + private: + GroupInfo groupInfo; + bool check; + + protected: + virtual void sendRequest(); + + public: + GroupAddRequest(Application *application, const GroupInfo &groupInfo0, bool checkOnly) + : Request(application), groupInfo(groupInfo0), check(checkOnly) {} + }; + + class GroupUpdateRequest : public Request { + private: + unsigned long gid; + GroupInfo groupInfo; + + protected: + virtual void sendRequest(); + + public: + GroupUpdateRequest(Application *application, unsigned long gid0, const GroupInfo &groupInfo0) + : Request(application), gid(gid0), groupInfo(groupInfo0) {} + }; + class UserGroupRequest : public Request { private: std::string type; @@ -130,10 +157,16 @@ class NetworkUserBackend : public UserDBBackend { //virtual void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception); + virtual void checkUserInfo(const UserInfo &userInfo) throw(Core::Exception); virtual void addUser(const UserInfo &userInfo) throw(Core::Exception); virtual void updateUser(unsigned long uid, const Common::UserInfo &userInfo) throw(Core::Exception); virtual void deleteUser(unsigned long uid) throw(Core::Exception); + virtual void checkGroupInfo(const GroupInfo &groupInfo) throw(Core::Exception); + virtual void addGroup(const GroupInfo &groupInfo) throw(Core::Exception); + virtual void updateGroup(unsigned long gid, const GroupInfo &groupInfo) throw(Core::Exception); + virtual void deleteGroup(unsigned long gid) throw(Core::Exception); + virtual void addUserToGroup(unsigned long uid, unsigned long gid) throw(Core::Exception); virtual void deleteUserFromGroup(unsigned long uid, unsigned long gid) throw(Core::Exception); diff --git a/src/Common/UserCache.h b/src/Common/UserCache.h index ff70520..1912caa 100644 --- a/src/Common/UserCache.h +++ b/src/Common/UserCache.h @@ -74,6 +74,11 @@ class UserCache : public UserDBBackend, private boost::noncopyable { backend->setPassword(uid, password); } + + virtual void checkUserInfo(const UserInfo &userInfo) throw(Core::Exception) { + backend->checkUserInfo(userInfo); + } + virtual void addUser(const UserInfo &userInfo) throw(Core::Exception) { backend->addUser(userInfo); } @@ -86,6 +91,11 @@ class UserCache : public UserDBBackend, private boost::noncopyable { backend->deleteUser(uid); } + + virtual void checkGroupInfo(const GroupInfo &groupInfo) throw(Core::Exception) { + backend->checkGroupInfo(groupInfo); + } + virtual void addGroup(const GroupInfo &groupInfo) throw(Core::Exception) { backend->addGroup(groupInfo); } @@ -98,6 +108,7 @@ class UserCache : public UserDBBackend, private boost::noncopyable { backend->deleteGroup(gid); } + virtual void addUserToGroup(unsigned long uid, unsigned long gid) throw(Core::Exception) { backend->addUserToGroup(uid, gid); } diff --git a/src/Common/UserConfigBackend.h b/src/Common/UserConfigBackend.h index 5055599..0c1f146 100644 --- a/src/Common/UserConfigBackend.h +++ b/src/Common/UserConfigBackend.h @@ -34,10 +34,14 @@ class UserConfigBackend { protected: friend class UserManager; + virtual void checkUserInfo(const UserInfo& /*userInfo*/) throw(Core::Exception) {} + virtual void addUser(const UserInfo& /*userInfo*/) throw(Core::Exception) {} virtual void updateUser(const UserInfo& /*oldUserInfo*/, const UserInfo& /*userInfo*/) throw(Core::Exception) {} virtual void deleteUser(const UserInfo& /*userInfo*/) throw(Core::Exception) {} + virtual void checkGroupInfo(const GroupInfo& /*groupInfo*/) throw(Core::Exception) {} + virtual void addGroup(const GroupInfo& /*groupInfo*/) throw(Core::Exception) {} virtual void updateGroup(const GroupInfo& /*oldGroupInfo*/, const GroupInfo& /*groupInfo*/) throw(Core::Exception) {} virtual void deleteGroup(const GroupInfo& /*groupInfo*/) throw(Core::Exception) {} diff --git a/src/Common/UserDBBackend.h b/src/Common/UserDBBackend.h index bed4c83..4a33f3a 100644 --- a/src/Common/UserDBBackend.h +++ b/src/Common/UserDBBackend.h @@ -17,8 +17,8 @@ * with this program. If not, see . */ -#ifndef MAD_SERVER_USERDBBACKEND_H_ -#define MAD_SERVER_USERDBBACKEND_H_ +#ifndef MAD_COMMON_USERDBBACKEND_H_ +#define MAD_COMMON_USERDBBACKEND_H_ #include @@ -90,6 +90,8 @@ class UserDBBackend { } + virtual void checkUserInfo(const UserInfo& /*userInfo*/) throw(Core::Exception) {} + virtual void addUser(const UserInfo& /*userInfo*/) throw(Core::Exception) { throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } @@ -103,6 +105,8 @@ class UserDBBackend { } + virtual void checkGroupInfo(const GroupInfo& /*groupInfo*/) throw(Core::Exception) {} + virtual void addGroup(const GroupInfo& /*groupInfo*/) throw(Core::Exception) { throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); } @@ -132,4 +136,4 @@ class UserDBBackend { } } -#endif /* MAD_SERVER_USERDBBACKEND_H_ */ +#endif /* MAD_COMMON_USERDBBACKEND_H_ */ diff --git a/src/Common/UserManager.cpp b/src/Common/UserManager.cpp index 37cee6c..7f50f28 100644 --- a/src/Common/UserManager.cpp +++ b/src/Common/UserManager.cpp @@ -137,7 +137,22 @@ void UserManager::setPassword(unsigned long /*uid*/, const std::string& /*passwo throw Core::Exception(Core::Exception::NOT_AVAILABLE); } +void UserManager::checkUserInfo(const UserInfo &userInfo) throw(Core::Exception) { + boost::shared_lock lock(mutex); + + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + dbBackend->checkUserInfo(userInfo); + + for(std::set >::iterator configBackend = configBackends.begin(); configBackend != configBackends.end(); ++configBackend) { + (*configBackend)->checkUserInfo(userInfo); + } +} + void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) { + checkUserInfo(userInfo); + boost::lock_guard lock(mutex); if(!dbBackend) @@ -156,6 +171,8 @@ void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) { } void UserManager::updateUser(unsigned long uid, const UserInfo &userInfo) throw(Core::Exception) { + checkUserInfo(userInfo); + boost::lock_guard lock(mutex); if(!dbBackend) @@ -199,7 +216,23 @@ void UserManager::deleteUser(unsigned long uid) throw(Core::Exception) { } } + +void UserManager::checkGroupInfo(const GroupInfo &groupInfo) throw(Core::Exception) { + boost::shared_lock lock(mutex); + + if(!dbBackend) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + dbBackend->checkGroupInfo(groupInfo); + + for(std::set >::iterator configBackend = configBackends.begin(); configBackend != configBackends.end(); ++configBackend) { + (*configBackend)->checkGroupInfo(groupInfo); + } +} + void UserManager::addGroup(const GroupInfo &groupInfo) throw(Core::Exception) { + checkGroupInfo(groupInfo); + boost::lock_guard lock(mutex); if(!dbBackend) @@ -218,6 +251,8 @@ void UserManager::addGroup(const GroupInfo &groupInfo) throw(Core::Exception) { } void UserManager::updateGroup(unsigned long gid, const GroupInfo &groupInfo) throw(Core::Exception) { + checkGroupInfo(groupInfo); + boost::lock_guard lock(mutex); if(!dbBackend) diff --git a/src/Common/UserManager.h b/src/Common/UserManager.h index 5cf14db..5eece35 100644 --- a/src/Common/UserManager.h +++ b/src/Common/UserManager.h @@ -17,8 +17,8 @@ * with this program. If not, see . */ -#ifndef MAD_SERVER_USERMANAGER_H_ -#define MAD_SERVER_USERMANAGER_H_ +#ifndef MAD_COMMON_USERMANAGER_H_ +#define MAD_COMMON_USERMANAGER_H_ #include "UserInfo.h" #include "GroupInfo.h" @@ -110,10 +110,12 @@ class UserManager : private boost::noncopyable { void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception); + void checkUserInfo(const UserInfo &userInfo) throw(Core::Exception); void addUser(const UserInfo &userInfo) throw(Core::Exception); void updateUser(unsigned long uid, const UserInfo &userInfo) throw(Core::Exception); void deleteUser(unsigned long uid) throw(Core::Exception); + void checkGroupInfo(const GroupInfo &groupInfo) throw(Core::Exception); void addGroup(const GroupInfo &groupInfo) throw(Core::Exception); void updateGroup(unsigned long gid, const GroupInfo &groupInfo) throw(Core::Exception); void deleteGroup(unsigned long gid) throw(Core::Exception); @@ -125,4 +127,4 @@ class UserManager : private boost::noncopyable { } } -#endif /* MAD_SERVER_USERMANAGER_H_ */ +#endif /* MAD_COMMON_USERMANAGER_H_ */ diff --git a/src/Core/Exception.h b/src/Core/Exception.h index c6bcebe..1824ff6 100644 --- a/src/Core/Exception.h +++ b/src/Core/Exception.h @@ -29,7 +29,7 @@ class Exception { public: enum ErrorCode { SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, NOT_AVAILABLE = 0x0003, NOT_FINISHED = 0x0004, NOT_IMPLEMENTED = 0x0005, - NOT_FOUND = 0x0006, + NOT_FOUND = 0x0006, INVALID_INPUT = 0x0007, INTERNAL_ERRNO = 0x0010, INVALID_ADDRESS = 0x0020, ALREADY_IDENTIFIED = 0x0030, UNKNOWN_DAEMON = 0x0031, diff --git a/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp b/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp index b830920..c0de46a 100644 --- a/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp +++ b/src/Server/RequestHandlers/UserRequestHandlerGroup.cpp @@ -245,6 +245,18 @@ void UserRequestHandlerGroup::handleFullUserGroupListRequest(boost::shared_ptr packet, Common::XmlPacket *ret, + Common::Connection* /*connection*/) { + Common::UserInfo userInfo(packet->get("uid"), packet->get("username")); + userInfo.setGid(packet->get("gid")); + userInfo.setFullName(packet->get("fullName")); + + application->getUserManager()->checkUserInfo(userInfo); + + ret->setType("OK"); +} + void UserRequestHandlerGroup::handleUserAddRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { Common::UserInfo userInfo(packet->get("uid"), packet->get("username")); @@ -274,6 +286,37 @@ void UserRequestHandlerGroup::handleUserDeleteRequest(boost::shared_ptrsetType("OK"); } + +void UserRequestHandlerGroup::handleGroupInfoCheckRequest(boost::shared_ptr packet, Common::XmlPacket *ret, + Common::Connection* /*connection*/) { + application->getUserManager()->checkGroupInfo(Common::GroupInfo(packet->get("gid"), packet->get("name"))); + + ret->setType("OK"); +} + +void UserRequestHandlerGroup::handleGroupAddRequest(boost::shared_ptr packet, Common::XmlPacket *ret, + Common::Connection* /*connection*/) { + application->getUserManager()->addGroup(Common::GroupInfo(packet->get("gid"), packet->get("name"))); + + ret->setType("OK"); +} + +void UserRequestHandlerGroup::handleGroupUpdateRequest(boost::shared_ptr packet, Common::XmlPacket *ret, + Common::Connection* /*connection*/) { + application->getUserManager()->updateGroup(packet->get("origGid"), + Common::GroupInfo(packet->get("gid"), packet->get("name"))); + + ret->setType("OK"); +} + +void UserRequestHandlerGroup::handleGroupDeleteRequest(boost::shared_ptr packet, Common::XmlPacket *ret, + Common::Connection* /*connection*/) { + application->getUserManager()->deleteGroup(packet->get("gid")); + + ret->setType("OK"); +} + + void UserRequestHandlerGroup::handleAddUserToGroupRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { application->getUserManager()->addUserToGroup(packet->get("uid"), packet->get("gid")); @@ -299,10 +342,16 @@ UserRequestHandlerGroup::UserRequestHandlerGroup(Application *application0) : ap registerHandler("GetFullUserGroupList", boost::bind(&UserRequestHandlerGroup::handleFullUserGroupListRequest, this, _1, _2, _3)); + registerHandler("CheckUserInfo", boost::bind(&UserRequestHandlerGroup::handleUserInfoCheckRequest, this, _1, _2, _3)); registerHandler("AddUser", boost::bind(&UserRequestHandlerGroup::handleUserAddRequest, this, _1, _2, _3)); registerHandler("UpdateUser", boost::bind(&UserRequestHandlerGroup::handleUserUpdateRequest, this, _1, _2, _3)); registerHandler("DeleteUser", boost::bind(&UserRequestHandlerGroup::handleUserDeleteRequest, this, _1, _2, _3)); + registerHandler("CheckGroupInfo", boost::bind(&UserRequestHandlerGroup::handleGroupInfoCheckRequest, this, _1, _2, _3)); + registerHandler("AddGroup", boost::bind(&UserRequestHandlerGroup::handleGroupAddRequest, this, _1, _2, _3)); + registerHandler("UpdateGroup", boost::bind(&UserRequestHandlerGroup::handleGroupUpdateRequest, this, _1, _2, _3)); + registerHandler("DeleteGroup", boost::bind(&UserRequestHandlerGroup::handleGroupDeleteRequest, this, _1, _2, _3)); + registerHandler("AddUserToGroup", boost::bind(&UserRequestHandlerGroup::handleAddUserToGroupRequest, this, _1, _2, _3)); registerHandler("DeleteUserFromGroup", boost::bind(&UserRequestHandlerGroup::handleDeleteUserFromGroupRequest, this, _1, _2, _3)); } diff --git a/src/Server/RequestHandlers/UserRequestHandlerGroup.h b/src/Server/RequestHandlers/UserRequestHandlerGroup.h index 6da97a3..c1338c4 100644 --- a/src/Server/RequestHandlers/UserRequestHandlerGroup.h +++ b/src/Server/RequestHandlers/UserRequestHandlerGroup.h @@ -43,10 +43,16 @@ class UserRequestHandlerGroup : public Common::RequestHandlers::SimpleRequestHan void handleFullUserGroupListRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); + void handleUserInfoCheckRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); void handleUserAddRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); void handleUserUpdateRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); void handleUserDeleteRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); + void handleGroupInfoCheckRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); + void handleGroupAddRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); + void handleGroupUpdateRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); + void handleGroupDeleteRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); + void handleAddUserToGroupRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); void handleDeleteUserFromGroupRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection *connection); -- cgit v1.2.3