diff --git a/modules/users/SCsub b/modules/users/SCsub index a1645c4cc..d4f418823 100644 --- a/modules/users/SCsub +++ b/modules/users/SCsub @@ -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_stats_web_page.cpp") +if env['module_database_enabled']: + sources.append("managers/user_manager_db.cpp") + if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes': # Shared lib compilation module_env.Append(CCFLAGS=['-fPIC']) diff --git a/modules/users/config.py b/modules/users/config.py index fdba5eb0d..712940c4b 100644 --- a/modules/users/config.py +++ b/modules/users/config.py @@ -16,6 +16,7 @@ def get_doc_classes(): "UserManager", "UserManagerStatic", "UserManagerFile", + "UserManagerDB", "UserDB", "UserSessionSetupWebServerMiddleware", diff --git a/modules/users/managers/user_manager_db.cpp b/modules/users/managers/user_manager_db.cpp index cffc35d94..fd8c1228e 100644 --- a/modules/users/managers/user_manager_db.cpp +++ b/modules/users/managers/user_manager_db.cpp @@ -2,6 +2,70 @@ #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 UserManagerDB::get_database() { + if (_database.is_valid()) { + return _database; + } + + return DatabaseManager::get_singleton()->get_ddb(); +} + +void UserManagerDB::set_database(const Ref &db) { + _database = db; + + // todo send event to children when it's implemented? +} + +Ref UserManagerDB::get_database_connection() { + Ref db = get_database(); + + ERR_FAIL_COND_V(!db.is_valid(), Ref()); + + Ref conn = db->get_connection(); + + ERR_FAIL_COND_V(!conn.is_valid(), Ref()); + + return conn; +} + +Ref UserManagerDB::get_table_builder() { + Ref db = get_database(); + + ERR_FAIL_COND_V(!db.is_valid(), Ref()); + + Ref conn = db->get_connection(); + + ERR_FAIL_COND_V(!conn.is_valid(), Ref()); + + return conn->get_table_builder(); +} + +Ref UserManagerDB::get_query_builder() { + Ref db = get_database(); + + ERR_FAIL_COND_V(!db.is_valid(), Ref()); + + Ref conn = db->get_connection(); + + ERR_FAIL_COND_V(!conn.is_valid(), Ref()); + + return conn->get_query_builder(); +} + Ref UserManagerDB::_get_user(const int id) { if (id == 0) { return Ref(); @@ -10,9 +74,9 @@ Ref UserManagerDB::_get_user(const int id) { Ref b = get_query_builder(); 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(); @@ -25,29 +89,29 @@ Ref UserManagerDB::_get_user(const int id) { Ref user; user = create_user(); - user->id = id; - user->name_user_input = r->get_cell(0); - user->email_user_input = r->get_cell(1); - user->rank = r->get_cell_int(2); - user->pre_salt = r->get_cell(3); - user->post_salt = r->get_cell(4); - user->password_hash = r->get_cell(5); - user->banned = r->get_cell_bool(6); - user->password_reset_token = r->get_cell(7); - user->locked = r->get_cell_bool(8); + user->set_user_id(id); + user->set_user_name(r->get_cell(0)); + user->set_email(r->get_cell(1)); + user->set_rank(r->get_cell_int(2)); + user->set_pre_salt(r->get_cell(3)); + user->set_post_salt(r->get_cell(4)); + user->set_password_hash(r->get_cell(5)); + user->set_banned(r->get_cell_bool(6)); + user->set_password_reset_token(r->get_cell(7)); + user->set_locked(r->get_cell_bool(8)); return user; } Ref UserManagerDB::_get_user_name(const String &user_name) { - if (user_name_input == "") { + if (user_name == "") { return Ref(); } Ref b = get_query_builder(); b->select("id, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); - b->from(_table_name); - b->where()->wp("username", user_name_input); + b->from(_database_table_name); + b->where()->wps("username", user_name); b->end_command(); Ref r = b->run(); @@ -59,35 +123,35 @@ Ref UserManagerDB::_get_user_name(const String &user_name) { Ref user; user = create_user(); - user->id = r->get_cell_int(0); - user->name_user_input = user_name_input; - user->email_user_input = r->get_cell(1); - user->rank = r->get_cell_int(2); - user->pre_salt = r->get_cell(3); - user->post_salt = r->get_cell(4); - user->password_hash = r->get_cell(5); - user->banned = r->get_cell_bool(6); - user->password_reset_token = r->get_cell(7); - user->locked = r->get_cell_bool(8); + user->set_user_id(r->get_cell_int(0)); + user->set_user_name(user_name); + user->set_email(r->get_cell(1)); + user->set_rank(r->get_cell_int(2)); + user->set_pre_salt(r->get_cell(3)); + user->set_post_salt(r->get_cell(4)); + user->set_password_hash(r->get_cell(5)); + user->set_banned(r->get_cell_bool(6)); + user->set_password_reset_token(r->get_cell(7)); + user->set_locked(r->get_cell_bool(8)); return user; } void UserManagerDB::_save_user(Ref user) { Ref b = get_query_builder(); - if (user->id == 0) { - b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); + if (user->get_user_id() == 0) { + b->insert(_database_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); b->values(); - b->val(user->name_user_input); - b->val(user->email_user_input); - b->val(user->rank); - b->val(user->pre_salt); - b->val(user->post_salt); - b->val(user->password_hash); - b->val(user->banned); - b->val(user->password_reset_token); - b->val(user->locked); + b->vals(user->get_user_name()); + b->vals(user->get_email()); + b->vali(user->get_rank()); + b->vals(user->get_pre_salt()); + b->vals(user->get_post_salt()); + b->vals(user->get_password_hash()); + b->valb(user->get_banned()); + b->vals(user->get_password_reset_token()); + b->valb(user->get_locked()); b->cvalues(); b->end_command(); @@ -95,21 +159,21 @@ void UserManagerDB::_save_user(Ref user) { Ref r = b->run(); - user->id = r->get_last_insert_rowid(); + user->set_user_id(r->get_last_insert_rowid()); } else { - b->update(_table_name); - b->set(); - b->setp("username", user->name_user_input); - b->setp("email", user->email_user_input); - b->setp("rank", user->rank); - b->setp("pre_salt", user->pre_salt); - b->setp("post_salt", user->post_salt); - b->setp("password_hash", user->password_hash); - b->setp("banned", user->banned); - b->setp("password_reset_token", user->password_reset_token); - b->setp("locked", user->locked); + b->update(_database_table_name); + b->sets(); + b->setps("username", user->get_user_name()); + b->setps("email", user->get_email()); + b->setpi("rank", user->get_rank()); + b->setps("pre_salt", user->get_pre_salt()); + b->setps("post_salt", user->get_post_salt()); + b->setps("password_hash", user->get_password_hash()); + b->setpb("banned", user->get_banned()); + b->setps("password_reset_token", user->get_password_reset_token()); + b->setpb("locked", user->get_locked()); b->cset(); - b->where()->wp("id", user->id); + b->where()->wpi("id", user->get_user_id()); // b->print(); @@ -120,14 +184,15 @@ Ref UserManagerDB::_create_user() { Ref u; u.instance(); - save_user(u); + //save_user(u); + u->connect("changed", this, "_on_user_changed"); return u; } bool UserManagerDB::_is_username_taken(const String &user_name) { Ref 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 r = b->run(); @@ -136,7 +201,7 @@ bool UserManagerDB::_is_username_taken(const String &user_name) { bool UserManagerDB::_is_email_taken(const String &email) { Ref 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 r = b->run(); @@ -147,7 +212,7 @@ Vector> UserManagerDB::get_all() { Ref b = get_query_builder(); 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->print(); @@ -158,16 +223,16 @@ Vector> UserManagerDB::get_all() { while (r->next_row()) { Ref user = create_user(); - user->id = r->get_cell_int(0); - user->name_user_input = r->get_cell(1); - user->email_user_input = r->get_cell(2); - user->rank = r->get_cell_int(3); - user->pre_salt = r->get_cell(4); - user->post_salt = r->get_cell(5); - user->password_hash = r->get_cell(6); - user->banned = r->get_cell_bool(7); - user->password_reset_token = r->get_cell(8); - user->locked = r->get_cell_bool(9); + user->set_user_id(r->get_cell_int(0)); + user->set_user_name(r->get_cell(1)); + user->set_email(r->get_cell(2)); + user->set_rank(r->get_cell_int(3)); + user->set_pre_salt(r->get_cell(4)); + user->set_post_salt(r->get_cell(5)); + user->set_password_hash(r->get_cell(6)); + user->set_banned(r->get_cell_bool(7)); + user->set_password_reset_token(r->get_cell(8)); + user->set_locked(r->get_cell_bool(9)); users.push_back(user); } @@ -176,9 +241,22 @@ Vector> UserManagerDB::get_all() { } 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 tb = get_table_builder(); - tb->create_table(_table_name); + tb->create_table(_database_table_name); tb->integer("id")->auto_increment()->next_row(); tb->varchar("username", 60)->not_null()->next_row(); tb->varchar("email", 100)->not_null()->next_row(); @@ -194,38 +272,109 @@ void UserManagerDB::create_table() { tb->run_query(); // tb->print(); } -void UserManagerDB::drop_table() { +void UserManagerDB::_drop_table() { Ref 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 = create_user(); - user->rank = 3; - user->name_user_input = "admin"; - user->email_user_input = "admin@admin.com"; + user->set_rank(3); + user->set_user_name("admin"); + user->set_email("admin@admin.com"); - create_password(user, "Password"); - db_save_user(user); + user->create_password("Password"); + user->save(); user = create_user(); - user->rank = 1; - user->name_user_input = "user"; - user->email_user_input = "user@user.com"; + user->set_rank(1); + user->set_user_name("user"); + user->set_email("user@user.com"); - create_password(user, "Password"); - db_save_user(user); + user->create_password("Password"); + 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 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() { + _database_table_name = "users"; } 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); } diff --git a/modules/users/managers/user_manager_db.h b/modules/users/managers/user_manager_db.h index 14718e900..8f37555d5 100644 --- a/modules/users/managers/user_manager_db.h +++ b/modules/users/managers/user_manager_db.h @@ -9,11 +9,25 @@ #include "user_manager.h" class User; +class Database; +class TableBuilder; +class QueryBuilder; +class DatabaseConnection; class UserManagerDB : public UserManager { GDCLASS(UserManagerDB, UserManager); public: + String get_database_table_name(); + void set_database_table_name(const String &val); + + Ref get_database(); + void set_database(const Ref &db); + + Ref get_database_connection(); + Ref get_table_builder(); + Ref get_query_builder(); + Ref _get_user(const int id); Ref _get_user_name(const String &user); void _save_user(Ref user); @@ -25,15 +39,29 @@ public: Vector> get_all(); - virtual void create_table(); - virtual void drop_table(); - virtual void create_default_entries(); + void create_table(); + void drop_table(); + 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(); protected: + void _notification(const int what); + static void _bind_methods(); + + String _database_table_name; + Ref _database; }; #endif diff --git a/modules/users/register_types.cpp b/modules/users/register_types.cpp index 118cadd94..eb331b0e6 100644 --- a/modules/users/register_types.cpp +++ b/modules/users/register_types.cpp @@ -48,6 +48,10 @@ SOFTWARE. #include "web/web_nodes/user_web_page.cpp" #endif +#ifdef MODULE_DATABASE_ENABLED +#include "managers/user_manager_db.h" +#endif + UserDB *_user_db = nullptr; void register_users_types() { @@ -75,6 +79,10 @@ void register_users_types() { ClassDB::register_class(); ClassDB::register_class(); #endif + +#ifdef MODULE_DATABASE_ENABLED + ClassDB::register_class(); +#endif } void unregister_users_types() {