summaryrefslogtreecommitdiffstats
path: root/src/Common/UserCache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common/UserCache.cpp')
-rw-r--r--src/Common/UserCache.cpp153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/Common/UserCache.cpp b/src/Common/UserCache.cpp
new file mode 100644
index 0000000..63f72dd
--- /dev/null
+++ b/src/Common/UserCache.cpp
@@ -0,0 +1,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);
+}
+
+}
+}