2022-07-22 10:16:49 +02:00
|
|
|
#include "user_manager_db.h"
|
|
|
|
|
|
|
|
#include "../users/user.h"
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
#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();
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:16:49 +02:00
|
|
|
Ref<User> UserManagerDB::_get_user(const int id) {
|
2022-12-18 18:42:09 +01:00
|
|
|
if (id <= 0) {
|
2022-07-22 10:16:49 +02:00
|
|
|
return Ref<User>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<QueryBuilder> b = get_query_builder();
|
|
|
|
|
|
|
|
b->select("username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
|
2022-07-24 16:26:23 +02:00
|
|
|
b->from(_database_table_name);
|
2022-07-22 10:16:49 +02:00
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
b->where()->wpi("id", id);
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
b->end_command();
|
|
|
|
|
|
|
|
Ref<QueryResult> r = b->run();
|
|
|
|
|
|
|
|
if (!r->next_row()) {
|
|
|
|
return Ref<User>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<User> user;
|
|
|
|
user = create_user();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
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));
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
Ref<User> UserManagerDB::_get_user_name(const String &user_name) {
|
2022-07-24 16:26:23 +02:00
|
|
|
if (user_name == "") {
|
2022-07-22 10:16:49 +02:00
|
|
|
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");
|
2022-07-24 16:26:23 +02:00
|
|
|
b->from(_database_table_name);
|
|
|
|
b->where()->wps("username", user_name);
|
2022-07-22 10:16:49 +02:00
|
|
|
b->end_command();
|
|
|
|
|
|
|
|
Ref<QueryResult> r = b->run();
|
|
|
|
|
|
|
|
if (!r->next_row()) {
|
|
|
|
return Ref<User>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<User> user;
|
|
|
|
user = create_user();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
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));
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
void UserManagerDB::_save_user(Ref<User> user) {
|
|
|
|
Ref<QueryBuilder> b = get_query_builder();
|
|
|
|
|
2022-12-18 13:16:58 +01:00
|
|
|
if (user->get_user_id() == -1) {
|
2022-07-24 16:26:23 +02:00
|
|
|
b->insert(_database_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
b->values();
|
2022-07-24 16:26:23 +02:00
|
|
|
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());
|
2022-07-22 10:16:49 +02:00
|
|
|
b->cvalues();
|
|
|
|
|
|
|
|
b->end_command();
|
|
|
|
b->select_last_insert_id();
|
2022-12-18 13:16:58 +01:00
|
|
|
|
2022-07-22 10:16:49 +02:00
|
|
|
Ref<QueryResult> r = b->run();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
user->set_user_id(r->get_last_insert_rowid());
|
2022-07-22 10:16:49 +02:00
|
|
|
} else {
|
2022-07-24 16:26:23 +02:00
|
|
|
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());
|
2022-07-22 10:16:49 +02:00
|
|
|
b->cset();
|
2022-07-24 16:26:23 +02:00
|
|
|
b->where()->wpi("id", user->get_user_id());
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
// b->print();
|
|
|
|
|
|
|
|
b->run_query();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ref<User> UserManagerDB::_create_user() {
|
|
|
|
Ref<User> u;
|
|
|
|
u.instance();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
//save_user(u);
|
2022-12-18 11:56:16 +01:00
|
|
|
u->connect("changed", this, "_save_user", varray(u));
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
bool UserManagerDB::_is_username_taken(const String &user_name) {
|
|
|
|
Ref<QueryBuilder> b = get_query_builder();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
b->select("id")->from(_database_table_name)->where("username")->like(user_name)->end_command();
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
Ref<QueryResult> r = b->run();
|
|
|
|
|
|
|
|
return r->next_row();
|
|
|
|
}
|
|
|
|
bool UserManagerDB::_is_email_taken(const String &email) {
|
|
|
|
Ref<QueryBuilder> b = get_query_builder();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
b->select("id")->from(_database_table_name)->where("username")->like(email)->end_command();
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
Ref<QueryResult> r = b->run();
|
|
|
|
|
|
|
|
return r->next_row();
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2022-07-24 16:26:23 +02:00
|
|
|
b->from(_database_table_name);
|
2022-07-22 10:16:49 +02:00
|
|
|
b->end_command();
|
|
|
|
// b->print();
|
|
|
|
|
|
|
|
Vector<Ref<User>> users;
|
|
|
|
|
|
|
|
Ref<QueryResult> r = b->run();
|
|
|
|
|
|
|
|
while (r->next_row()) {
|
|
|
|
Ref<User> user = create_user();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
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));
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
users.push_back(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserManagerDB::create_table() {
|
2022-07-24 16:26:23 +02:00
|
|
|
call("_create_table");
|
|
|
|
}
|
|
|
|
void UserManagerDB::drop_table() {
|
|
|
|
call("_drop_table");
|
|
|
|
}
|
2023-09-22 16:21:37 +02:00
|
|
|
void UserManagerDB::update_table(const int p_current_table_version) {
|
|
|
|
call("_update_table", p_current_table_version);
|
2022-07-24 16:26:23 +02:00
|
|
|
}
|
|
|
|
void UserManagerDB::create_default_entries(const int p_seed) {
|
|
|
|
call("_create_default_entries", p_seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserManagerDB::_create_table() {
|
2022-07-22 10:16:49 +02:00
|
|
|
Ref<TableBuilder> tb = get_table_builder();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
tb->create_table(_database_table_name);
|
2022-07-22 10:16:49 +02:00
|
|
|
tb->integer("id")->auto_increment()->next_row();
|
|
|
|
tb->varchar("username", 60)->not_null()->next_row();
|
|
|
|
tb->varchar("email", 100)->not_null()->next_row();
|
|
|
|
tb->integer("rank")->not_null()->next_row();
|
|
|
|
tb->varchar("pre_salt", 100)->next_row();
|
|
|
|
tb->varchar("post_salt", 100)->next_row();
|
|
|
|
tb->varchar("password_hash", 100)->next_row();
|
|
|
|
tb->integer("banned")->next_row();
|
|
|
|
tb->varchar("password_reset_token", 100)->next_row();
|
|
|
|
tb->integer("locked")->next_row();
|
|
|
|
tb->primary_key("id");
|
|
|
|
tb->ccreate_table();
|
|
|
|
tb->run_query();
|
|
|
|
// tb->print();
|
|
|
|
}
|
2022-07-24 16:26:23 +02:00
|
|
|
void UserManagerDB::_drop_table() {
|
2022-07-22 10:16:49 +02:00
|
|
|
Ref<TableBuilder> tb = get_table_builder();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
tb->drop_table_if_exists(_database_table_name)->run_query();
|
|
|
|
}
|
|
|
|
|
2023-09-22 16:21:37 +02:00
|
|
|
void UserManagerDB::_update_table(const int p_current_table_version) {
|
2022-07-22 10:16:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
void UserManagerDB::_create_default_entries(const int p_seed) {
|
2022-07-22 10:16:49 +02:00
|
|
|
Ref<User> user;
|
|
|
|
user = create_user();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
user->set_rank(3);
|
|
|
|
user->set_user_name("admin");
|
|
|
|
user->set_email("admin@admin.com");
|
2022-07-22 10:16:49 +02:00
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
user->create_password("Password");
|
|
|
|
user->save();
|
2022-07-22 10:16:49 +02:00
|
|
|
|
|
|
|
user = create_user();
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
user->set_rank(1);
|
|
|
|
user->set_user_name("user");
|
|
|
|
user->set_email("user@user.com");
|
|
|
|
|
|
|
|
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);
|
2023-09-22 16:21:37 +02:00
|
|
|
update_table(ver);
|
2022-07-24 16:26:23 +02:00
|
|
|
#else
|
2023-09-22 16:21:37 +02:00
|
|
|
update_table(0);
|
2022-07-24 16:26:23 +02:00
|
|
|
#endif
|
|
|
|
}
|
2022-07-22 10:16:49 +02:00
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
if (p_should_seed) {
|
|
|
|
create_default_entries(p_seed);
|
|
|
|
}
|
2022-07-22 10:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UserManagerDB::UserManagerDB() {
|
2022-07-24 16:26:23 +02:00
|
|
|
_database_table_name = "users";
|
2022-07-22 10:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UserManagerDB::~UserManagerDB() {
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:26:23 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:16:49 +02:00
|
|
|
void UserManagerDB::_bind_methods() {
|
2022-07-24 16:26:23 +02:00
|
|
|
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"));
|
2023-09-22 16:21:37 +02:00
|
|
|
BIND_VMETHOD(MethodInfo("_update_table", PropertyInfo(Variant::INT, "current_table_version")));
|
2022-07-24 16:26:23 +02:00
|
|
|
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);
|
2023-09-22 16:21:37 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("update_table", "current_table_version"), &UserManagerDB::update_table);
|
2022-07-24 16:26:23 +02:00
|
|
|
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);
|
2023-09-22 16:21:37 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_update_table", "current_table_version"), &UserManagerDB::_update_table);
|
2022-07-24 16:26:23 +02:00
|
|
|
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);
|
2022-07-22 10:16:49 +02:00
|
|
|
}
|