mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2024-12-23 20:36:53 +01:00
Cleaned up and added UserManagerDB to the build.
This commit is contained in:
parent
13f0e82b6c
commit
e0b2f048c2
@ -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'])
|
||||
|
@ -16,6 +16,7 @@ def get_doc_classes():
|
||||
"UserManager",
|
||||
"UserManagerStatic",
|
||||
"UserManagerFile",
|
||||
"UserManagerDB",
|
||||
"UserDB",
|
||||
|
||||
"UserSessionSetupWebServerMiddleware",
|
||||
|
@ -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<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) {
|
||||
if (id == 0) {
|
||||
return Ref<User>();
|
||||
@ -10,9 +74,9 @@ Ref<User> UserManagerDB::_get_user(const int id) {
|
||||
Ref<QueryBuilder> 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<User> UserManagerDB::_get_user(const int id) {
|
||||
Ref<User> 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<User> UserManagerDB::_get_user_name(const String &user_name) {
|
||||
if (user_name_input == "") {
|
||||
if (user_name == "") {
|
||||
return Ref<User>();
|
||||
}
|
||||
|
||||
Ref<QueryBuilder> 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<QueryResult> r = b->run();
|
||||
@ -59,35 +123,35 @@ Ref<User> UserManagerDB::_get_user_name(const String &user_name) {
|
||||
Ref<User> 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> user) {
|
||||
Ref<QueryBuilder> 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> user) {
|
||||
|
||||
Ref<QueryResult> 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<User> UserManagerDB::_create_user() {
|
||||
Ref<User> 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<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();
|
||||
|
||||
@ -136,7 +201,7 @@ bool UserManagerDB::_is_username_taken(const String &user_name) {
|
||||
bool UserManagerDB::_is_email_taken(const String &email) {
|
||||
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();
|
||||
|
||||
@ -147,7 +212,7 @@ Vector<Ref<User>> UserManagerDB::get_all() {
|
||||
Ref<QueryBuilder> 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<Ref<User>> UserManagerDB::get_all() {
|
||||
while (r->next_row()) {
|
||||
Ref<User> 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<Ref<User>> 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<TableBuilder> 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<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;
|
||||
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<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() {
|
||||
_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);
|
||||
}
|
||||
|
@ -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<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_name(const String &user);
|
||||
void _save_user(Ref<User> user);
|
||||
@ -25,15 +39,29 @@ public:
|
||||
|
||||
Vector<Ref<User>> 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> _database;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -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<UserSettingsWebPage>();
|
||||
ClassDB::register_class<UserStatsWebPage>();
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_DATABASE_ENABLED
|
||||
ClassDB::register_class<UserManagerDB>();
|
||||
#endif
|
||||
}
|
||||
|
||||
void unregister_users_types() {
|
||||
|
Loading…
Reference in New Issue
Block a user