summaryrefslogtreecommitdiffstats
path: root/src/Common/UserCache.cpp
blob: 63f72ddf345f8b99b967ee4b2239346c12e750e6 (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
/*
 * 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 <iostream>

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) {
    std::cerr << "Cached" << std::endl;
    return users;
  }
  else if(userException)
    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) {
  return backend->getUserGroupList(uid);
}


boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserCache::getGroupList() throw(Core::Exception) {
  boost::lock_guard<boost::shared_mutex> lock(mutex);

  if(groups) {
    std::cerr << "Cached" << std::endl;
    return groups;
  }
  else if(groupException)
    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 uid) throw(Core::Exception) {
  return backend->getGroupUserList(uid);
}

}
}