From e7c1cd574193e5eb63b7461360ea9e16177c7276 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 19 Aug 2021 23:55:36 +0200 Subject: [PATCH] Added a new database based user. Still wip. --- modules/users/db_based_user.cpp | 184 ++++++++++++++++++++++++ modules/users/db_based_user.h | 25 ++++ modules/users/db_based_user_manager.cpp | 31 ++++ modules/users/db_based_user_manager.h | 26 ++++ modules/users/user.cpp | 8 +- modules/users/user.h | 1 + 6 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 modules/users/db_based_user.cpp create mode 100644 modules/users/db_based_user.h create mode 100644 modules/users/db_based_user_manager.cpp create mode 100644 modules/users/db_based_user_manager.h diff --git a/modules/users/db_based_user.cpp b/modules/users/db_based_user.cpp new file mode 100644 index 0000000..66a4e0b --- /dev/null +++ b/modules/users/db_based_user.cpp @@ -0,0 +1,184 @@ +#include "db_based_user.h" + +#include "core/database/database_manager.h" +#include "core/database/table_builder.h" +#include "core/database/query_builder.h" +#include "user_manager.h" +#include + +void DBBasedUser::save() { + QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder(); + + if (id == 0) { + b->insert(_table_name); + } else { + //todo + } + + + delete b; + + /* + //todo sanitize name! + _file_path = _path + name; + + rapidjson::Document document; + document.SetObject(); + + document.AddMember("id", id, document.GetAllocator()); + + document.AddMember("name", rapidjson::Value(name.c_str(), document.GetAllocator()), document.GetAllocator()); + document.AddMember("email", rapidjson::Value(email.c_str(), document.GetAllocator()), document.GetAllocator()); + document.AddMember("rank", rank, document.GetAllocator()); + document.AddMember("pre_salt", rapidjson::Value(pre_salt.c_str(), document.GetAllocator()), document.GetAllocator()); + document.AddMember("post_salt", rapidjson::Value(post_salt.c_str(), document.GetAllocator()), document.GetAllocator()); + document.AddMember("password_hash", rapidjson::Value(password_hash.c_str(), document.GetAllocator()), document.GetAllocator()); + document.AddMember("banned", banned, document.GetAllocator()); + document.AddMember("password_reset_token", rapidjson::Value(password_reset_token.c_str(), document.GetAllocator()), document.GetAllocator()); + document.AddMember("locked", locked, document.GetAllocator()); + + rapidjson::Value sa(rapidjson::Type::kArrayType); + rapidjson::Document::AllocatorType &allocator = document.GetAllocator(); + + for (int i = 0; i < sessions.size(); i++) { + sa.PushBack(rapidjson::Value(sessions[i].c_str(), document.GetAllocator()), allocator); + } + + document.AddMember("sessions", sa, document.GetAllocator()); + + FILE *fp = fopen(_file_path.c_str(), "w"); + + char writeBuffer[65536]; + rapidjson::FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer)); + + rapidjson::Writer writer(os); + document.Accept(writer); + + fclose(fp); + */ +} + +void DBBasedUser::load() { + unregister_sessions(); + + /* + FILE *f = fopen(_file_path.c_str(), "r"); + + if (!f) { + printf("DBBasedUser::load: Error opening file! %s\n", _file_path.c_str()); + return; + } + + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + fseek(f, 0, SEEK_SET); // same as rewind(f); + + std::string fd; + fd.resize(fsize); + + fread(&fd[0], 1, fsize, f); + fclose(f); + + rapidjson::Document data; + data.Parse(fd.c_str()); + + rapidjson::Value uobj = data.GetObject(); + + id = uobj["id"].GetInt(); + name = uobj["name"].GetString(); + email = uobj["email"].GetString(); + rank = uobj["rank"].GetInt(); + pre_salt = uobj["pre_salt"].GetString(); + post_salt = uobj["post_salt"].GetString(); + password_hash = uobj["password_hash"].GetString(); + banned = uobj["banned"].GetBool(); + + password_reset_token = uobj["password_reset_token"].GetString(); + locked = uobj["locked"].GetBool(); + + const rapidjson::Value &sess = uobj["sessions"].GetArray(); + + for (rapidjson::Value::ConstValueIterator itr = sess.Begin(); itr != sess.End(); ++itr) { + sessions.push_back(itr->GetString()); + } +*/ + register_sessions(); +} + +void DBBasedUser::migrate() { + TableBuilder *tb = DatabaseManager::get_singleton()->ddb->get_table_builder(); + + tb->drop_table_if_exists(_table_name)->run_query(); + tb->drop_table_if_exists(_table_name + "_sessions")->run_query(); + //tb->print(); + + tb->result = ""; + + tb->create_table(_table_name); + tb->integer("id")->not_null()->auto_increment()->next_row(); + tb->varchar("username", 60)->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(); + //tb->print(); + + tb->result = ""; + + tb->create_table(_table_name + "_sessions"); + tb->integer("user_id")->not_null()->next_row(); + tb->varchar("session_id", 100)->next_row(); + tb->foreign_key("user_id"); + tb->references("user", "id"); + tb->ccreate_table(); + //tb->print(); + tb->run(); + + delete tb; +} + +void DBBasedUser::load_all() { + /* + tinydir_dir dir; + if (tinydir_open(&dir, _path.c_str()) == -1) { + return; + } + + while (dir.has_next) { + tinydir_file file; + if (tinydir_readfile(&dir, &file) == -1) { + tinydir_next(&dir); + continue; + } + + if (!file.is_dir) { + std::string np = file.path; + np = np.substr(_path.size(), np.size() - _path.size()); + + DBBasedUser *u = new DBBasedUser(); + u->load(np); + + UserManager::get_singleton()->add_user(u); + } + + tinydir_next(&dir); + } + + tinydir_close(&dir); + */ +} + +DBBasedUser::DBBasedUser() : + User() { +} + +DBBasedUser::~DBBasedUser() { +} + +std::string DBBasedUser::_table_name = "users"; \ No newline at end of file diff --git a/modules/users/db_based_user.h b/modules/users/db_based_user.h new file mode 100644 index 0000000..8bae6b4 --- /dev/null +++ b/modules/users/db_based_user.h @@ -0,0 +1,25 @@ +#ifndef DB_BASED_USER_H +#define DB_BASED_USER_H + +#include "user.h" + +class DBBasedUser : public User { +public: + static std::string get_path(); + static void set_path(const std::string &path); + + void save(); + void load(); + + void migrate(); + + static void load_all(); + + DBBasedUser(); + ~DBBasedUser(); + +protected: + static std::string _table_name; +}; + +#endif \ No newline at end of file diff --git a/modules/users/db_based_user_manager.cpp b/modules/users/db_based_user_manager.cpp new file mode 100644 index 0000000..5c43eba --- /dev/null +++ b/modules/users/db_based_user_manager.cpp @@ -0,0 +1,31 @@ +#include "db_based_user_manager.h" + +#include "db_based_user.h" + +User *DBBasedUserManager::create_user() { + User *u = new DBBasedUser(); + + return u; +} + +void DBBasedUserManager::load_all() { + //DBBasedUser::load_all(); +} + +void DBBasedUserManager::set_table_name(const std::string &path) { + //DBBasedUser::set_path(path); +} + +void DBBasedUserManager::migrate() { + DBBasedUser u; + u.migrate(); +} + +DBBasedUserManager::DBBasedUserManager() : + UserManager() { + + printf("Using DBBasedUserManager.\n"); +} + +DBBasedUserManager::~DBBasedUserManager() { +} diff --git a/modules/users/db_based_user_manager.h b/modules/users/db_based_user_manager.h new file mode 100644 index 0000000..a6015e3 --- /dev/null +++ b/modules/users/db_based_user_manager.h @@ -0,0 +1,26 @@ +#ifndef DB_BASED_USER_MANAGER_H +#define DB_BASED_USER_MANAGER_H + +#include "core/object.h" + +#include "user_manager.h" + +class User; + +class DBBasedUserManager : public UserManager { + +public: + virtual User *create_user(); + void load_all(); + + void set_table_name(const std::string &name); + + virtual void migrate(); + + DBBasedUserManager(); + ~DBBasedUserManager(); + +protected: +}; + +#endif \ No newline at end of file diff --git a/modules/users/user.cpp b/modules/users/user.cpp index 3cf9a81..200aa2e 100644 --- a/modules/users/user.cpp +++ b/modules/users/user.cpp @@ -19,7 +19,13 @@ void User::load() { } void User::load(const std::string &p_name) { - name = p_name; + //name = p_name; + + //load(); +} + +void User::load(const int p_id) { + id = p_id; load(); } diff --git a/modules/users/user.h b/modules/users/user.h index 353caeb..1a1c349 100644 --- a/modules/users/user.h +++ b/modules/users/user.h @@ -27,6 +27,7 @@ public: virtual void save(); virtual void load(); virtual void load(const std::string &p_name); + virtual void load(const int p_id); virtual void changed(); virtual void update();