/* * 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 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 General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "NetworkUserBackend.h" #include "../RequestManager.h" #include namespace Mad { namespace Common { namespace Backends { void NetworkUserBackend::SimpleUserRequest::sendRequest() { Common::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() { Common::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() { Common::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); } boost::shared_ptr > NetworkUserBackend::getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { }*/ /*void NetworkUserBackend::addUser(const UserInfo &userInfo) throw(Core::Exception) { }*/ } } }