summaryrefslogtreecommitdiffstats
path: root/src/Common/UserCache.cpp
blob: 1367041f9fb6134a5f0decb6665d3fd642fea6ac (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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * 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 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 "UserCache.h"
#include "Application.h"

namespace Mad {
namespace Common {

boost::shared_ptr<const std::map<unsigned long, UserInfo> > UserCache::getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  boost::posix_time::ptime newTime(userTime);

  try {
    boost::shared_ptr<const std::map<unsigned long, UserInfo> > newUsers = backend->getUserList(&newTime);

    if(newUsers) {
      users = newUsers;
      userException = Core::Exception();
      userTime = newTime;

      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));
    }
    else {
      if(users)
        application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_DEBUG, "Using cached user list");
      else
        application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_DEBUG, "Error cached");
    }
  }
  catch(Core::Exception e) {
    if(e.getErrorCode() != Core::Exception::NOT_AVAILABLE || !users) {
      users.reset();
      userException = e;
      userTime = newTime;
    }
  }

  if((!timestamp) || timestamp->is_not_a_date_time() || (*timestamp < userTime)) {
    if(timestamp)
      *timestamp = userTime;

    if(users)
      return users;
    else
      throw userException;
  }

  return boost::shared_ptr<const std::map<unsigned long, UserInfo> >();
}

boost::shared_ptr<const UserInfo> UserCache::getUserInfo(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  if(!getUserList(timestamp))
    return boost::shared_ptr<const UserInfo>();

  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, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  if(!getUserList(timestamp))
    return boost::shared_ptr<const UserInfo>();

  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, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  if(!getFullUserGroupList(timestamp))
    return boost::shared_ptr<const std::set<unsigned long> >();

  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(boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  boost::posix_time::ptime newTime(groupTime);

  try {
    boost::shared_ptr<const std::map<unsigned long, GroupInfo> > newGroups = backend->getGroupList(&newTime);

    if(newGroups) {
      groups = newGroups;
      groupException = Core::Exception();
      groupTime = newTime;

      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));
    }
    else {
      if(groups)
        application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_DEBUG, "Using cached group list");
      else
        application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_DEBUG, "Error cached");
    }
  }
  catch(Core::Exception e) {
    if(e.getErrorCode() != Core::Exception::NOT_AVAILABLE || !groups) {
      groups.reset();
      groupException = e;
      groupTime = newTime;
    }
  }

  if((!timestamp) || timestamp->is_not_a_date_time() || (*timestamp < groupTime)) {
    if(timestamp)
      *timestamp = groupTime;

    if(groups)
      return groups;
    else
      throw groupException;
  }

  return boost::shared_ptr<const std::map<unsigned long, GroupInfo> >();
}

boost::shared_ptr<const GroupInfo> UserCache::getGroupInfo(unsigned long gid, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  if(!getGroupList(timestamp))
    return boost::shared_ptr<const GroupInfo>();

  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, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  if(!getGroupList(timestamp))
    return boost::shared_ptr<const GroupInfo>();

  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, boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  if(!getFullUserGroupList(timestamp))
    return boost::shared_ptr<const std::set<unsigned long> >();

  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(boost::posix_time::ptime *timestamp) throw(Core::Exception) {
  boost::lock_guard<boost::recursive_mutex> lock(mutex);

  boost::posix_time::ptime newTime(userGroupTime);

  try {
    boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > newUserGroups = backend->getFullUserGroupList(&newTime);

    if(newUserGroups) {
      userGroups = newUserGroups;
      userGroupException = Core::Exception();
      userGroupTime = newTime;

      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));
    }
    else {
      if(userGroups)
        application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_DEBUG, "Using cached user-group table");
      else
        application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_DEBUG, "Error cached");
    }
  }
  catch(Core::Exception e) {
    if(e.getErrorCode() != Core::Exception::NOT_AVAILABLE || !userGroups) {
      userGroups.reset();
      userGroupException = e;
      userGroupTime = newTime;
    }
  }

  if((!timestamp) || timestamp->is_not_a_date_time() || (*timestamp < userTime)) {
    if(timestamp)
      *timestamp = userGroupTime;

    if(userGroups)
      return userGroups;
    else
      throw userGroupException;
  }

  return boost::shared_ptr<const std::multimap<unsigned long, unsigned long> >();
}

}
}