The Battle for Wesnoth  1.19.7+dev
user_handler.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 class config;
19 
20 #include "exceptions.hpp"
21 
22 #include <string>
23 
24 #include <boost/asio/io_context.hpp>
25 
27 
28 namespace wesnothd
29 {
30  class server;
31 }
32 
33 /**
34  * An interface class to handle nick registration
35  * To activate it put a [user_handler] section into the
36  * server configuration file
37  */
39 {
40 // public functions are called by the server
41 //
42 // private functions are for convenience as they
43 // will probably be the same for all user_handler
44 // implementations
45 
46 public:
48  {
49  }
50 
51  virtual ~user_handler()
52  {
53  }
54 
55  /**
56  * Return true if the given password matches the password for the given user.
57  *
58  * Password could also be a hash
59  * Seed is not needed for clear text log ins
60  * Currently the login procedure in the server and client code is hardcoded
61  * for the forum_user_handler implementation
62  */
63  virtual bool login(const std::string& name, const std::string& password) = 0;
64 
65  /** Executed when the user with the given name logged in. */
66  virtual void user_logged_in(const std::string& name) = 0;
67 
68  /**
69  * Returns a string containing info like the last login of this user.
70  *
71  * Formatted for user readable output.
72  */
73  virtual std::string user_info(const std::string& name) = 0;
74 
75  /** Returns true if a user with the given name exists. */
76  virtual bool user_exists(const std::string& name) = 0;
77 
78  /** Returns the forum user id for the given username */
79  virtual long get_forum_id(const std::string& name) = 0;
80 
81  /** Returns the user's email from the forum database */
82  virtual std::string get_user_email(const std::string& user) = 0;
83 
84  /** Returns true if the specified user account is usable for logins. */
85  virtual bool user_is_active(const std::string& name) = 0;
86 
87  /** Returns true if this user is a moderator on this server */
88  virtual bool user_is_moderator(const std::string& name) = 0;
89 
90  /** Mark this user as a moderator */
91  virtual void set_is_moderator(const std::string& name, const bool& is_moderator) = 0;
92 
93  /** Ban type values */
94  enum BAN_TYPE
95  {
96  BAN_NONE = 0, /**< Not a ban */
97  BAN_IP = 1, /**< IP address ban */
98  BAN_USER = 2, /**< User account/name ban */
99  BAN_EMAIL = 3, /**< Account email address ban */
100  };
101 
102  /** Ban status description */
103  struct ban_info
104  {
105  BAN_TYPE type = BAN_NONE; /**< Ban type */
106  std::chrono::seconds duration{0}; /**< Ban duration (0 if permanent) */
107  };
108 
109  /**
110  * Returns true if this user account or IP address is banned.
111  *
112  * @note The IP address is only used by the @a forum_user_handler
113  * subclass. Regular IP ban checks are done by @a server_base
114  * instead.
115  */
116  virtual ban_info user_is_banned(const std::string& name, const std::string& addr="") = 0;
117 
118  struct error : public game::error {
119  error(const std::string& message) : game::error(message) {}
120  };
121 
122  /**
123  * Create custom salt.
124  */
125  virtual std::string extract_salt(const std::string& username) = 0;
126 
127  virtual std::string get_uuid() = 0;
128  virtual std::string get_tournaments() = 0;
129  virtual void async_get_and_send_game_history(boost::asio::io_context& io_service, wesnothd::server& s, any_socket_ptr socket, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content) =0;
130  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;
131  virtual void db_update_game_end(const std::string& uuid, int game_id, const std::string& replay_location) = 0;
132  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, const std::string& leaders) = 0;
133  virtual unsigned long long 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& addon_id, const std::string& addon_version) = 0;
134  virtual void db_set_oos_flag(const std::string& uuid, int game_id) = 0;
135  virtual void async_test_query(boost::asio::io_context& io_service, int limit) = 0;
136  virtual bool db_topic_id_exists(int topic_id) = 0;
137  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, const std::string uploader) = 0;
138  virtual unsigned long long db_insert_login(const std::string& username, const std::string& ip, const std::string& version) = 0;
139  virtual void db_update_logout(unsigned long long login_id) = 0;
140  virtual void get_users_for_ip(const std::string& ip, std::ostringstream* out) = 0;
141  virtual void get_ips_for_user(const std::string& username, std::ostringstream* out) = 0;
142  virtual void db_update_addon_download_count(const std::string& instance_version, const std::string& id, const std::string& version) = 0;
143  virtual bool db_is_user_primary_author(const std::string& instance_version, const std::string& id, const std::string& username) = 0;
144  virtual bool db_is_user_secondary_author(const std::string& instance_version, const std::string& id, const std::string& username) = 0;
145  virtual void db_delete_addon_authors(const std::string& instance_version, const std::string& id) = 0;
146  virtual void db_insert_addon_authors(const std::string& instance_version, const std::string& id, const std::vector<std::string>& primary_authors, const std::vector<std::string>& secondary_authors) = 0;
147  virtual bool db_do_any_authors_exist(const std::string& instance_version, const std::string& id) = 0;
148 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
An interface class to handle nick registration To activate it put a [user_handler] section into the s...
virtual void db_delete_addon_authors(const std::string &instance_version, const std::string &id)=0
virtual std::string get_user_email(const std::string &user)=0
Returns the user's email from the forum database.
virtual long get_forum_id(const std::string &name)=0
Returns the forum user id for the given username.
virtual void db_update_addon_download_count(const std::string &instance_version, const std::string &id, const std::string &version)=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 void db_update_logout(unsigned long long login_id)=0
virtual bool login(const std::string &name, const std::string &password)=0
Return true if the given password matches the password for the given user.
virtual bool user_is_moderator(const std::string &name)=0
Returns true if this user is a moderator on this server.
BAN_TYPE
Ban type values.
@ BAN_EMAIL
Account email address ban.
@ BAN_IP
IP address ban.
@ BAN_USER
User account/name ban.
@ BAN_NONE
Not a ban.
virtual unsigned long long db_insert_login(const std::string &username, const std::string &ip, const std::string &version)=0
virtual bool user_is_active(const std::string &name)=0
Returns true if the specified user account is usable for logins.
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, const std::string uploader)=0
virtual ~user_handler()
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, const std::string &leaders)=0
virtual void db_update_game_end(const std::string &uuid, int game_id, const std::string &replay_location)=0
virtual void db_set_oos_flag(const std::string &uuid, int game_id)=0
virtual void db_insert_addon_authors(const std::string &instance_version, const std::string &id, const std::vector< std::string > &primary_authors, const std::vector< std::string > &secondary_authors)=0
virtual bool db_is_user_primary_author(const std::string &instance_version, const std::string &id, const std::string &username)=0
virtual bool user_exists(const std::string &name)=0
Returns true if a user with the given name exists.
virtual std::string get_tournaments()=0
virtual unsigned long long 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 &addon_id, const std::string &addon_version)=0
virtual bool db_is_user_secondary_author(const std::string &instance_version, const std::string &id, const std::string &username)=0
virtual void get_users_for_ip(const std::string &ip, std::ostringstream *out)=0
virtual bool db_topic_id_exists(int topic_id)=0
virtual void async_get_and_send_game_history(boost::asio::io_context &io_service, wesnothd::server &s, any_socket_ptr socket, int player_id, int offset, std::string &search_game_name, int search_content_type, std::string &search_content)=0
virtual std::string extract_salt(const std::string &username)=0
Create custom salt.
virtual void set_is_moderator(const std::string &name, const bool &is_moderator)=0
Mark this user as a moderator.
virtual std::string get_uuid()=0
virtual bool db_do_any_authors_exist(const std::string &instance_version, const std::string &id)=0
virtual void user_logged_in(const std::string &name)=0
Executed when the user with the given name logged in.
virtual void async_test_query(boost::asio::io_context &io_service, int limit)=0
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
virtual std::string user_info(const std::string &name)=0
Returns a string containing info like the last login of this user.
virtual void get_ips_for_user(const std::string &username, std::ostringstream *out)=0
int side_number
Definition: game_info.hpp:40
utils::variant< socket_ptr, tls_socket_ptr > any_socket_ptr
Definition: server_base.hpp:53
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string message
Definition: exceptions.hpp:30
Ban status description.
BAN_TYPE type
Ban type.
std::chrono::seconds duration
Ban duration (0 if permanent)
error(const std::string &message)
static map_location::direction s