The Battle for Wesnoth  1.15.12+dev
user_handler.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 class config;
18 
19 #include "exceptions.hpp"
21 
22 #include <ctime>
23 #include <string>
24 
25 #include <boost/asio/io_service.hpp>
26 
27 /**
28  * An interface class to handle nick registration
29  * To activate it put a [user_handler] section into the
30  * server configuration file
31  */
33 {
34 // public functions are called by the server
35 //
36 // private functions are for convenience as they
37 // will probably be the same for all user_handler
38 // implementations
39 
40 public:
42  {
43  }
44 
45  virtual ~user_handler()
46  {
47  }
48 
49  /**
50  * Return true if the given password matches the password for the given user.
51  *
52  * Password could also be a hash
53  * Seed is not needed for clear text log ins
54  * Currently the login procedure in the server and client code is hardcoded
55  * for the forum_user_handler implementation
56  */
57  virtual bool login(const std::string& name, const std::string& password, const std::string& seed) = 0;
58 
59  /** Executed when the user with the given name logged in. */
60  virtual void user_logged_in(const std::string& name) = 0;
61 
62  /**
63  * Returns a string containing info like the last login of this user.
64  *
65  * Formatted for user readable output.
66  */
67  virtual std::string user_info(const std::string& name) = 0;
68 
69  /** Returns true if a user with the given name exists. */
70  virtual bool user_exists(const std::string& name) = 0;
71 
72  /** Returns the forum user id for the given username */
73  virtual long get_forum_id(const std::string& name) = 0;
74 
75  /** Returns true if the specified user account is usable for logins. */
76  virtual bool user_is_active(const std::string& name) = 0;
77 
78  /** Returns true if this user is a moderator on this server */
79  virtual bool user_is_moderator(const std::string& name) = 0;
80 
81  /** Mark this user as a moderator */
82  virtual void set_is_moderator(const std::string& name, const bool& is_moderator) = 0;
83 
84  /** Ban type values */
85  enum BAN_TYPE
86  {
87  BAN_NONE = 0, /**< Not a ban */
88  BAN_IP = 1, /**< IP address ban */
89  BAN_USER = 2, /**< User account/name ban */
90  BAN_EMAIL = 3, /**< Account email address ban */
91  };
92 
93  /** Ban status description */
94  struct ban_info
95  {
96  BAN_TYPE type; /**< Ban type */
97  std::time_t duration; /**< Ban duration (0 if permanent) */
98 
100  : type(BAN_NONE)
101  , duration(0)
102  {
103  }
104 
105  ban_info(BAN_TYPE ptype, std::time_t pduration)
106  : type(ptype)
107  , duration(pduration)
108  {
109  }
110  };
111 
112  /**
113  * Returns true if this user account or IP address is banned.
114  *
115  * @note The IP address is only used by the @a forum_user_handler
116  * subclass. Regular IP ban checks are done by @a server_base
117  * instead.
118  */
119  virtual ban_info user_is_banned(const std::string& name, const std::string& addr="") = 0;
120 
121  struct error : public game::error {
122  error(const std::string& message) : game::error(message) {}
123  };
124 
125  /** Create a random string of digits for password encryption. */
126  std::string create_unsecure_nonce(int length = 8);
127  std::string create_secure_nonce();
128 
129  /**
130  * Create custom salt.
131  *
132  * If not needed let it return and empty string or whatever you feel like.
133  */
134  virtual std::string extract_salt(const std::string& username) = 0;
135 
136  virtual std::string get_uuid() = 0;
137  virtual std::string get_tournaments() = 0;
138  virtual void async_get_and_send_game_history(boost::asio::io_service& io_service, server_base& s_base, socket_ptr player_socket, int player_id, int offset) =0;
139  virtual void db_insert_game_info(const std::string& uuid, int game_id, const std::string& version, const std::string& name, int reload, int observers, int is_public, int has_password) = 0;
140  virtual void db_update_game_end(const std::string& uuid, int game_id, const std::string& replay_location) = 0;
141  virtual void db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source, const std::string& current_user) = 0;
142  virtual void db_insert_game_content_info(const std::string& uuid, int game_id, const std::string& type, const std::string& name, const std::string& id, const std::string& source, const std::string& version) = 0;
143  virtual void db_set_oos_flag(const std::string& uuid, int game_id) = 0;
144  virtual void async_test_query(boost::asio::io_service& io_service, int limit) = 0;
145  virtual bool db_topic_id_exists(int topic_id) = 0;
146  virtual void db_insert_addon_info(const std::string& instance_version, const std::string& id, const std::string& name, const std::string& type, const std::string& version, bool forum_auth, int topic_id) = 0;
147  virtual unsigned long long db_insert_login(const std::string& username, const std::string& ip, const std::string& version) = 0;
148  virtual void db_update_logout(unsigned long long login_id) = 0;
149  virtual void get_users_for_ip(const std::string& ip, std::ostringstream* out) = 0;
150  virtual void get_ips_for_user(const std::string& username, std::ostringstream* out) = 0;
151 };
std::string create_unsecure_nonce(int length=8)
Create a random string of digits for password encryption.
virtual void async_get_and_send_game_history(boost::asio::io_service &io_service, server_base &s_base, socket_ptr player_socket, int player_id, int offset)=0
std::time_t duration
Ban duration (0 if permanent)
virtual void db_set_oos_flag(const std::string &uuid, int game_id)=0
virtual void db_insert_game_content_info(const std::string &uuid, int game_id, const std::string &type, const std::string &name, const std::string &id, const std::string &source, const std::string &version)=0
virtual std::string extract_salt(const std::string &username)=0
Create custom salt.
virtual void db_update_game_end(const std::string &uuid, int game_id, const std::string &replay_location)=0
virtual bool user_is_moderator(const std::string &name)=0
Returns true if this user is a moderator on this server.
User account/name ban.
virtual void set_is_moderator(const std::string &name, const bool &is_moderator)=0
Mark this user as a moderator.
ban_info(BAN_TYPE ptype, std::time_t pduration)
virtual ~user_handler()
virtual void async_test_query(boost::asio::io_service &io_service, int limit)=0
virtual void get_users_for_ip(const std::string &ip, std::ostringstream *out)=0
An interface class to handle nick registration To activate it put a [user_handler] section into the s...
virtual void db_insert_game_player_info(const std::string &uuid, int game_id, const std::string &username, int side_number, int is_host, const std::string &faction, const std::string &version, const std::string &source, const std::string &current_user)=0
virtual void db_update_logout(unsigned long long login_id)=0
virtual void db_insert_addon_info(const std::string &instance_version, const std::string &id, const std::string &name, const std::string &type, const std::string &version, bool forum_auth, int topic_id)=0
virtual std::string get_tournaments()=0
virtual ban_info user_is_banned(const std::string &name, const std::string &addr="")=0
Returns true if this user account or IP address is banned.
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.
IP address ban.
Ban status description.
virtual bool db_topic_id_exists(int topic_id)=0
std::string password(const std::string &server, const std::string &login)
virtual bool user_is_active(const std::string &name)=0
Returns true if the specified user account is usable for logins.
BAN_TYPE type
Ban type.
virtual void db_insert_game_info(const std::string &uuid, int game_id, const std::string &version, const std::string &name, int reload, int observers, int is_public, int has_password)=0
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
virtual unsigned long long db_insert_login(const std::string &username, const std::string &ip, const std::string &version)=0
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.
std::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
Definition: server_base.hpp:43
BAN_TYPE
Ban type values.
int side_number
Definition: game_info.hpp:39
Account email address ban.
virtual long get_forum_id(const std::string &name)=0
Returns the forum user id for the given username.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
virtual void get_ips_for_user(const std::string &username, std::ostringstream *out)=0
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.
Base class for servers using Wesnoth&#39;s WML over TCP protocol.
std::string create_secure_nonce()
virtual std::string get_uuid()=0