/* * UserRequestHandlerGroup.cpp * * Copyright (C) 2009 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program. If not, see . */ #include "UserRequestHandlerGroup.h" #include "../Application.h" #include #include namespace Mad { namespace Server { namespace RequestHandlers { void UserRequestHandlerGroup::handleUserListRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { boost::posix_time::ptime timestamp(boost::posix_time::not_a_date_time); const std::string ×tr = packet->get("timestamp"); if(!timestr.empty()) { try { timestamp = boost::posix_time::from_iso_string(timestr); } catch(...) {} } boost::shared_ptr > users = application->getUserManager()->getUserList(×tamp); ret->setType("OK"); if(!timestamp.is_not_a_date_time()) ret->set("timestamp", boost::posix_time::to_iso_string(timestamp)); if(users) { Common::XmlPacket::List *list = ret->createList("users"); for(std::map::const_iterator user = users->begin(); user != users->end(); ++user) { Common::XmlPacket::List::iterator entry = list->addEntry(); entry->set("uid", user->second.getUid()); entry->set("gid", user->second.getGid()); entry->set("username", user->second.getUsername()); entry->set("fullName", user->second.getFullName()); } } } void UserRequestHandlerGroup::handleUserInfoRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { boost::posix_time::ptime timestamp(boost::posix_time::not_a_date_time); const std::string ×tr = packet->get("timestamp"); if(!timestr.empty()) { try { timestamp = boost::posix_time::from_iso_string(timestr); } catch(...) {} } boost::shared_ptr info; unsigned long uid = packet->get("uid"); if(uid) info = application->getUserManager()->getUserInfo(uid, ×tamp); else info = application->getUserManager()->getUserInfoByName(packet->get("name"), ×tamp); ret->setType("OK"); if(!timestamp.is_not_a_date_time()) ret->set("timestamp", boost::posix_time::to_iso_string(timestamp)); if(info) { ret->set("uid", info->getUid()); ret->set("gid", info->getGid()); ret->set("username", info->getUsername()); ret->set("fullName", info->getFullName()); } } void UserRequestHandlerGroup::handleUserGroupListRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { boost::posix_time::ptime timestamp(boost::posix_time::not_a_date_time); const std::string ×tr = packet->get("timestamp"); if(!timestr.empty()) { try { timestamp = boost::posix_time::from_iso_string(timestr); } catch(...) {} } boost::shared_ptr > groups = application->getUserManager()->getUserGroupList(packet->get("uid"), ×tamp); ret->setType("OK"); if(!timestamp.is_not_a_date_time()) ret->set("timestamp", boost::posix_time::to_iso_string(timestamp)); if(groups) { Common::XmlPacket::List *list = ret->createList("groups"); for(std::set::const_iterator group = groups->begin(); group != groups->end(); ++group) { Common::XmlPacket::List::iterator entry = list->addEntry(); entry->set("gid", *group); } } } void UserRequestHandlerGroup::handleGroupListRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { boost::posix_time::ptime timestamp(boost::posix_time::not_a_date_time); const std::string ×tr = packet->get("timestamp"); if(!timestr.empty()) { try { timestamp = boost::posix_time::from_iso_string(timestr); } catch(...) {} } boost::shared_ptr > groups = application->getUserManager()->getGroupList(×tamp); ret->setType("OK"); if(!timestamp.is_not_a_date_time()) ret->set("timestamp", boost::posix_time::to_iso_string(timestamp)); if(groups) { Common::XmlPacket::List *list = ret->createList("groups"); for(std::map::const_iterator group = groups->begin(); group != groups->end(); ++group) { Common::XmlPacket::List::iterator entry = list->addEntry(); entry->set("gid", group->second.getGid()); entry->set("name", group->second.getName()); } } } void UserRequestHandlerGroup::handleGroupInfoRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { boost::posix_time::ptime timestamp(boost::posix_time::not_a_date_time); const std::string ×tr = packet->get("timestamp"); if(!timestr.empty()) { try { timestamp = boost::posix_time::from_iso_string(timestr); } catch(...) {} } boost::shared_ptr info; unsigned long gid = packet->get("gid"); if(gid) info = application->getUserManager()->getGroupInfo(gid, ×tamp); else info = application->getUserManager()->getGroupInfoByName(packet->get("name"), ×tamp); ret->setType("OK"); if(!timestamp.is_not_a_date_time()) ret->set("timestamp", boost::posix_time::to_iso_string(timestamp)); if(info) { ret->set("gid", info->getGid()); ret->set("name", info->getName()); } } void UserRequestHandlerGroup::handleGroupUserListRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { boost::posix_time::ptime timestamp(boost::posix_time::not_a_date_time); const std::string ×tr = packet->get("timestamp"); if(!timestr.empty()) { try { timestamp = boost::posix_time::from_iso_string(timestr); } catch(...) {} } boost::shared_ptr > users = application->getUserManager()->getGroupUserList(packet->get("gid"), ×tamp); ret->setType("OK"); if(!timestamp.is_not_a_date_time()) ret->set("timestamp", boost::posix_time::to_iso_string(timestamp)); Common::XmlPacket::List *list = ret->createList("users"); for(std::set::const_iterator user = users->begin(); user != users->end(); ++user) { Common::XmlPacket::List::iterator entry = list->addEntry(); entry->set("uid", *user); } } void UserRequestHandlerGroup::handleFullUserGroupListRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { boost::posix_time::ptime timestamp(boost::posix_time::not_a_date_time); const std::string ×tr = packet->get("timestamp"); if(!timestr.empty()) { try { timestamp = boost::posix_time::from_iso_string(timestr); } catch(...) {} } boost::shared_ptr > userGroups = application->getUserManager()->getFullUserGroupList(×tamp); ret->setType("OK"); if(!timestamp.is_not_a_date_time()) ret->set("timestamp", boost::posix_time::to_iso_string(timestamp)); if(userGroups) { Common::XmlPacket::List *list = ret->createList("userGroupList"); for(std::multimap::const_iterator userGroup = userGroups->begin(); userGroup != userGroups->end(); ++userGroup) { Common::XmlPacket::List::iterator entry = list->addEntry(); entry->set("uid", userGroup->first); entry->set("gid", userGroup->second); } } } void UserRequestHandlerGroup::handleUserInfoCheckRequest(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")); userInfo.setGid(packet->get("gid")); userInfo.setFullName(packet->get("fullName")); application->getUserManager()->addUser(userInfo); ret->setType("OK"); } void UserRequestHandlerGroup::handleUserUpdateRequest(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()->updateUser(packet->get("origUid"), userInfo); ret->setType("OK"); } void UserRequestHandlerGroup::handleUserDeleteRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { application->getUserManager()->deleteUser(packet->get("uid")); ret->setType("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")); ret->setType("OK"); } void UserRequestHandlerGroup::handleDeleteUserFromGroupRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { application->getUserManager()->deleteUserFromGroup(packet->get("uid"), packet->get("gid")); ret->setType("OK"); } void UserRequestHandlerGroup::handlePasswordSetRequest(boost::shared_ptr packet, Common::XmlPacket *ret, Common::Connection* /*connection*/) { application->getUserManager()->setPassword(packet->get("uid"), packet->get("password")); ret->setType("OK"); } UserRequestHandlerGroup::UserRequestHandlerGroup(Application *application0) : application(application0) { registerHandler("ListUsers", boost::bind(&UserRequestHandlerGroup::handleUserListRequest, this, _1, _2, _3)); registerHandler("GetUserInfo", boost::bind(&UserRequestHandlerGroup::handleUserInfoRequest, this, _1, _2, _3)); registerHandler("ListUserGroups", boost::bind(&UserRequestHandlerGroup::handleUserGroupListRequest, this, _1, _2, _3)); registerHandler("ListGroups", boost::bind(&UserRequestHandlerGroup::handleGroupListRequest, this, _1, _2, _3)); registerHandler("GetGroupInfo", boost::bind(&UserRequestHandlerGroup::handleGroupInfoRequest, this, _1, _2, _3)); registerHandler("ListGroupUsers", boost::bind(&UserRequestHandlerGroup::handleGroupUserListRequest, this, _1, _2, _3)); 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)); registerHandler("SetPassword", boost::bind(&UserRequestHandlerGroup::handlePasswordSetRequest, this, _1, _2, _3)); } } } }