/* * UserCache.cpp * * Copyright (C) 2009 Matthias Schiffer * * 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 . */ #include "UserCache.h" #include "Application.h" namespace Mad { namespace Common { boost::shared_ptr > UserCache::getUserList() throw(Core::Exception) { boost::lock_guard 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); for(std::map::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 UserCache::getUserInfo(unsigned long uid) throw(Core::Exception) { getUserList(); boost::lock_guard lock(mutex); if(!users) throw userException; std::map::const_iterator user = users->find(uid); if(user == users->end()) throw Core::Exception(Core::Exception::NOT_FOUND); return boost::shared_ptr(new UserInfo(user->second)); } boost::shared_ptr UserCache::getUserInfoByName(const std::string &name) throw(Core::Exception) { getUserList(); boost::lock_guard lock(mutex); if(!users) throw userException; std::map::const_iterator uid = userNames->find(name); if(uid == userNames->end()) throw Core::Exception(Core::Exception::NOT_FOUND); std::map::const_iterator user = users->find(uid->second); return boost::shared_ptr(new UserInfo(user->second)); } boost::shared_ptr > UserCache::getUserGroupList(unsigned long uid) throw(Core::Exception) { getFullUserGroupList(); boost::lock_guard lock(mutex); if(!userGroups) throw userGroupException; std::pair::const_iterator, std::multimap::const_iterator> range = userGroups->equal_range(uid); boost::shared_ptr > groups(new std::set); for(std::multimap::const_iterator group = range.first; group != range.second; ++group) groups->insert(group->second); return groups; } boost::shared_ptr > UserCache::getGroupList() throw(Core::Exception) { boost::lock_guard 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); for(std::map::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 UserCache::getGroupInfo(unsigned long gid) throw(Core::Exception) { getGroupList(); boost::lock_guard lock(mutex); if(!groups) throw groupException; std::map::const_iterator group = groups->find(gid); if(group == groups->end()) throw Core::Exception(Core::Exception::NOT_FOUND); return boost::shared_ptr(new GroupInfo(group->second)); } boost::shared_ptr UserCache::getGroupInfoByName(const std::string &name) throw(Core::Exception) { getGroupList(); boost::lock_guard lock(mutex); if(!groups) throw groupException; std::map::const_iterator gid = groupNames->find(name); if(gid == groupNames->end()) throw Core::Exception(Core::Exception::NOT_FOUND); std::map::const_iterator group = groups->find(gid->second); return boost::shared_ptr(new GroupInfo(group->second)); } boost::shared_ptr > UserCache::getGroupUserList(unsigned long gid) throw(Core::Exception) { getFullUserGroupList(); boost::lock_guard lock(mutex); if(!userGroups) throw userGroupException; std::pair::iterator, std::multimap::iterator> range = groupUsers->equal_range(gid); boost::shared_ptr > users(new std::set); for(std::multimap::iterator user = range.first; user != range.second; ++user) users->insert(user->second); return users; } boost::shared_ptr > UserCache::getFullUserGroupList() throw(Core::Exception) { boost::lock_guard 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); for(std::multimap::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; } } } }