Cleaned up and added UserManagerDB to the build.

This commit is contained in:
Relintai 2022-07-24 16:26:23 +02:00
parent 13f0e82b6c
commit e0b2f048c2
5 changed files with 270 additions and 81 deletions

View File

@ -28,6 +28,9 @@ if env['module_web_enabled']:
sources.append("web/web_nodes/user_settings_web_page.cpp") sources.append("web/web_nodes/user_settings_web_page.cpp")
sources.append("web/web_nodes/user_stats_web_page.cpp") sources.append("web/web_nodes/user_stats_web_page.cpp")
if env['module_database_enabled']:
sources.append("managers/user_manager_db.cpp")
if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes': if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes':
# Shared lib compilation # Shared lib compilation
module_env.Append(CCFLAGS=['-fPIC']) module_env.Append(CCFLAGS=['-fPIC'])

View File

@ -16,6 +16,7 @@ def get_doc_classes():
"UserManager", "UserManager",
"UserManagerStatic", "UserManagerStatic",
"UserManagerFile", "UserManagerFile",
"UserManagerDB",
"UserDB", "UserDB",
"UserSessionSetupWebServerMiddleware", "UserSessionSetupWebServerMiddleware",

View File

@ -2,6 +2,70 @@
#include "../users/user.h" #include "../users/user.h"
#include "../../database/database.h"
#include "../../database/database_connection.h"
#include "../../database/database_manager.h"
#include "../../database/query_builder.h"
#include "../../database/query_result.h"
#include "../../database/table_builder.h"
String UserManagerDB::get_database_table_name() {
return _database_table_name;
}
void UserManagerDB::set_database_table_name(const String &val) {
_database_table_name = val;
}
Ref<Database> UserManagerDB::get_database() {
if (_database.is_valid()) {
return _database;
}
return DatabaseManager::get_singleton()->get_ddb();
}
void UserManagerDB::set_database(const Ref<Database> &db) {
_database = db;
// todo send event to children when it's implemented?
}
Ref<DatabaseConnection> UserManagerDB::get_database_connection() {
Ref<Database> db = get_database();
ERR_FAIL_COND_V(!db.is_valid(), Ref<DatabaseConnection>());
Ref<DatabaseConnection> conn = db->get_connection();
ERR_FAIL_COND_V(!conn.is_valid(), Ref<DatabaseConnection>());
return conn;
}
Ref<TableBuilder> UserManagerDB::get_table_builder() {
Ref<Database> db = get_database();
ERR_FAIL_COND_V(!db.is_valid(), Ref<TableBuilder>());
Ref<DatabaseConnection> conn = db->get_connection();
ERR_FAIL_COND_V(!conn.is_valid(), Ref<TableBuilder>());
return conn->get_table_builder();
}
Ref<QueryBuilder> UserManagerDB::get_query_builder() {
Ref<Database> db = get_database();
ERR_FAIL_COND_V(!db.is_valid(), Ref<QueryBuilder>());
Ref<DatabaseConnection> conn = db->get_connection();
ERR_FAIL_COND_V(!conn.is_valid(), Ref<QueryBuilder>());
return conn->get_query_builder();
}
Ref<User> UserManagerDB::_get_user(const int id) { Ref<User> UserManagerDB::_get_user(const int id) {
if (id == 0) { if (id == 0) {
return Ref<User>(); return Ref<User>();
@ -10,9 +74,9 @@ Ref<User> UserManagerDB::_get_user(const int id) {
Ref<QueryBuilder> b = get_query_builder(); Ref<QueryBuilder> b = get_query_builder();
b->select("username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); b->select("username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
b->from(_table_name); b->from(_database_table_name);
b->where()->wp("id", id); b->where()->wpi("id", id);
b->end_command(); b->end_command();
@ -25,29 +89,29 @@ Ref<User> UserManagerDB::_get_user(const int id) {
Ref<User> user; Ref<User> user;
user = create_user(); user = create_user();
user->id = id; user->set_user_id(id);
user->name_user_input = r->get_cell(0); user->set_user_name(r->get_cell(0));
user->email_user_input = r->get_cell(1); user->set_email(r->get_cell(1));
user->rank = r->get_cell_int(2); user->set_rank(r->get_cell_int(2));
user->pre_salt = r->get_cell(3); user->set_pre_salt(r->get_cell(3));
user->post_salt = r->get_cell(4); user->set_post_salt(r->get_cell(4));
user->password_hash = r->get_cell(5); user->set_password_hash(r->get_cell(5));
user->banned = r->get_cell_bool(6); user->set_banned(r->get_cell_bool(6));
user->password_reset_token = r->get_cell(7); user->set_password_reset_token(r->get_cell(7));
user->locked = r->get_cell_bool(8); user->set_locked(r->get_cell_bool(8));
return user; return user;
} }
Ref<User> UserManagerDB::_get_user_name(const String &user_name) { Ref<User> UserManagerDB::_get_user_name(const String &user_name) {
if (user_name_input == "") { if (user_name == "") {
return Ref<User>(); return Ref<User>();
} }
Ref<QueryBuilder> b = get_query_builder(); Ref<QueryBuilder> b = get_query_builder();
b->select("id, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); b->select("id, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
b->from(_table_name); b->from(_database_table_name);
b->where()->wp("username", user_name_input); b->where()->wps("username", user_name);
b->end_command(); b->end_command();
Ref<QueryResult> r = b->run(); Ref<QueryResult> r = b->run();
@ -59,35 +123,35 @@ Ref<User> UserManagerDB::_get_user_name(const String &user_name) {
Ref<User> user; Ref<User> user;
user = create_user(); user = create_user();
user->id = r->get_cell_int(0); user->set_user_id(r->get_cell_int(0));
user->name_user_input = user_name_input; user->set_user_name(user_name);
user->email_user_input = r->get_cell(1); user->set_email(r->get_cell(1));
user->rank = r->get_cell_int(2); user->set_rank(r->get_cell_int(2));
user->pre_salt = r->get_cell(3); user->set_pre_salt(r->get_cell(3));
user->post_salt = r->get_cell(4); user->set_post_salt(r->get_cell(4));
user->password_hash = r->get_cell(5); user->set_password_hash(r->get_cell(5));
user->banned = r->get_cell_bool(6); user->set_banned(r->get_cell_bool(6));
user->password_reset_token = r->get_cell(7); user->set_password_reset_token(r->get_cell(7));
user->locked = r->get_cell_bool(8); user->set_locked(r->get_cell_bool(8));
return user; return user;
} }
void UserManagerDB::_save_user(Ref<User> user) { void UserManagerDB::_save_user(Ref<User> user) {
Ref<QueryBuilder> b = get_query_builder(); Ref<QueryBuilder> b = get_query_builder();
if (user->id == 0) { if (user->get_user_id() == 0) {
b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); b->insert(_database_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
b->values(); b->values();
b->val(user->name_user_input); b->vals(user->get_user_name());
b->val(user->email_user_input); b->vals(user->get_email());
b->val(user->rank); b->vali(user->get_rank());
b->val(user->pre_salt); b->vals(user->get_pre_salt());
b->val(user->post_salt); b->vals(user->get_post_salt());
b->val(user->password_hash); b->vals(user->get_password_hash());
b->val(user->banned); b->valb(user->get_banned());
b->val(user->password_reset_token); b->vals(user->get_password_reset_token());
b->val(user->locked); b->valb(user->get_locked());
b->cvalues(); b->cvalues();
b->end_command(); b->end_command();
@ -95,21 +159,21 @@ void UserManagerDB::_save_user(Ref<User> user) {
Ref<QueryResult> r = b->run(); Ref<QueryResult> r = b->run();
user->id = r->get_last_insert_rowid(); user->set_user_id(r->get_last_insert_rowid());
} else { } else {
b->update(_table_name); b->update(_database_table_name);
b->set(); b->sets();
b->setp("username", user->name_user_input); b->setps("username", user->get_user_name());
b->setp("email", user->email_user_input); b->setps("email", user->get_email());
b->setp("rank", user->rank); b->setpi("rank", user->get_rank());
b->setp("pre_salt", user->pre_salt); b->setps("pre_salt", user->get_pre_salt());
b->setp("post_salt", user->post_salt); b->setps("post_salt", user->get_post_salt());
b->setp("password_hash", user->password_hash); b->setps("password_hash", user->get_password_hash());
b->setp("banned", user->banned); b->setpb("banned", user->get_banned());
b->setp("password_reset_token", user->password_reset_token); b->setps("password_reset_token", user->get_password_reset_token());
b->setp("locked", user->locked); b->setpb("locked", user->get_locked());
b->cset(); b->cset();
b->where()->wp("id", user->id); b->where()->wpi("id", user->get_user_id());
// b->print(); // b->print();
@ -120,14 +184,15 @@ Ref<User> UserManagerDB::_create_user() {
Ref<User> u; Ref<User> u;
u.instance(); u.instance();
save_user(u); //save_user(u);
u->connect("changed", this, "_on_user_changed");
return u; return u;
} }
bool UserManagerDB::_is_username_taken(const String &user_name) { bool UserManagerDB::_is_username_taken(const String &user_name) {
Ref<QueryBuilder> b = get_query_builder(); Ref<QueryBuilder> b = get_query_builder();
b->select("id")->from(_table_name)->where("username")->like(user_name_input)->end_command(); b->select("id")->from(_database_table_name)->where("username")->like(user_name)->end_command();
Ref<QueryResult> r = b->run(); Ref<QueryResult> r = b->run();
@ -136,7 +201,7 @@ bool UserManagerDB::_is_username_taken(const String &user_name) {
bool UserManagerDB::_is_email_taken(const String &email) { bool UserManagerDB::_is_email_taken(const String &email) {
Ref<QueryBuilder> b = get_query_builder(); Ref<QueryBuilder> b = get_query_builder();
b->select("id")->from(_table_name)->where("username")->like(email_input)->end_command(); b->select("id")->from(_database_table_name)->where("username")->like(email)->end_command();
Ref<QueryResult> r = b->run(); Ref<QueryResult> r = b->run();
@ -147,7 +212,7 @@ Vector<Ref<User>> UserManagerDB::get_all() {
Ref<QueryBuilder> b = get_query_builder(); Ref<QueryBuilder> b = get_query_builder();
b->select("id, username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); b->select("id, username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
b->from(_table_name); b->from(_database_table_name);
b->end_command(); b->end_command();
// b->print(); // b->print();
@ -158,16 +223,16 @@ Vector<Ref<User>> UserManagerDB::get_all() {
while (r->next_row()) { while (r->next_row()) {
Ref<User> user = create_user(); Ref<User> user = create_user();
user->id = r->get_cell_int(0); user->set_user_id(r->get_cell_int(0));
user->name_user_input = r->get_cell(1); user->set_user_name(r->get_cell(1));
user->email_user_input = r->get_cell(2); user->set_email(r->get_cell(2));
user->rank = r->get_cell_int(3); user->set_rank(r->get_cell_int(3));
user->pre_salt = r->get_cell(4); user->set_pre_salt(r->get_cell(4));
user->post_salt = r->get_cell(5); user->set_post_salt(r->get_cell(5));
user->password_hash = r->get_cell(6); user->set_password_hash(r->get_cell(6));
user->banned = r->get_cell_bool(7); user->set_banned(r->get_cell_bool(7));
user->password_reset_token = r->get_cell(8); user->set_password_reset_token(r->get_cell(8));
user->locked = r->get_cell_bool(9); user->set_locked(r->get_cell_bool(9));
users.push_back(user); users.push_back(user);
} }
@ -176,9 +241,22 @@ Vector<Ref<User>> UserManagerDB::get_all() {
} }
void UserManagerDB::create_table() { void UserManagerDB::create_table() {
call("_create_table");
}
void UserManagerDB::drop_table() {
call("_drop_table");
}
void UserManagerDB::udpate_table(const int p_current_table_version) {
call("_udpate_table", p_current_table_version);
}
void UserManagerDB::create_default_entries(const int p_seed) {
call("_create_default_entries", p_seed);
}
void UserManagerDB::_create_table() {
Ref<TableBuilder> tb = get_table_builder(); Ref<TableBuilder> tb = get_table_builder();
tb->create_table(_table_name); tb->create_table(_database_table_name);
tb->integer("id")->auto_increment()->next_row(); tb->integer("id")->auto_increment()->next_row();
tb->varchar("username", 60)->not_null()->next_row(); tb->varchar("username", 60)->not_null()->next_row();
tb->varchar("email", 100)->not_null()->next_row(); tb->varchar("email", 100)->not_null()->next_row();
@ -194,38 +272,109 @@ void UserManagerDB::create_table() {
tb->run_query(); tb->run_query();
// tb->print(); // tb->print();
} }
void UserManagerDB::drop_table() { void UserManagerDB::_drop_table() {
Ref<TableBuilder> tb = get_table_builder(); Ref<TableBuilder> tb = get_table_builder();
tb->drop_table_if_exists(_table_name)->run_query(); tb->drop_table_if_exists(_database_table_name)->run_query();
} }
void UserManagerDB::create_default_entries() { void UserManagerDB::_udpate_table(const int p_current_table_version) {
}
void UserManagerDB::_create_default_entries(const int p_seed) {
Ref<User> user; Ref<User> user;
user = create_user(); user = create_user();
user->rank = 3; user->set_rank(3);
user->name_user_input = "admin"; user->set_user_name("admin");
user->email_user_input = "admin@admin.com"; user->set_email("admin@admin.com");
create_password(user, "Password"); user->create_password("Password");
db_save_user(user); user->save();
user = create_user(); user = create_user();
user->rank = 1; user->set_rank(1);
user->name_user_input = "user"; user->set_user_name("user");
user->email_user_input = "user@user.com"; user->set_email("user@user.com");
create_password(user, "Password"); user->create_password("Password");
db_save_user(user); user->save();
}
void UserManagerDB::migrate(const bool p_clear, const bool p_should_seed, const int p_seed) {
call("_migrate", p_clear, p_should_seed, p_seed);
}
void UserManagerDB::_migrate(const bool p_clear, const bool p_should_seed, const int p_seed) {
if (p_clear) {
drop_table();
create_table();
} else {
#ifdef MODULE_DATABASE_ENABLED
Ref<DatabaseConnection> conn = get_database_connection();
ERR_FAIL_COND(!conn.is_valid());
int ver = conn->get_table_version(_database_table_name);
udpate_table(ver);
#else
udpate_table(0);
#endif
}
if (p_should_seed) {
create_default_entries(p_seed);
}
} }
UserManagerDB::UserManagerDB() { UserManagerDB::UserManagerDB() {
_database_table_name = "users";
} }
UserManagerDB::~UserManagerDB() { UserManagerDB::~UserManagerDB() {
} }
void UserManagerDB::_bind_methods() { void UserManagerDB::_notification(const int what) {
switch (what) {
case NOTIFICATION_ENTER_TREE: {
DatabaseManager::get_singleton()->connect("migration", this, "migrate");
} break;
case NOTIFICATION_EXIT_TREE: {
DatabaseManager::get_singleton()->disconnect("migration", this, "migrate");
} break;
default:
break;
}
}
void UserManagerDB::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_database_table_name"), &UserManagerDB::get_database_table_name);
ClassDB::bind_method(D_METHOD("set_database_table_name", "val"), &UserManagerDB::set_database_table_name);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "database_table_name"), "set_database_table_name", "get_database_table_name");
ClassDB::bind_method(D_METHOD("get_database"), &UserManagerDB::get_database);
ClassDB::bind_method(D_METHOD("set_database", "val"), &UserManagerDB::set_database);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "database", PROPERTY_HINT_RESOURCE_TYPE, "Database", 0), "set_database", "get_database");
ClassDB::bind_method(D_METHOD("get_database_connection"), &UserManagerDB::get_database_connection);
ClassDB::bind_method(D_METHOD("get_table_builder"), &UserManagerDB::get_table_builder);
ClassDB::bind_method(D_METHOD("get_query_builder"), &UserManagerDB::get_query_builder);
BIND_VMETHOD(MethodInfo("_create_table"));
BIND_VMETHOD(MethodInfo("_drop_table"));
BIND_VMETHOD(MethodInfo("_udpate_table", PropertyInfo(Variant::INT, "current_table_version")));
BIND_VMETHOD(MethodInfo("_create_default_entries", PropertyInfo(Variant::INT, "pseed")));
ClassDB::bind_method(D_METHOD("create_table"), &UserManagerDB::create_table);
ClassDB::bind_method(D_METHOD("drop_table"), &UserManagerDB::drop_table);
ClassDB::bind_method(D_METHOD("udpate_table", "current_table_version"), &UserManagerDB::udpate_table);
ClassDB::bind_method(D_METHOD("create_default_entries", "pseed"), &UserManagerDB::create_default_entries);
ClassDB::bind_method(D_METHOD("_create_table"), &UserManagerDB::_create_table);
ClassDB::bind_method(D_METHOD("_drop_table"), &UserManagerDB::_drop_table);
ClassDB::bind_method(D_METHOD("_udpate_table", "current_table_version"), &UserManagerDB::_udpate_table);
ClassDB::bind_method(D_METHOD("_create_default_entries", "pseed"), &UserManagerDB::_create_default_entries);
BIND_VMETHOD(MethodInfo("_migrate", PropertyInfo(Variant::BOOL, "clear"), PropertyInfo(Variant::BOOL, "should_seed"), PropertyInfo(Variant::INT, "pseed")));
ClassDB::bind_method(D_METHOD("migrate", "clear", "pseed"), &UserManagerDB::migrate);
ClassDB::bind_method(D_METHOD("_migrate", "clear", "pseed"), &UserManagerDB::_migrate);
} }

View File

@ -9,11 +9,25 @@
#include "user_manager.h" #include "user_manager.h"
class User; class User;
class Database;
class TableBuilder;
class QueryBuilder;
class DatabaseConnection;
class UserManagerDB : public UserManager { class UserManagerDB : public UserManager {
GDCLASS(UserManagerDB, UserManager); GDCLASS(UserManagerDB, UserManager);
public: public:
String get_database_table_name();
void set_database_table_name(const String &val);
Ref<Database> get_database();
void set_database(const Ref<Database> &db);
Ref<DatabaseConnection> get_database_connection();
Ref<TableBuilder> get_table_builder();
Ref<QueryBuilder> get_query_builder();
Ref<User> _get_user(const int id); Ref<User> _get_user(const int id);
Ref<User> _get_user_name(const String &user); Ref<User> _get_user_name(const String &user);
void _save_user(Ref<User> user); void _save_user(Ref<User> user);
@ -25,15 +39,29 @@ public:
Vector<Ref<User>> get_all(); Vector<Ref<User>> get_all();
virtual void create_table(); void create_table();
virtual void drop_table(); void drop_table();
virtual void create_default_entries(); void udpate_table(const int p_current_table_version);
void create_default_entries(const int p_seed);
virtual void _create_table();
virtual void _drop_table();
virtual void _udpate_table(const int p_current_table_version);
virtual void _create_default_entries(const int p_seed);
void migrate(const bool p_clear, const bool p_should_seed, const int p_seed);
virtual void _migrate(const bool p_clear, const bool p_should_seed, const int p_seed);
UserManagerDB(); UserManagerDB();
~UserManagerDB(); ~UserManagerDB();
protected: protected:
void _notification(const int what);
static void _bind_methods(); static void _bind_methods();
String _database_table_name;
Ref<Database> _database;
}; };
#endif #endif

View File

@ -48,6 +48,10 @@ SOFTWARE.
#include "web/web_nodes/user_web_page.cpp" #include "web/web_nodes/user_web_page.cpp"
#endif #endif
#ifdef MODULE_DATABASE_ENABLED
#include "managers/user_manager_db.h"
#endif
UserDB *_user_db = nullptr; UserDB *_user_db = nullptr;
void register_users_types() { void register_users_types() {
@ -75,6 +79,10 @@ void register_users_types() {
ClassDB::register_class<UserSettingsWebPage>(); ClassDB::register_class<UserSettingsWebPage>();
ClassDB::register_class<UserStatsWebPage>(); ClassDB::register_class<UserStatsWebPage>();
#endif #endif
#ifdef MODULE_DATABASE_ENABLED
ClassDB::register_class<UserManagerDB>();
#endif
} }
void unregister_users_types() { void unregister_users_types() {