00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _PASSENGER_ACCOUNT_H_
00026 #define _PASSENGER_ACCOUNT_H_
00027
00028 #include <string>
00029 #include <vector>
00030 #include <boost/shared_ptr.hpp>
00031 #include "StaticString.h"
00032 #include "Exceptions.h"
00033 #include "Utils/StrIntUtils.h"
00034
00035 namespace Passenger {
00036
00037 using namespace boost;
00038 using namespace std;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 class Account {
00060 public:
00061 enum Rights {
00062 ALL = ~0,
00063 NONE = 0,
00064
00065
00066 GET = 1 << 0,
00067 CLEAR = 1 << 1,
00068 DETACH = 1 << 2,
00069 GET_PARAMETERS = 1 << 3,
00070 SET_PARAMETERS = 1 << 4,
00071 INSPECT_BASIC_INFO = 1 << 5,
00072 INSPECT_SENSITIVE_INFO = 1 << 6,
00073
00074
00075
00076
00077 INSPECT_BACKTRACES = 1 << 8,
00078
00079
00080 EXIT = 1 << 31
00081 };
00082
00083 private:
00084 string username;
00085 string passwordOrHash;
00086 bool hashGiven;
00087 Rights rights;
00088
00089 public:
00090
00091
00092
00093 static Rights parseRightsString(const string &str, int defaultValue = NONE) {
00094 vector<string> rights_vec;
00095 vector<string>::const_iterator it;
00096 int result = defaultValue;
00097
00098 split(str, ',', rights_vec);
00099 for (it = rights_vec.begin(); it != rights_vec.end(); it++) {
00100 if (*it == "all") {
00101 result = ALL;
00102 } else if (*it == "none") {
00103 result = NONE;
00104
00105 } else if (*it == "get") {
00106 result |= GET;
00107 } else if (*it == "clear") {
00108 result |= CLEAR;
00109 } else if (*it == "detach") {
00110 result |= DETACH;
00111 } else if (*it == "get_parameters") {
00112 result |= GET_PARAMETERS;
00113 } else if (*it == "set_parameters") {
00114 result |= SET_PARAMETERS;
00115 } else if (*it == "inspect_basic_info") {
00116 result |= INSPECT_BASIC_INFO;
00117 } else if (*it == "inspect_sensitive_info") {
00118 result |= INSPECT_SENSITIVE_INFO;
00119
00120 } else if (*it == "inspect_backtraces") {
00121 result |= INSPECT_BACKTRACES;
00122
00123 } else if (*it == "exit") {
00124 result |= EXIT;
00125
00126 } else if (*it != "") {
00127 throw ArgumentException("Unknown right '" + *it + "'.");
00128 }
00129 }
00130
00131 return (Rights) result;
00132 }
00133
00134 Account(const string &username, const string &passwordOrHash, bool hashGiven, int rights = ALL) {
00135 this->username = username;
00136 this->passwordOrHash = passwordOrHash;
00137 this->hashGiven = hashGiven;
00138 this->rights = (Rights) rights;
00139 }
00140
00141 bool checkPasswordOrHash(const StaticString &userSuppliedPassword) const {
00142 if (hashGiven) {
00143 return passwordOrHash == createHash(userSuppliedPassword);
00144 } else {
00145 return userSuppliedPassword == passwordOrHash;
00146 }
00147 }
00148
00149 bool hasRights(int rights) const {
00150 return this->rights & rights;
00151 }
00152
00153 void setRights(int rights) {
00154 this->rights = (Rights) rights;
00155 }
00156
00157 string getUsername() const {
00158 return username;
00159 }
00160
00161 string getRawPassword() const {
00162 return passwordOrHash;
00163 }
00164
00165 static string createHash(const StaticString &userSuppliedPassword) {
00166
00167 return userSuppliedPassword;
00168 }
00169 };
00170
00171 typedef shared_ptr<Account> AccountPtr;
00172
00173 }
00174
00175 #endif