From 0eddc28a331437ef95a60418ed1fc6de4e9b63c1 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 23 Sep 2009 09:44:02 +0200 Subject: UserListManager: Moved module into libCommon and libServer --- src/Common/CMakeLists.txt | 5 ++ src/Common/UserLists/UserList.h | 40 ++++++++++++ src/Common/UserLists/UserListDiff.cpp | 46 ++++++++++++++ src/Common/UserLists/UserListDiff.h | 88 +++++++++++++++++++++++++++ src/Common/UserLists/UserListEntry.h | 93 ++++++++++++++++++++++++++++ src/Common/UserLists/Util.cpp | 111 ++++++++++++++++++++++++++++++++++ src/Common/UserLists/Util.h | 55 +++++++++++++++++ 7 files changed, 438 insertions(+) create mode 100644 src/Common/UserLists/UserList.h create mode 100644 src/Common/UserLists/UserListDiff.cpp create mode 100644 src/Common/UserLists/UserListDiff.h create mode 100644 src/Common/UserLists/UserListEntry.h create mode 100644 src/Common/UserLists/Util.cpp create mode 100644 src/Common/UserLists/Util.h (limited to 'src/Common') diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index dd12a36..a99f9be 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -18,6 +18,11 @@ mad_library(Common Requests/SimpleRequest.cpp Requests/SimpleRequest.h Requests/StatusRequest.h + UserLists/UserList.h + UserLists/UserListDiff.cpp UserLists/UserListDiff.h + UserLists/UserListEntry.h + UserLists/Util.cpp UserLists/Util.h + Application.cpp Application.h AuthBackend.h AuthContext.h diff --git a/src/Common/UserLists/UserList.h b/src/Common/UserLists/UserList.h new file mode 100644 index 0000000..e80abaf --- /dev/null +++ b/src/Common/UserLists/UserList.h @@ -0,0 +1,40 @@ +/* + * UserList.h + * + * 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 . + */ + +#ifndef MAD_COMMON_USERLISTS_USERLIST_H_ +#define MAD_COMMON_USERLISTS_USERLIST_H_ + +#include "UserListEntry.h" + +#include + +namespace Mad { +namespace Common { +namespace UserLists { + +class UserList : public std::list { + public: + UserList() {} +}; + +} +} +} + +#endif /* MAD_COMMON_USERLISTS_USERLIST_H_ */ diff --git a/src/Common/UserLists/UserListDiff.cpp b/src/Common/UserLists/UserListDiff.cpp new file mode 100644 index 0000000..e763afe --- /dev/null +++ b/src/Common/UserLists/UserListDiff.cpp @@ -0,0 +1,46 @@ +/* + * UserListDiff.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 "UserListDiff.h" +#include "UserList.h" + +namespace Mad { +namespace Common { +namespace UserLists { + +UserListDiff::UserListDiff(const UserList *oldList, const UserList *newList) { + for(UserList::const_iterator user = oldList->begin(); user != oldList->end(); ++user) + deletedUsers.insert(*user); + + for(UserList::const_iterator user = newList->begin(); user != newList->end(); ++user) { + std::set::const_iterator it = deletedUsers.find(*user); + + if(it != deletedUsers.end()) { + deletedUsers.erase(it); + unchangedUsers.insert(*user); + } + else { + addedUsers.insert(*user); + } + } +} + +} +} +} diff --git a/src/Common/UserLists/UserListDiff.h b/src/Common/UserLists/UserListDiff.h new file mode 100644 index 0000000..41b8e81 --- /dev/null +++ b/src/Common/UserLists/UserListDiff.h @@ -0,0 +1,88 @@ +/* + * UserListDiff.h + * + * 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 . + */ + +#ifndef MAD_COMMON_USERLISTS_USERLISTDIFF_H_ +#define MAD_COMMON_USERLISTS_USERLISTDIFF_H_ + +#include "../export.h" + +#include "UserListEntry.h" +#include + +#include + +namespace Mad { +namespace Common { +namespace UserLists { + +class UserList; + +class MAD_COMMON_EXPORT UserListDiff { + private: + std::set addedUsers; + std::set deletedUsers; + std::set unchangedUsers; + + public: + UserListDiff() {} + UserListDiff(const UserList *oldList, const UserList *newList); + + void invert() { + addedUsers.swap(deletedUsers); + } + + const std::set& getAddedUsers() const { + return addedUsers; + } + + const std::set& getDeletedUsers() const { + return deletedUsers; + } + + const std::set& getUnchangedUsers() const { + return unchangedUsers; + } + + bool insertAddedUser(UserListEntry user) { + if(deletedUsers.count(user) || unchangedUsers.count(user)) + return false; + + return addedUsers.insert(user).second; + } + + bool insertDeletedUser(UserListEntry user) { + if(addedUsers.count(user) || unchangedUsers.count(user)) + return false; + + return deletedUsers.insert(user).second; + } + + bool insertUnchangedUser(UserListEntry user) { + if(addedUsers.count(user) || deletedUsers.count(user)) + return false; + + return unchangedUsers.insert(user).second; + } +}; + +} +} +} + +#endif /* MAD_COMMON_USERLISTS_USERLISTDIFF_H_ */ diff --git a/src/Common/UserLists/UserListEntry.h b/src/Common/UserLists/UserListEntry.h new file mode 100644 index 0000000..8642275 --- /dev/null +++ b/src/Common/UserLists/UserListEntry.h @@ -0,0 +1,93 @@ +/* + * UserListEntry.h + * + * 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 . + */ + +#ifndef MAD_COMMON_USERLISTS_USERLISTENTRY_H_ +#define MAD_COMMON_USERLISTS_USERLISTENTRY_H_ + +#include +#include +#include + + +namespace Mad { +namespace Common { +namespace UserLists { + +class UserListEntry { + private: + std::string name; + std::string group; + + std::map details; + + public: + UserListEntry(const std::string &name0 = std::string(), const std::string &group0 = std::string()) : name(name0), group(group0) {} + + const std::string& getName() const { + return name; + } + + void setName(const std::string &newName) { + name = newName; + } + + const std::string& getGroup() const { + return group; + } + + void setGroup(const std::string &newGroup) { + group = newGroup; + } + + std::set getDetailList() const { + std::set ret; + + for(std::map::const_iterator it = details.begin(); it != details.end(); ++it) + ret.insert(it->first); + + return ret; + } + + std::string getDetail(const std::string &name) const { + std::map::const_iterator it = details.find(name); + if(it != details.end()) + return it->second; + else + return std::string(); + } + + void setDetail(const std::string &name, const std::string &value) { + details.erase(name); + details.insert(std::make_pair(name, value)); + } + + void unsetDetail(const std::string &name) { + details.erase(name); + } + + bool operator<(const UserListEntry o) const { + return (name < o.name); + } +}; + +} +} +} + +#endif /* MAD_COMMON_USERLISTS_USERLISTENTRY_H_ */ diff --git a/src/Common/UserLists/Util.cpp b/src/Common/UserLists/Util.cpp new file mode 100644 index 0000000..812c2c7 --- /dev/null +++ b/src/Common/UserLists/Util.cpp @@ -0,0 +1,111 @@ +/* + * Util.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 "Util.h" +#include "UserList.h" +#include "UserListDiff.h" + +namespace Mad { +namespace Common { +namespace UserLists { + +void Util::serializeUserListEntry(const UserListEntry *user, XmlData::List::iterator entry) { + std::set details = user->getDetailList(); + for(std::set::iterator detail = details.begin(); detail != details.end(); ++detail) + entry->set(*detail, user->getDetail(*detail)); + + entry->set("name", user->getName()); + entry->set("group", user->getGroup()); +} + +UserListEntry Util::deserializeUserListEntry(XmlData::List::const_iterator entry) { + UserListEntry user(entry->get("name"), entry->get("group")); + + std::set details = entry->getChildren(); + for(std::set::iterator detail = details.begin(); detail != details.end(); ++detail) { + if(*detail == "user" || *detail == "group") + continue; + + user.setDetail(*detail, entry->get(*detail)); + } + + return user; +} + +void Util::serializeUserList(const UserList *list, XmlData *data) { + XmlData::List *userList = data->createList("users"); + + for(UserList::const_iterator user = list->begin(); user != list->end(); ++user) + serializeUserListEntry(&*user, userList->addEntry()); +} + +boost::shared_ptr Util::deserializeUserList(const XmlData *data) { + boost::shared_ptr users(new UserList); + + const XmlData::List *userList = data->getList("users"); + + if(userList) { + for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) + users->push_back(deserializeUserListEntry(user)); + } + + return users; +} + +void Util::serializeUserListDiff(const UserListDiff *diff, XmlData *data) { + XmlData::List *userList = data->createList("addedUsers"); + for(std::set::const_iterator user = diff->getAddedUsers().begin(); user != diff->getAddedUsers().end(); ++user) + serializeUserListEntry(&*user, userList->addEntry()); + + userList = data->createList("deletedUsers"); + for(std::set::const_iterator user = diff->getDeletedUsers().begin(); user != diff->getDeletedUsers().end(); ++user) + serializeUserListEntry(&*user, userList->addEntry()); + + userList = data->createList("unchangedUsers"); + for(std::set::const_iterator user = diff->getUnchangedUsers().begin(); user != diff->getUnchangedUsers().end(); ++user) + serializeUserListEntry(&*user, userList->addEntry()); +} + +boost::shared_ptr Util::deserializeUserListDiff(const XmlData *data) { + boost::shared_ptr diff(new UserListDiff); + + const XmlData::List *userList = data->getList("addedUsers"); + if(userList) { + for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) + diff->insertAddedUser(deserializeUserListEntry(user)); + } + + userList = data->getList("deletedUsers"); + if(userList) { + for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) + diff->insertDeletedUser(deserializeUserListEntry(user)); + } + + userList = data->getList("unchangedUsers"); + if(userList) { + for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) + diff->insertUnchangedUser(deserializeUserListEntry(user)); + } + + return diff; +} + +} +} +} diff --git a/src/Common/UserLists/Util.h b/src/Common/UserLists/Util.h new file mode 100644 index 0000000..df7f203 --- /dev/null +++ b/src/Common/UserLists/Util.h @@ -0,0 +1,55 @@ +/* + * Util.h + * + * 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 . + */ + +#ifndef MAD_COMMON_USERLISTS_UTIL_H_ +#define MAD_COMMON_USERLISTS_UTIL_H_ + +#include "../export.h" + +#include "../XmlData.h" +#include + +namespace Mad { +namespace Common { +namespace UserLists { + +class UserList; +class UserListDiff; +class UserListEntry; + +class MAD_COMMON_EXPORT Util { + private: + static void serializeUserListEntry(const UserListEntry *user, XmlData::List::iterator entry); + static UserListEntry deserializeUserListEntry(XmlData::List::const_iterator entry); + + Util(); + + public: + static void serializeUserList(const UserList *list, XmlData *data); + static boost::shared_ptr deserializeUserList(const XmlData *data); + + static void serializeUserListDiff(const UserListDiff *diff, XmlData *data); + static boost::shared_ptr deserializeUserListDiff(const XmlData *data); +}; + +} +} +} + +#endif /* MAD_COMMON_USERLISTS_UTIL_H_ */ -- cgit v1.2.3