/* * UserManager.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 "UserManager.h" #include "UserBackend.h" #include namespace Mad { namespace Common { bool UserManager::Compare::operator() (boost::shared_ptr b1, boost::shared_ptr b2) { if(b1->getPriority() == b2->getPriority()) return (b1.get() > b2.get()); else return (b1->getPriority() > b2->getPriority()); } boost::shared_ptr > UserManager::getUserList() throw(Core::Exception) { { boost::shared_lock lock(mutex); if(caching) { if(users) return users; else if(userException) throw *userException; } } Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::shared_ptr > ret; { boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { ret = (*backend)->getUserList(); break; } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } } { boost::upgrade_lock lock(mutex); if(caching) { if(ret) { boost::upgrade_to_unique_lock upgradeLock(lock); users = ret; boost::shared_ptr > names(new std::map); for(std::map::const_iterator user = users->begin(); user != users->end(); ++user) names->insert(std::make_pair(user->second.getUsername(), user->first)); userNames = names; } else userException.reset(new Core::Exception(e)); } } if(ret) return ret; else throw e; } boost::shared_ptr UserManager::getUserInfo(unsigned long uid) throw(Core::Exception) { { boost::shared_lock lock(mutex); if(caching) { if(!users && !userException) { lock.unlock(); getUserList(); lock.lock(); } if(caching) { if(users) { std::map::const_iterator user = users->find(uid); if(user != users->end()) return boost::shared_ptr(new UserInfo(user->second)); else throw Core::Exception(Core::Exception::NOT_FOUND); } else if(userException) throw *userException; } } } Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getUserInfo(uid); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } boost::shared_ptr UserManager::getUserInfoByName(const std::string &name) throw(Core::Exception) { { boost::shared_lock lock(mutex); if(caching) { if(!users && !userException) { lock.unlock(); getUserList(); lock.lock(); } if(caching) { if(userNames) { std::map::const_iterator user = userNames->find(name); if(user != userNames->end()) return boost::shared_ptr(new UserInfo(users->find(user->second)->second)); else throw Core::Exception(Core::Exception::NOT_FOUND); } else if(userException) throw *userException; } } } Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getUserInfoByName(name); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } boost::shared_ptr > UserManager::getUserGroupList(unsigned long uid) throw(Core::Exception) { Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getUserGroupList(uid); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } boost::shared_ptr > UserManager::getGroupList() throw(Core::Exception) { { boost::shared_lock lock(mutex); if(caching) { if(groups) return groups; else if(groupException) throw *groupException; } } Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::shared_ptr > ret; { boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { ret = (*backend)->getGroupList(); break; } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } } { boost::upgrade_lock lock(mutex); if(caching) { if(ret) { boost::upgrade_to_unique_lock upgradeLock(lock); groups = ret; boost::shared_ptr > names(new std::map); for(std::map::const_iterator group = groups->begin(); group != groups->end(); ++group) names->insert(std::make_pair(group->second.getName(), group->first)); groupNames = names; } else groupException.reset(new Core::Exception(e)); } } if(ret) return ret; else throw e; } boost::shared_ptr UserManager::getGroupInfo(unsigned long gid) throw(Core::Exception) { { boost::shared_lock lock(mutex); if(caching) { if(!groups && !groupException) { lock.unlock(); getGroupList(); lock.lock(); } if(caching) { if(groups) { std::map::const_iterator group = groups->find(gid); if(group != groups->end()) return boost::shared_ptr(new GroupInfo(group->second)); else throw Core::Exception(Core::Exception::NOT_FOUND); } else if(groupException) throw *groupException; } } } Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getGroupInfo(gid); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } boost::shared_ptr UserManager::getGroupInfoByName(const std::string &name) throw(Core::Exception) { { boost::shared_lock lock(mutex); if(caching) { if(!groups && !groupException) { lock.unlock(); getGroupList(); lock.lock(); } if(caching) { if(groupNames) { std::map::const_iterator group = groupNames->find(name); if(group != groupNames->end()) return boost::shared_ptr(new GroupInfo(groups->find(group->second)->second)); else throw Core::Exception(Core::Exception::NOT_FOUND); } else if(groupException) throw *groupException; } } } Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getGroupInfoByName(name); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } boost::shared_ptr > UserManager::getGroupUserList(unsigned long gid) throw(Core::Exception) { Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { return (*backend)->getGroupUserList(gid); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) { Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->setPassword(uid, password); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) { Core::Exception e(Core::Exception::NOT_IMPLEMENTED); boost::lock_guard backendLock(backendMutex); for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { try { (*backend)->addUser(userInfo); } catch(Core::Exception e2) { if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) e = e2; } } throw e; } } }