mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Added a new database based user. Still wip.
This commit is contained in:
parent
d2a0f25369
commit
e7c1cd5741
184
modules/users/db_based_user.cpp
Normal file
184
modules/users/db_based_user.cpp
Normal file
@ -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 <cstdio>
|
||||
|
||||
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<rapidjson::FileWriteStream> 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";
|
25
modules/users/db_based_user.h
Normal file
25
modules/users/db_based_user.h
Normal file
@ -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
|
31
modules/users/db_based_user_manager.cpp
Normal file
31
modules/users/db_based_user_manager.cpp
Normal file
@ -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() {
|
||||
}
|
26
modules/users/db_based_user_manager.h
Normal file
26
modules/users/db_based_user_manager.h
Normal file
@ -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
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user