summaryrefslogtreecommitdiffstats
path: root/src/Server/UserListManager.cpp
blob: 04ba7d21104752e65043a61ce6508a1bc079b607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * UserListManager.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "UserListManager.h"
#include "Application.h"
#include "RequestHandlers/UserListRequestHandlerGroup.h"
#include "RequestHandlers/UserListUploadRequestHandler.h"
#include "RequestHandlers/UserListDiffUploadRequestHandler.h"

#include <Common/RequestManager.h>
#include <Common/StorageManager.h>
#include <Common/UserManager.h>
#include <Common/UserLists/UserList.h>
#include <Common/UserLists/Util.h>
#include <Core/ConfigManager.h>

namespace Mad {
namespace Server {

UserListManager::UserListManager(Application *application0) : application(application0), requestHandlerGroup(new RequestHandlers::UserListRequestHandlerGroup(application)) {
  application->getConfigManager()->registerConfigurable(this);
}

UserListManager::~UserListManager() {
  application->getRequestManager()->unregisterPacketType("UploadUserList");
  application->getRequestManager()->unregisterPacketType("UploadUserListDiff");
  application->getRequestManager()->unregisterRequestHandlerGroup(requestHandlerGroup);
  application->getConfigManager()->unregisterConfigurable(this);
}


void UserListManager::configure() {
  userLists = application->getStorageManager()->list("UserList");
  userListDiffs = application->getStorageManager()->list("UserListDiff");

  application->getRequestManager()->registerRequestHandlerGroup(requestHandlerGroup);
  application->getRequestManager()->registerPacketType<RequestHandlers::UserListUploadRequestHandler>("UploadUserList");
  application->getRequestManager()->registerPacketType<RequestHandlers::UserListDiffUploadRequestHandler>("UploadUserListDiff");
}

bool UserListManager::existsUserList(const Core::String &name) {
  return application->getStorageManager()->exists("UserList", name);
}

boost::shared_ptr<Common::UserLists::UserList> UserListManager::loadUserList(const Core::String &name) {
  boost::shared_ptr<Common::XmlData> data = application->getStorageManager()->load("UserList", name);

  if(!data)
    return boost::shared_ptr<Common::UserLists::UserList>();

  return Common::UserLists::Util::deserializeUserList(data.get());
}

void UserListManager::storeUserList(const Core::String &name, const Common::UserLists::UserList *list) {
  Common::XmlData data;

  Common::UserLists::Util::serializeUserList(list, &data);
  application->getStorageManager()->store("UserList", name, &data);
}

void UserListManager::copyUserList(const Core::String &fromName, const Core::String &toName) {
  application->getStorageManager()->copy("UserList", fromName, toName);
}

void UserListManager::renameUserList(const Core::String &fromName, const Core::String &toName) {
  application->getStorageManager()->rename("UserList", fromName, toName);
}

void UserListManager::removeUserList(const Core::String &name) {
  application->getStorageManager()->remove("UserList", name);
}


bool UserListManager::existsUserListDiff(const Core::String &name) {
  return application->getStorageManager()->exists("UserListDiff", name);
}

boost::shared_ptr<Common::UserLists::UserListDiff> UserListManager::loadUserListDiff(const Core::String &name) {
  boost::shared_ptr<Common::XmlData> data = application->getStorageManager()->load("UserListDiff", name);

  if(!data)
    return boost::shared_ptr<Common::UserLists::UserListDiff>();

  return Common::UserLists::Util::deserializeUserListDiff(data.get());
}

void UserListManager::storeUserListDiff(const Core::String &name, const Common::UserLists::UserListDiff *list) {
  Common::XmlData data;

  Common::UserLists::Util::serializeUserListDiff(list, &data);
  application->getStorageManager()->store("UserListDiff", name, &data);
}

void UserListManager::copyUserListDiff(const Core::String &fromName, const Core::String &toName) {
  application->getStorageManager()->copy("UserListDiff", fromName, toName);
}

void UserListManager::renameUserListDiff(const Core::String &fromName, const Core::String &toName) {
  application->getStorageManager()->rename("UserListDiff", fromName, toName);
}

void UserListManager::removeUserListDiff(const Core::String &name) {
  application->getStorageManager()->remove("UserListDiff", name);
}

boost::shared_ptr<Common::UserLists::UserList> UserListManager::getCurrentUserList() {
  boost::shared_ptr<Common::UserLists::UserList> list(new Common::UserLists::UserList);

  boost::shared_ptr<const std::map<unsigned long, Common::UserInfo> > userList = application->getUserManager()->getUserList();
  boost::shared_ptr<const std::map<unsigned long, Common::GroupInfo> > groupList = application->getUserManager()->getGroupList();

  for(std::map<unsigned long, Common::UserInfo>::const_iterator user = userList->begin(); user != userList->end(); ++user) {
    std::map<unsigned long, Common::GroupInfo>::const_iterator group = groupList->find(user->second.getGid());
    Core::String groupname;

    if(group != groupList->end()) {
      groupname = group->second.getName();
    }
    else {
      std::ostringstream stream;
      stream << user->second.getGid();
      groupname = stream.str().c_str();
    }

    Common::UserLists::UserListEntry entry(user->second.getUsername(), groupname);
    entry.setDetail("Full name", user->second.getFullName());

    list->addUser(entry);
  }

  return list;
}

}
}