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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/*
* UserCache.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 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 <http://www.gnu.org/licenses/>.
*/
#include "UserCache.h"
#include "Application.h"
namespace Mad {
namespace Common {
boost::shared_ptr<const std::map<unsigned long, UserInfo> > UserCache::getUserList() throw(Core::Exception) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(users) {
application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached user list");
return users;
}
else if(userException) {
application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached");
throw userException;
}
try {
users = backend->getUserList();
userNames.reset(new std::map<std::string, unsigned long>);
for(std::map<unsigned long, UserInfo>::const_iterator user = users->begin(); user != users->end(); ++user)
userNames->insert(std::make_pair(user->second.getUsername(), user->first));
return users;
}
catch(Core::Exception e) {
userException = e;
throw userException;
}
}
boost::shared_ptr<const UserInfo> UserCache::getUserInfo(unsigned long uid) throw(Core::Exception) {
getUserList();
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(!users)
throw userException;
std::map<unsigned long, UserInfo>::const_iterator user = users->find(uid);
if(user == users->end())
throw Core::Exception(Core::Exception::NOT_FOUND);
return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
}
boost::shared_ptr<const UserInfo> UserCache::getUserInfoByName(const std::string &name) throw(Core::Exception) {
getUserList();
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(!users)
throw userException;
std::map<std::string, unsigned long>::const_iterator uid = userNames->find(name);
if(uid == userNames->end())
throw Core::Exception(Core::Exception::NOT_FOUND);
std::map<unsigned long, UserInfo>::const_iterator user = users->find(uid->second);
return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
}
boost::shared_ptr<const std::set<unsigned long> > UserCache::getUserGroupList(unsigned long uid) throw(Core::Exception) {
getFullUserGroupList();
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(!userGroups)
throw userGroupException;
std::pair<std::multimap<unsigned long, unsigned long>::const_iterator, std::multimap<unsigned long, unsigned long>::const_iterator> range = userGroups->equal_range(uid);
boost::shared_ptr<std::set<unsigned long> > groups(new std::set<unsigned long>);
for(std::multimap<unsigned long, unsigned long>::const_iterator group = range.first; group != range.second; ++group)
groups->insert(group->second);
return groups;
}
boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserCache::getGroupList() throw(Core::Exception) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(groups) {
application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached group list");
return groups;
}
else if(groupException) {
application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached");
throw groupException;
}
try {
groups = backend->getGroupList();
groupNames.reset(new std::map<std::string, unsigned long>);
for(std::map<unsigned long, GroupInfo>::const_iterator group = groups->begin(); group != groups->end(); ++group)
groupNames->insert(std::make_pair(group->second.getName(), group->first));
return groups;
}
catch(Core::Exception e) {
groupException = e;
throw groupException;
}
}
boost::shared_ptr<const GroupInfo> UserCache::getGroupInfo(unsigned long gid) throw(Core::Exception) {
getGroupList();
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(!groups)
throw groupException;
std::map<unsigned long, GroupInfo>::const_iterator group = groups->find(gid);
if(group == groups->end())
throw Core::Exception(Core::Exception::NOT_FOUND);
return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
}
boost::shared_ptr<const GroupInfo> UserCache::getGroupInfoByName(const std::string &name) throw(Core::Exception) {
getGroupList();
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(!groups)
throw groupException;
std::map<std::string, unsigned long>::const_iterator gid = groupNames->find(name);
if(gid == groupNames->end())
throw Core::Exception(Core::Exception::NOT_FOUND);
std::map<unsigned long, GroupInfo>::const_iterator group = groups->find(gid->second);
return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
}
boost::shared_ptr<const std::set<unsigned long> > UserCache::getGroupUserList(unsigned long gid) throw(Core::Exception) {
getFullUserGroupList();
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(!userGroups)
throw userGroupException;
std::pair<std::multimap<unsigned long, unsigned long>::iterator, std::multimap<unsigned long, unsigned long>::iterator> range = groupUsers->equal_range(gid);
boost::shared_ptr<std::set<unsigned long> > users(new std::set<unsigned long>);
for(std::multimap<unsigned long, unsigned long>::iterator user = range.first; user != range.second; ++user)
users->insert(user->second);
return users;
}
boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > UserCache::getFullUserGroupList() throw(Core::Exception) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
if(userGroups) {
application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Using cached user-group table");
return userGroups;
}
else if(userGroupException) {
application->log(Core::LoggerBase::USER, Core::LoggerBase::DEBUG, "Error cached");
throw userGroupException;
}
try {
userGroups = backend->getFullUserGroupList();
groupUsers.reset(new std::multimap<unsigned long, unsigned long>);
for(std::multimap<unsigned long, unsigned long>::const_iterator usergroup = userGroups->begin(); usergroup != userGroups->end(); ++usergroup)
groupUsers->insert(std::make_pair(usergroup->second, usergroup->first));
return userGroups;
}
catch(Core::Exception e) {
userGroupException = e;
throw userGroupException;
}
}
}
}
|