/* * NetworkUserBackend.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 "NetworkUserBackend.h" #include "../RequestManager.h" #include namespace Mad { namespace Common { namespace Backends { const std::string NetworkUserBackend::name("NetworkUserBackend"); void NetworkUserBackend::SimpleUserRequest::sendRequest() { XmlPacket packet; packet.setType(type); if(!timestamp.is_not_a_date_time()) packet.set("timestamp", boost::posix_time::to_iso_string(timestamp)); sendPacket(packet); } void NetworkUserBackend::IdUserRequest::sendRequest() { XmlPacket packet; packet.setType(type); packet.set(idType, id); if(!timestamp.is_not_a_date_time()) packet.set("timestamp", boost::posix_time::to_iso_string(timestamp)); sendPacket(packet); } void NetworkUserBackend::NameUserRequest::sendRequest() { XmlPacket packet; packet.setType(type); packet.set("name", name); if(!timestamp.is_not_a_date_time()) packet.set("timestamp", boost::posix_time::to_iso_string(timestamp)); sendPacket(packet); } void NetworkUserBackend::UserAddRequest::sendRequest() { XmlPacket packet; packet.setType(check ? "CheckUserInfo" : "AddUser"); packet.set("uid", userInfo.getUid()); packet.set("gid", userInfo.getGid()); packet.set("username", userInfo.getUsername()); packet.set("fullName", userInfo.getFullName()); sendPacket(packet); } void NetworkUserBackend::UserUpdateRequest::sendRequest() { XmlPacket packet; packet.setType("UpdateUser"); packet.set("origUid", uid); packet.set("uid", userInfo.getUid()); packet.set("gid", userInfo.getGid()); packet.set("username", userInfo.getUsername()); packet.set("fullName", userInfo.getFullName()); 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); packet.set("uid", uid); packet.set("gid", gid); sendPacket(packet); } void NetworkUserBackend::PasswordRequest::sendRequest() { XmlPacket packet; packet.setType("SetPassword"); packet.set("uid", uid); packet.set("password", password); sendPacket(packet); } boost::shared_ptr > NetworkUserBackend::getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new SimpleUserRequest(application, "ListUsers", timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } const XmlPacket::List *users = result.first->getList("users"); if(users) { boost::shared_ptr > userList(new std::map); for(XmlPacket::List::const_iterator user = users->begin(); user != users->end(); ++user) { UserInfo userInfo(user->get("uid"), user->get("username")); userInfo.setGid(user->get("gid")); userInfo.setFullName(user->get("fullName")); userList->insert(std::make_pair(userInfo.getUid(), userInfo)); } return userList; } return boost::shared_ptr >(); } boost::shared_ptr NetworkUserBackend::getUserInfo(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new IdUserRequest(application, "GetUserInfo", "uid", uid, timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } unsigned long uid2 = result.first->get("uid"); if(uid2) { boost::shared_ptr userInfo(new UserInfo(uid2, result.first->get("username"))); userInfo->setGid(result.first->get("gid")); userInfo->setFullName(result.first->get("fullName")); return userInfo; } return boost::shared_ptr(); } boost::shared_ptr NetworkUserBackend::getUserInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new NameUserRequest(application, "GetUserInfo", name, timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } unsigned long uid = result.first->get("uid"); if(uid) { boost::shared_ptr userInfo(new UserInfo(uid, result.first->get("username"))); userInfo->setGid(result.first->get("gid")); userInfo->setFullName(result.first->get("fullName")); return userInfo; } return boost::shared_ptr(); } boost::shared_ptr > NetworkUserBackend::getUserGroupList(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new IdUserRequest(application, "ListUserGroups", "uid", uid, timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } const XmlPacket::List *groups = result.first->getList("groups"); if(groups) { boost::shared_ptr > groupList(new std::set); for(XmlPacket::List::const_iterator group = groups->begin(); group != groups->end(); ++group) groupList->insert(group->get("gid")); return groupList; } return boost::shared_ptr >(); } boost::shared_ptr > NetworkUserBackend::getGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new SimpleUserRequest(application, "ListGroups", timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } const XmlPacket::List *groups = result.first->getList("groups"); if(groups) { boost::shared_ptr > groupList(new std::map); for(XmlPacket::List::const_iterator group = groups->begin(); group != groups->end(); ++group) { GroupInfo groupInfo(group->get("gid"), group->get("name")); groupList->insert(std::make_pair(groupInfo.getGid(), groupInfo)); } return groupList; } return boost::shared_ptr >(); } boost::shared_ptr NetworkUserBackend::getGroupInfo(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new IdUserRequest(application, "GetGroupInfo", "gid", gid, timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } unsigned long gid2 = result.first->get("gid"); if(gid2) return boost::shared_ptr(new GroupInfo(gid2, result.first->get("name"))); return boost::shared_ptr(); } boost::shared_ptr NetworkUserBackend::getGroupInfoByName(const std::string &name, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new NameUserRequest(application, "GetGroupInfo", name, timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } unsigned long gid = result.first->get("gid"); if(gid) return boost::shared_ptr(new GroupInfo(gid, result.first->get("name"))); return boost::shared_ptr(); } boost::shared_ptr > NetworkUserBackend::getGroupUserList(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new IdUserRequest(application, "ListGroupUsers", "gid", gid, timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } const XmlPacket::List *users = result.first->getList("users"); if(users) { boost::shared_ptr > userList(new std::set); for(XmlPacket::List::const_iterator user = users->begin(); user != users->end(); ++user) { userList->insert(user->get("uid")); } return userList; } return boost::shared_ptr >(); } boost::shared_ptr > NetworkUserBackend::getFullUserGroupList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new SimpleUserRequest(application, "GetFullUserGroupList", timestamp)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; if(timestamp) { try { *timestamp = boost::posix_time::from_iso_string(result.first->get("timestamp")); } catch(...) {} } const XmlPacket::List *list = result.first->getList("userGroupList"); if(list) { boost::shared_ptr > ret(new std::multimap); for(XmlPacket::List::const_iterator entry = list->begin(); entry != list->end(); ++entry) ret->insert(std::make_pair(entry->get("uid"), entry->get("gid"))); return ret; } return boost::shared_ptr >(); } 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, 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::updateUser(unsigned long uid, const Common::UserInfo &userInfo) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new UserUpdateRequest(application, uid, userInfo)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; } void NetworkUserBackend::deleteUser(unsigned long uid) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new IdUserRequest(application, "DeleteUser", "uid", uid)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) 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(); boost::shared_ptr request(new UserGroupRequest(application, "AddUserToGroup", uid, 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::deleteUserFromGroup(unsigned long uid, unsigned long gid) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new UserGroupRequest(application, "DeleteUserFromGroup", uid, 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::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { application->getThreadManager()->detach(); boost::shared_ptr request(new PasswordRequest(application, uid, password)); application->getRequestManager()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) throw result.second; } } } }