The Battle for Wesnoth  1.15.2+dev
forum_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 #include "server/user_handler.hpp"
18 
19 #include <vector>
20 #include <memory>
21 #include <ctime>
22 
23 #include <mysql/mysql.h>
24 
25 // The [user_handler] section in the server configuration
26 // file could look like this:
27 //
28 //[user_handler]
29 // db_name=phpbb3
30 // db_host=localhost
31 // db_user=root
32 // db_password=secret
33 // db_users_table=users
34 // db_banlist_table=banlist
35 // db_extra_table=extra_data
36 //[/user_handler]
37 
38 /**
39  * A user_handler implementation to link the server with a phpbb3 forum.
40  */
41 class fuh : public user_handler {
42  public:
43  fuh(const config& c);
44  ~fuh();
45 
46  bool login(const std::string& name, const std::string& password, const std::string& seed);
47 
48  /**
49  * Needed because the hashing algorithm used by phpbb requires some info
50  * from the original hash to recreate the same hash
51  *
52  * Return an empty string if an error occurs
53  */
54  std::string extract_salt(const std::string& name);
55 
56  void user_logged_in(const std::string& name);
57 
58  bool user_exists(const std::string& name);
59 
60  bool user_is_active(const std::string& name);
61 
62  bool user_is_moderator(const std::string& name);
63  void set_is_moderator(const std::string& name, const bool& is_moderator);
64 
65  ban_info user_is_banned(const std::string& name, const std::string& addr);
66 
67  // Throws user_handler::error
68  std::string user_info(const std::string& name);
69 
70  bool use_phpbb_encryption() const { return true; }
71 
72  std::string get_uuid();
73  void db_insert_game_info(const std::string& uuid, int game_id, const std::string& version, const std::string& name);
74  void db_update_game_start(const std::string& uuid, int game_id, const std::string& map_name, const std::string& era_name, int reload, int observers, int is_public, int has_password);
75  void db_update_game_end(const std::string& uuid, int game_id, const std::string& replay_location);
76  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);
77  void db_insert_modification_info(const std::string& uuid, int game_id, const std::string& modification_name);
78  void db_set_oos_flag(const std::string& uuid, int game_id);
79 
80  private:
81  std::string get_hash(const std::string& user);
82  std::time_t get_lastlogin(const std::string& user);
83  std::time_t get_registrationdate(const std::string& user);
84  bool is_inactive(const std::string& user);
85 
86  void set_lastlogin(const std::string& user, const std::time_t& lastlogin);
87 
88  template<typename T>
90 
91  std::time_t retrieve_ban_duration_internal(const std::string& col, const std::string& detail);
92  std::time_t retrieve_ban_duration_internal(const std::string& col, unsigned int detail);
93 
95  unsigned int mp_mod_group_;
96 
97  MYSQL *conn;
98 
99  template<typename T, typename... Args>
100  inline T prepared_statement(const std::string& sql, Args&&...);
101  // Query a detail for a particular user from the database
102  template<typename T>
103  T get_detail_for_user(const std::string& name, const std::string& detail);
104  template<typename T>
105  T get_writable_detail_for_user(const std::string& name, const std::string& detail);
106 
107  // Write something to the write table
108  template<typename T>
109  void write_detail(const std::string& name, const std::string& detail, T&& value);
110 
111  // Same as user_exists() but checks if we have a row for this user in the extra table
112  bool extra_row_exists(const std::string& name);
113 
114  bool is_user_in_group(const std::string& name, unsigned int group_id);
115 };
116 
std::string get_hash(const std::string &user)
std::string db_host_
bool is_inactive(const std::string &user)
std::string get_uuid()
void user_logged_in(const std::string &name)
Executed when the user with the given name logged in.
std::string db_name_
T prepared_statement(const std::string &sql, Args &&...)
std::string db_game_info_table_
std::time_t get_lastlogin(const std::string &user)
std::string db_game_player_info_table_
std::string db_game_modification_info_table_
std::string user_info(const std::string &name)
Returns a string containing info like the last login of this user.
ban_info retrieve_ban_info(BAN_TYPE, T detail)
bool user_exists(const std::string &name)
Returns true if a user with the given name exists.
void set_is_moderator(const std::string &name, const bool &is_moderator)
Mark this user as a moderator.
fuh(const config &c)
T get_detail_for_user(const std::string &name, const std::string &detail)
A user_handler implementation to link the server with a phpbb3 forum.
bool is_user_in_group(const std::string &name, unsigned int group_id)
bool extra_row_exists(const std::string &name)
ban_info user_is_banned(const std::string &name, const std::string &addr)
Returns true if this user account or IP address is banned.
void db_insert_modification_info(const std::string &uuid, int game_id, const std::string &modification_name)
An interface class to handle nick registration To activate it put a [user_handler] section into the s...
std::string db_password_
bool login(const std::string &name, const std::string &password, const std::string &seed)
Return true if the given password matches the password for the given user.
void db_set_oos_flag(const std::string &uuid, int game_id)
std::string db_extra_table_
void db_insert_game_info(const std::string &uuid, int game_id, const std::string &version, const std::string &name)
bool user_is_active(const std::string &name)
Returns true if the specified user account is usable for logins.
Ban status description.
std::string password(const std::string &server, const std::string &login)
std::time_t get_registrationdate(const std::string &user)
~fuh()
std::string db_users_table_
bool user_is_moderator(const std::string &name)
Returns true if this user is a moderator on this server.
void write_detail(const std::string &name, const std::string &detail, T &&value)
std::string db_user_
void db_update_game_start(const std::string &uuid, int game_id, const std::string &map_name, const std::string &era_name, int reload, int observers, int is_public, int has_password)
bool use_phpbb_encryption() const
Does this user_handler want passwords passed encrypted using phpbb&#39;s algorithm?
std::time_t retrieve_ban_duration_internal(const std::string &col, const std::string &detail)
T get_writable_detail_for_user(const std::string &name, const std::string &detail)
std::string db_user_group_table_
std::string db_banlist_table_
void set_lastlogin(const std::string &user, const std::time_t &lastlogin)
BAN_TYPE
Ban type values.
int side_number
Definition: game_info.hpp:39
MYSQL * conn
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
std::string extract_salt(const std::string &name)
Needed because the hashing algorithm used by phpbb requires some info from the original hash to recre...
mock_char c
unsigned int mp_mod_group_
void db_update_game_end(const std::string &uuid, int game_id, const std::string &replay_location)
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)