Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "sample_user_handler.hpp"
00017
00018 #include "config.hpp"
00019 #include "serialization/string_utils.hpp"
00020 #include "util.hpp"
00021
00022 #include <iostream>
00023
00024 suh::suh(config c)
00025 : user_expiration_(0)
00026 , users_()
00027 {
00028
00029 if(c["user_expiration"].empty()) {
00030 user_expiration_ = 60;
00031 } else {
00032 try {
00033 user_expiration_ = lexical_cast_default<int>(c["user_expiration"]);
00034 } catch (bad_lexical_cast) {
00035 std::cerr << "Bad lexical cast reading 'user_expiration', using default value.\n";
00036 user_expiration_ = 60;
00037 }
00038 }
00039 }
00040
00041 void suh::add_user(const std::string& name, const std::string& mail, const std::string& password) {
00042 if(user_exists(name)) throw error("This nickname is already registered");
00043
00044 users_.insert(std::pair<std::string, user>(name, user()));
00045
00046 set_password(name, password);
00047 set_mail(name, mail);
00048
00049 user_logged_in(name);
00050 }
00051
00052 void suh::remove_user(const std::string& name) {
00053 if(!user_exists(name)) throw error("This nickname is not registered");
00054
00055 users_.erase(name);
00056 }
00057
00058 bool suh::user_exists(const std::string& name) {
00059 std::map<std::string,user>::const_iterator u = users_.find(name);
00060
00061 return (u != users_.end());
00062 }
00063
00064 bool suh::user_is_active(const std::string& ) {
00065
00066 return true;
00067 }
00068
00069 std::vector<std::string> suh::users() {
00070 std::vector<std::string> users;
00071 for(std::map<std::string,user>::const_iterator u = users_.begin(); u != users_.end(); ++u) {
00072 users.push_back(u->first);
00073 }
00074 return users;
00075 }
00076
00077 void suh::set_user_detail(const std::string& user, const std::string& detail, const std::string& value) {
00078 if(detail == "mail") {
00079 set_mail(user, value);
00080 } else if (detail == "password") {
00081 set_password(user, value);
00082 } else if (detail == "realname") {
00083 set_realname(user, value);
00084 } else {
00085 throw error("Invalid usersdetail '" + detail + "'. Valid details are: " + get_valid_details());
00086 }
00087 }
00088
00089 std::string suh::get_valid_details() {
00090 return "'mail', 'password', 'realname';";
00091 }
00092
00093
00094 bool suh::user_is_moderator(const std::string& name) {
00095 if(!user_exists(name)) return false;
00096 return users_[name].is_moderator;
00097 }
00098
00099 void suh::set_is_moderator(const std::string& name, const bool& is_moderator) {
00100 if(!user_exists(name)) return;
00101 users_[name].is_moderator = is_moderator;
00102 }
00103
00104 void suh::set_mail(const std::string& user, const std::string& mail) {
00105 check_mail(mail);
00106 users_[user].mail = mail;
00107 }
00108
00109 void suh::set_password(const std::string& user, const std::string& password) {
00110 check_password(password);
00111 users_[user].password = password;
00112 }
00113
00114 void suh::set_realname(const std::string& user, const std::string& realname) {
00115 check_realname(realname);
00116 users_[user].realname = realname;
00117 }
00118
00119
00120
00121
00122
00123 void suh::set_lastlogin(const std::string& user, const time_t& lastlogin) {
00124 users_[user].lastlogin = lastlogin;
00125 }
00126
00127
00128
00129 std::string suh::get_mail(const std::string& user) {
00130 return users_[user].mail;
00131 }
00132
00133 std::string suh::get_password(const std::string& user) {
00134 return users_[user].password;
00135 }
00136
00137 std::string suh::get_realname(const std::string& user) {
00138 return users_[user].realname;
00139 }
00140
00141 time_t suh::get_lastlogin(const std::string& user) {
00142 return users_[user].lastlogin;
00143 }
00144
00145 time_t suh::get_registrationdate(const std::string& user) {
00146 return users_[user].registrationdate;
00147 }
00148
00149
00150
00151 void suh::check_name(const std::string& name) {
00152 if(!utils::isvalid_username(name)) {
00153 throw error("This username contains invalid "
00154 "characters. Only alpha-numeric characters, underscores and hyphens"
00155 "are allowed.");
00156 }
00157 if(name.size() > 20) {
00158 throw error("This username is too long. Usernames must be 20 characters or less.");
00159 }
00160 }
00161
00162 void suh::check_mail(const std::string& ) {
00163 return;
00164 }
00165
00166 void suh::check_password(const std::string& password) {
00167 if(!utils::isvalid_username(password)) {
00168 throw error("Password contains invalid characters");
00169 }
00170 }
00171
00172 void suh::check_realname(const std::string& realname) {
00173 if(realname.size() > 50) {
00174 throw error("This name is too long. Names must be 50 characters or less");
00175 }
00176 }
00177
00178
00179 void suh::clean_up() {
00180
00181
00182
00183
00184
00185 if(!user_expiration_) {
00186 return;
00187 }
00188
00189 time_t now = time(NULL);
00190
00191
00192
00193 time_t limit = user_expiration_ * 60 * 60 * 24;
00194
00195 std::vector<std::string> us = users();
00196 for(std::vector<std::string>::const_iterator u = us.begin(); u != us.end(); ++u) {
00197 if((now - get_lastlogin(*u)) > limit) {
00198 std::cout << "User '" << *u << "' exceeds expiration limit.\n";
00199 remove_user(*u);
00200 }
00201 }
00202 }
00203
00204 bool suh::login(const std::string& name, const std::string& password, const std::string&) {
00205 return password == get_password(name);
00206 }
00207
00208 void suh::user_logged_in(const std::string& name) {
00209 set_lastlogin(name, time(NULL));
00210 }
00211
00212 void suh::password_reminder(const std::string& name) {
00213 std::stringstream msg;
00214 msg << "Hello " << name << ",\nyour password is '" << get_password(name) << "'.\n\nHave fun playing Wesnoth :)";
00215 send_mail(name, "Wesnoth Password Reminder", msg.str());
00216 }
00217
00218 std::string suh::user_info(const std::string& name) {
00219 if(!user_exists(name)) throw error("No user with the name '" + name + "' exists.");
00220
00221 time_t reg_date = get_registrationdate(name);
00222 time_t ll_date = get_lastlogin(name);
00223
00224 std::string reg_string = ctime(®_date);
00225 std::string ll_string;
00226
00227 if(ll_date) {
00228 ll_string = ctime(&ll_date);
00229 } else {
00230 ll_string = "Never";
00231 }
00232
00233 std::stringstream info;
00234 info << "Name: " << name << "\n"
00235 << "Real name: " << get_realname(name) << "\n"
00236 << "Registered: " << reg_string
00237 << "Last login: " << ll_string;
00238 if(!user_is_active(name)) {
00239 info << "This account is currently inactive.\n";
00240 }
00241 return info.str();
00242 }