The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sample_user_handler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Thomas Baumhauer <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "sample_user_handler.hpp"
16 
17 #include "config.hpp"
19 #include "util.hpp"
20 
21 #include <iostream>
22 
24  : user_expiration_(0)
25  , users_()
26 {
27 
28  if(c["user_expiration"].empty()) {
29  user_expiration_ = 60;
30  } else {
31  try {
32  user_expiration_ = lexical_cast_default<int>(c["user_expiration"]);
33  } catch (bad_lexical_cast) {
34  std::cerr << "Bad lexical cast reading 'user_expiration', using default value.\n";
35  user_expiration_ = 60;
36  }
37  }
38 }
39 
40 void suh::add_user(const std::string& name, const std::string& mail, const std::string& password) {
41  if(user_exists(name)) throw error("This nickname is already registered");
42 
43  users_.insert(std::pair<std::string, user>(name, user()));
44 
45  set_password(name, password);
46  set_mail(name, mail);
47 
48  user_logged_in(name);
49 }
50 
52  if(!user_exists(name)) throw error("This nickname is not registered");
53 
54  users_.erase(name);
55 }
56 
58  std::map<std::string,user>::const_iterator u = users_.find(name);
59 
60  return (u != users_.end());
61 }
62 
63 bool suh::user_is_active(const std::string& /*name*/) {
64  // FIXME: add support for inactive users maybe?
65  return true;
66 }
67 
68 std::vector<std::string> suh::users() {
69  std::vector<std::string> users;
70  for(std::map<std::string,user>::const_iterator u = users_.begin(); u != users_.end(); ++u) {
71  users.push_back(u->first);
72  }
73  return users;
74 }
75 
77  if(detail == "mail") {
78  set_mail(user, value);
79  } else if (detail == "password") {
80  set_password(user, value);
81  } else if (detail == "realname") {
82  set_realname(user, value);
83  } else {
84  throw error("Invalid usersdetail '" + detail + "'. Valid details are: " + get_valid_details());
85  }
86 }
87 
89  return "'mail', 'password', 'realname';";
90 }
91 
92 
94  if(!user_exists(name)) return false;
95  return users_[name].is_moderator;
96 }
97 
98 void suh::set_is_moderator(const std::string& name, const bool& is_moderator) {
99  if(!user_exists(name)) return;
100  users_[name].is_moderator = is_moderator;
101 }
102 
103 void suh::set_mail(const std::string& user, const std::string& mail) {
104  check_mail(mail);
105  users_[user].mail = mail;
106 }
107 
109  check_password(password);
110  users_[user].password = password;
111 }
112 
113 void suh::set_realname(const std::string& user, const std::string& realname) {
114  check_realname(realname);
115  users_[user].realname = realname;
116 }
117 
118 //--
119 // set_lastlogin() is not called by the server via set_user_detail()
120 // and thus must not throw user_handler::error
121 
122 void suh::set_lastlogin(const std::string& user, const time_t& lastlogin) {
123  users_[user].lastlogin = lastlogin;
124 }
125 
126 //---
127 
129  return users_[user].mail;
130 }
131 
133  return users_[user].password;
134 }
135 
137  return users_[user].realname;
138 }
139 
141  return users_[user].lastlogin;
142 }
143 
145  return users_[user].registrationdate;
146 }
147 
148 //---
149 
151  if(!utils::isvalid_username(name)) {
152  throw error("This username contains invalid "
153  "characters. Only alpha-numeric characters, underscores and hyphens"
154  "are allowed.");
155  }
156  if(name.size() > 20) {
157  throw error("This username is too long. Usernames must be 20 characters or less.");
158  }
159 }
160 
161 void suh::check_mail(const std::string& /*mail*/) {
162  return;
163 }
164 
166  if(!utils::isvalid_username(password)) {
167  throw error("Password contains invalid characters");
168  }
169 }
170 
171 void suh::check_realname(const std::string& realname) {
172  if(realname.size() > 50) {
173  throw error("This name is too long. Names must be 50 characters or less");
174  }
175 }
176 
177 
179  // Remove users that have not logged in for user_expiration_ days:
180  // Check if the last login of this user exceeds the
181  // expiration limit
182 
183  //The expiration time set to 0 means no expiration limit
184  if(!user_expiration_) {
185  return;
186  }
187 
188  time_t now = time(nullptr);
189 
190  //A minute has 60 seconds, an hour 60 minutes and
191  //a day 24 hours.
192  time_t limit = user_expiration_ * 60 * 60 * 24;
193 
194  std::vector<std::string> us = users();
195  for(std::vector<std::string>::const_iterator u = us.begin(); u != us.end(); ++u) {
196  if((now - get_lastlogin(*u)) > limit) {
197  std::cout << "User '" << *u << "' exceeds expiration limit.\n";
198  remove_user(*u);
199  }
200  }
201 }
202 
204  return password == get_password(name);
205 }
206 
208  set_lastlogin(name, time(nullptr));
209 }
210 
212  std::stringstream msg;
213  msg << "Hello " << name << ",\nyour password is '" << get_password(name) << "'.\n\nHave fun playing Wesnoth :)";
214  send_mail(name, "Wesnoth Password Reminder", msg.str());
215 }
216 
218  if(!user_exists(name)) throw error("No user with the name '" + name + "' exists.");
219 
220  time_t reg_date = get_registrationdate(name);
221  time_t ll_date = get_lastlogin(name);
222 
223  std::string reg_string = ctime(&reg_date);
224  std::string ll_string;
225 
226  if(ll_date) {
227  ll_string = ctime(&ll_date);
228  } else {
229  ll_string = "Never\n";
230  }
231 
232  std::stringstream info;
233  info << "Name: " << name << "\n"
234  << "Real name: " << get_realname(name) << "\n"
235  << "Registered: " << reg_string
236  << "Last login: " << ll_string;
237  if(!user_is_active(name)) {
238  info << "This account is currently inactive.\n";
239  }
240  return info.str();
241 }
void set_mail(const std::string &user, const std::string &mail)
void check_mail(const std::string &mail)
bool user_is_moderator(const std::string &name)
Returns true if this user is a moderator on this server.
bool isvalid_username(const std::string &username)
Check if the username contains only valid characters.
void clean_up()
Called by the server once a day.
const GLfloat * c
Definition: glew.h:12741
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:29
logger & info()
Definition: log.cpp:91
bool login(const std::string &name, const std::string &password, const std::string &)
Return true if the given password matches the password for the given user.
void set_lastlogin(const std::string &user, const time_t &lastlogin)
bool send_mail(const std::string &to_user, const std::string &subject, const std::string &message)
Sends an email to the specified address.
Definitions for the interface to Wesnoth Markup Language (WML).
suh(config c)
std::string get_valid_details()
List of details that can be set for this user_handler.
void check_realname(const std::string &realname)
GLint limit
Definition: glew.h:10112
void check_name(const std::string &name)
void password_reminder(const std::string &name)
Send a password reminder email to the given user.
std::map< std::string, user > users_
GLsizei const GLfloat * value
Definition: glew.h:1817
void set_realname(const std::string &user, const std::string &realname)
time_t get_lastlogin(const std::string &user)
Templates and utility-routines for strings and numbers.
void user_logged_in(const std::string &name)
Executed when the user with the given name logged in.
void set_password(const std::string &user, const std::string &password)
bool user_exists(const std::string &name)
Returns true if a user with the given name exists.
void check_password(const std::string &password)
std::vector< std::string > users()
std::string get_mail(const std::string &user)
Used in send_mail().
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
std::string get_password(const std::string &user)
GLuint const GLchar * name
Definition: glew.h:1782
void set_is_moderator(const std::string &name, const bool &is_moderator)
Mark this user as a moderator.
void set_user_detail(const std::string &user, const std::string &detail, const std::string &value)
Set data for a given user name.
bool user_is_active(const std::string &name)
Returns true if the specified user account is usable for logins.
std::string get_realname(const std::string &user)
void remove_user(const std::string &name)
Removes a user.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void add_user(const std::string &name, const std::string &mail, const std::string &password)
Adds a user.
Thrown when a lexical_cast fails.
int user_expiration_
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::string user_info(const std::string &name)
Returns a string containing info like the last login of this user.
time_t get_registrationdate(const std::string &user)