- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List

# AccountsDatabase.h

    00001 /*
    00002  *  Phusion Passenger - http://www.modrails.com/
    00003  *  Copyright (c) 2010 Phusion
    00004  *
    00005  *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
    00006  *
    00007  *  Permission is hereby granted, free of charge, to any person obtaining a copy
    00008  *  of this software and associated documentation files (the "Software"), to deal
    00009  *  in the Software without restriction, including without limitation the rights
    00010  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00011  *  copies of the Software, and to permit persons to whom the Software is
    00012  *  furnished to do so, subject to the following conditions:
    00013  *
    00014  *  The above copyright notice and this permission notice shall be included in
    00015  *  all copies or substantial portions of the Software.
    00016  *
    00017  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00018  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00019  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00020  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00021  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00022  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00023  *  THE SOFTWARE.
    00024  */
    00025 #ifndef _PASSENGER_ACCOUNTS_DATABASE_H_
    00026 #define _PASSENGER_ACCOUNTS_DATABASE_H_
    00027
    00028 #include <string>
    00029 #include <map>
    00030 #include <boost/thread.hpp>
    00031 #include <boost/shared_ptr.hpp>
    00032 #include "Account.h"
    00033 #include "ServerInstanceDir.h"
    00034 #include "StaticString.h"
    00035
    00036 /* This source file follows the security guidelines written in Account.h. */
    00037
    00038 namespace Passenger {
    00039
    00040 using namespace std;
    00041 using namespace boost;
    00042
    00043 class AccountsDatabase;
    00044 typedef shared_ptr<AccountsDatabase> AccountsDatabasePtr;
    00045
    00046
    00047 class AccountsDatabase {
    00048 private:
    00049         mutable boost::mutex lock;
    00050         map<string, AccountPtr> accounts;
    00051         unsigned int uniqueNumber;
    00052
    00053 public:
    00054         static AccountsDatabasePtr createDefault(const ServerInstanceDir::GenerationPtr &generation,
    00055                                                  bool userSwitching, const string &defaultUser,
    00056                                                  const string &defaultGroup);
    00057
    00058         AccountsDatabase() {
    00059                 uniqueNumber = 0;
    00060         }
    00061
    00062         unsigned int size() const {
    00063                 lock_guard<boost::mutex> l(lock);
    00064                 return accounts.size();
    00065         }
    00066
    00067         vector<string> listUsernames() const {
    00068                 map<string, AccountPtr>::const_iterator it;
    00069                 vector<string> result;
    00070
    00071                 for (it = accounts.begin(); it != accounts.end(); it++) {
    00072                         result.push_back(it->second->getUsername());
    00073                 }
    00074                 return result;
    00075         }
    00076
    00077         AccountPtr add(const string &username, const string &passwordOrHash, bool hashGiven, int rights = Account::ALL) {
    00078                 AccountPtr account(new Account(username, passwordOrHash, hashGiven, rights));
    00079                 lock_guard<boost::mutex> l(lock);
    00080                 accounts[username] = account;
    00081                 return account;
    00082         }
    00083
    00084         const AccountPtr get(const string &username) const {
    00085                 lock_guard<boost::mutex> l(lock);
    00086                 map<string, AccountPtr>::const_iterator it = accounts.find(username);
    00087                 if (it == accounts.end()) {
    00088                         return AccountPtr();
    00089                 } else {
    00090                         return it->second;
    00091                 }
    00092         }
    00093
    00094         AccountPtr authenticate(const string &username, const StaticString &userSuppliedPassword) const {
    00095                 lock_guard<boost::mutex> l(lock);
    00096                 map<string, AccountPtr>::const_iterator it = accounts.find(username);
    00097                 if (it == accounts.end()) {
    00098                         return AccountPtr();
    00099                 } else {
    00100                         AccountPtr account = it->second;
    00101                         if (account->checkPasswordOrHash(userSuppliedPassword)) {
    00102                                 return account;
    00103                         } else {
    00104                                 return AccountPtr();
    00105                         }
    00106                 }
    00107         }
    00108
    00109         bool remove(const string &username) {
    00110                 lock_guard<boost::mutex> l(lock);
    00111                 map<string, AccountPtr>::iterator it = accounts.find(username);
    00112                 if (it == accounts.end()) {
    00113                         return false;
    00114                 } else {
    00115                         accounts.erase(it);
    00116                         return true;
    00117                 }
    00118         }
    00119
    00120         unsigned int getUniqueNumber() {
    00121                 lock_guard<boost::mutex> l(lock);
    00122                 unsigned int result = uniqueNumber;
    00123                 uniqueNumber++;
    00124                 return result;
    00125         }
    00126 };
    00127
    00128 } // namespace Passenger
    00129
    00130 #endif /* _PASSENGER_ACCOUNTS_DATABASE_H_ */