The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
user_handler.hpp
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 #ifndef USER_HANDLER_HPP_INCLUDED
16 #define USER_HANDLER_HPP_INCLUDED
17 
18 class config;
19 
20 #include "exceptions.hpp"
21 #include "global.hpp"
22 
23 #include <string>
24 
25 /**
26  * An interface class to handle nick registration
27  * To activate it put a [user_handler] section into the
28  * server configuration file
29  */
30 class user_handler {
31 
32  // public functions are called by the server
33  //
34  // private functions are for convenience as they
35  // will probably be the same for all user_handler
36  // implementations
37 
38  public:
40  {
41  }
42 
43  virtual ~user_handler()
44  {
45  }
46 
47  /**
48  * Adds a user.
49  *
50  * Throws an error containing the error message if adding fails
51  * (e.g. because a user with the same name already exists).
52  */
53  virtual void add_user(const std::string& name, const std::string& mail, const std::string& password) =0;
54 
55  /**
56  * Removes a user.
57  *
58  * Throws an error containing the error message if removing fails
59  * (e.g. no user with the given name exists).
60  */
61  virtual void remove_user(const std::string& name) =0;
62 
63  /**
64  * Called by the server once a day.
65  *
66  * Could for example be used for removing users that have not logged in
67  * for a certain amount of time.
68  */
69  virtual void clean_up() =0;
70 
71  /**
72  * Send a password reminder email to the given user.
73  *
74  * Should throw user_handler::error if sending fails
75  * (e.g. because we cannot send email).
76  */
77  virtual void password_reminder(const std::string& name) =0;
78 
79  /**
80  * Return true if the given password matches the password for the given user.
81  *
82  * Password could also be a hash
83  * Seed is not needed for clear text log ins
84  * Currently the login procedure in the server and client code is hardcoded
85  * for the forum_user_handler implementation
86  */
87  virtual bool login(const std::string& name, const std::string& password, const std::string& seed) =0;
88 
89  /** Executed when the user with the given name logged in. */
90  virtual void user_logged_in(const std::string& name) =0;
91 
92  /**
93  * Returns a string containing info like the last login of this user.
94  *
95  * Formatted for user readable output.
96  */
97  virtual std::string user_info(const std::string& name) =0;
98 
99  /**
100  * Set data for a given user name.
101  *
102  * Should throw an error on invalid data.
103  */
104  virtual void set_user_detail(const std::string& user, const std::string& detail, const std::string& value) =0;
105 
106  /** List of details that can be set for this user_handler. */
107  virtual std::string get_valid_details() =0;
108 
109  /** Returns true if a user with the given name exists. */
110  virtual bool user_exists(const std::string& name) =0;
111 
112  /** Returns true if the specified user account is usable for logins. */
113  virtual bool user_is_active(const std::string& name) =0;
114 
115  /** Returns true if this user is a moderator on this server */
116  virtual bool user_is_moderator(const std::string& name) =0;
117 
118  /** Mark this user as a moderator */
119  virtual void set_is_moderator(const std::string& name, const bool& is_moderator) =0;
120 
121  struct error : public game::error {
122  error(const std::string& message) : game::error(message) {}
123  };
124 
125  /** Initiate the mailer object. */
126  void init_mailer(const config &c);
127 
128  /** Create a random string of digits for password encryption. */
130 
131  /**
132  * Create custom salt.
133  *
134  * If not needed let it return and empty string or whatever you feel like.
135  */
136  virtual std::string create_pepper(const std::string& username) =0;
137 
138  /**
139  * Does this user_handler want passwords passed encrypted using phpbb's algorithm?
140  *
141  * Let it return true if it does and false if it does not.
142  */
143  virtual bool use_phpbb_encryption() const =0;
144 
145  protected:
146 
147  /**
148  * Sends an email to the specified address. Requires access to an SMTP server.
149  *
150  * Throws an error if the mail could not be sent.
151  */
152  bool send_mail(const std::string& to_user, const std::string& subject, const std::string& message);
153 
154  /**
155  * Used in send_mail().
156  *
157  * Should return an empty string when not used.
158  */
159  virtual std::string get_mail(const std::string& user) =0;
160 };
161 
162 #endif //USER_HANDLER_HPP_INCLUDED
virtual void remove_user(const std::string &name)=0
Removes a user.
virtual bool use_phpbb_encryption() const =0
Does this user_handler want passwords passed encrypted using phpbb's algorithm?
const GLfloat * c
Definition: glew.h:12741
virtual std::string get_mail(const std::string &user)=0
Used in send_mail().
virtual bool user_is_moderator(const std::string &name)=0
Returns true if this user is a moderator on this server.
virtual void set_user_detail(const std::string &user, const std::string &detail, const std::string &value)=0
Set data for a given user name.
virtual void add_user(const std::string &name, const std::string &mail, const std::string &password)=0
Adds a user.
virtual void clean_up()=0
Called by the server once a day.
bool send_mail(const std::string &to_user, const std::string &subject, const std::string &message)
Sends an email to the specified address.
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
virtual void set_is_moderator(const std::string &name, const bool &is_moderator)=0
Mark this user as a moderator.
void init_mailer(const config &c)
Initiate the mailer object.
virtual ~user_handler()
GLsizei const GLfloat * value
Definition: glew.h:1817
An interface class to handle nick registration To activate it put a [user_handler] section into the s...
virtual std::string user_info(const std::string &name)=0
Returns a string containing info like the last login of this user.
virtual bool user_exists(const std::string &name)=0
Returns true if a user with the given name exists.
std::string create_salt(int length=8)
Create a random string of digits for password encryption.
virtual bool user_is_active(const std::string &name)=0
Returns true if the specified user account is usable for logins.
virtual void password_reminder(const std::string &name)=0
Send a password reminder email to the given user.
GLuint const GLchar * name
Definition: glew.h:1782
virtual std::string get_valid_details()=0
List of details that can be set for this user_handler.
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
error(const std::string &message)
virtual void user_logged_in(const std::string &name)=0
Executed when the user with the given name logged in.
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
virtual std::string create_pepper(const std::string &username)=0
Create custom salt.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual bool login(const std::string &name, const std::string &password, const std::string &seed)=0
Return true if the given password matches the password for the given user.
GLsizei const GLcharARB ** string
Definition: glew.h:4503