mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Merged the DBBasedUser into User.
This commit is contained in:
parent
381df9b487
commit
1b190a9963
@ -1,205 +0,0 @@
|
||||
#include "db_based_user.h"
|
||||
|
||||
#include "core/database/database_manager.h"
|
||||
#include "core/database/query_builder.h"
|
||||
#include "core/database/query_result.h"
|
||||
#include "core/database/table_builder.h"
|
||||
#include "user_manager.h"
|
||||
#include <cstdio>
|
||||
|
||||
void DBBasedUser::save() {
|
||||
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
|
||||
if (get_id() == 0) {
|
||||
b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
|
||||
|
||||
b->values();
|
||||
b->eval(_nameui);
|
||||
b->eval(_emailui);
|
||||
b->val(_rank);
|
||||
b->val(_pre_salt);
|
||||
b->val(_post_salt);
|
||||
b->val(_password_hash);
|
||||
b->val(_banned);
|
||||
b->val(_password_reset_token);
|
||||
b->val(_locked);
|
||||
b->cvalues();
|
||||
|
||||
b->end_command();
|
||||
b->select_last_insert_id();
|
||||
|
||||
QueryResult *r = b->run();
|
||||
|
||||
set_id(r->get_last_insert_rowid());
|
||||
|
||||
delete r;
|
||||
|
||||
} else {
|
||||
b->udpate(_table_name);
|
||||
b->set();
|
||||
b->esetp("username", _nameui);
|
||||
b->esetp("email", _emailui);
|
||||
b->setp("rank", _rank);
|
||||
b->setp("pre_salt", _pre_salt);
|
||||
b->setp("post_salt", _post_salt);
|
||||
b->setp("password_hash", _password_hash);
|
||||
b->setp("banned", _banned);
|
||||
b->setp("password_reset_token", _password_reset_token);
|
||||
b->setp("locked", _locked);
|
||||
b->cset();
|
||||
b->where()->wp("id", get_id());
|
||||
|
||||
//b->print();
|
||||
|
||||
b->run_query();
|
||||
}
|
||||
|
||||
if (get_id() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
b->reset();
|
||||
|
||||
b->del(_table_name + "_sessions")->where()->wp("user_id", get_id())->end_command();
|
||||
//b->print();
|
||||
|
||||
b->end_command();
|
||||
b->run_query();
|
||||
|
||||
b->reset();
|
||||
|
||||
for (int i = 0; i < _sessions.size(); ++i) {
|
||||
b->insert(_table_name + "_sessions")->values()->val(get_id())->val(_sessions[i])->cvalues()->end_command();
|
||||
}
|
||||
|
||||
//b->print();
|
||||
|
||||
b->run_query();
|
||||
|
||||
delete b;
|
||||
}
|
||||
|
||||
void DBBasedUser::load() {
|
||||
unregister_sessions();
|
||||
|
||||
if (get_id() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
|
||||
b->select("username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
|
||||
b->from(_table_name);
|
||||
|
||||
b->where()->wp("id", get_id());
|
||||
|
||||
b->end_command();
|
||||
|
||||
QueryResult *r = b->run();
|
||||
|
||||
if (r->next_row()) {
|
||||
_nameui = r->get_cell(0);
|
||||
_emailui = r->get_cell(1);
|
||||
_rank = r->get_cell_int(2);
|
||||
_pre_salt = r->get_cell(3);
|
||||
_post_salt = r->get_cell(4);
|
||||
_password_hash = r->get_cell(5);
|
||||
_banned = r->get_cell_bool(6);
|
||||
_password_reset_token = r->get_cell(7);
|
||||
_locked = r->get_cell_bool(8);
|
||||
}
|
||||
|
||||
delete r;
|
||||
|
||||
b->query_result = "";
|
||||
|
||||
b->select("session_id");
|
||||
b->from(_table_name + "_sessions");
|
||||
b->where()->wp("user_id", get_id());
|
||||
b->end_command();
|
||||
|
||||
r = b->run();
|
||||
|
||||
while (r->next_row()) {
|
||||
_sessions.push_back(r->get_cell(0));
|
||||
}
|
||||
|
||||
delete r;
|
||||
|
||||
delete b;
|
||||
|
||||
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")->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();
|
||||
|
||||
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_query();
|
||||
|
||||
delete tb;
|
||||
}
|
||||
|
||||
void DBBasedUser::load_all() {
|
||||
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
|
||||
b->select("id");
|
||||
b->from(_table_name);
|
||||
b->end_command();
|
||||
b->print();
|
||||
|
||||
QueryResult *r = b->run();
|
||||
|
||||
while (r->next_row()) {
|
||||
DBBasedUser *u = new DBBasedUser();
|
||||
u->set_id(r->get_cell_int(0));
|
||||
u->load();
|
||||
|
||||
//printf("%s\n", u->to_json().c_str());
|
||||
|
||||
UserManager::get_singleton()->add_user(u);
|
||||
}
|
||||
|
||||
delete r;
|
||||
|
||||
delete b;
|
||||
}
|
||||
|
||||
DBBasedUser::DBBasedUser() :
|
||||
User() {
|
||||
}
|
||||
|
||||
DBBasedUser::~DBBasedUser() {
|
||||
}
|
||||
|
||||
std::string DBBasedUser::_table_name = "users";
|
@ -1,25 +0,0 @@
|
||||
#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
|
@ -1,33 +0,0 @@
|
||||
#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");
|
||||
|
||||
User::create_validators();
|
||||
}
|
||||
|
||||
DBBasedUserManager::~DBBasedUserManager() {
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
#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
|
@ -1,14 +1,19 @@
|
||||
#include "user.h"
|
||||
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/filewritestream.h"
|
||||
#include "rapidjson/rapidjson.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include <rapidjson/writer.h>
|
||||
#include "rapidjson/document.h"
|
||||
#include "user_manager.h"
|
||||
#include <rapidjson/writer.h>
|
||||
#include <tinydir/tinydir.h>
|
||||
#include <cstdio>
|
||||
|
||||
#include "core/database/database_manager.h"
|
||||
#include "core/database/query_builder.h"
|
||||
#include "core/database/query_result.h"
|
||||
#include "core/database/table_builder.h"
|
||||
|
||||
#include "core/hash/sha256.h"
|
||||
#include "core/html/form_validator.h"
|
||||
#include "core/html/html_builder.h"
|
||||
@ -90,12 +95,129 @@ void User::set_locked(const bool value) {
|
||||
}
|
||||
|
||||
void User::save() {
|
||||
|
||||
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
|
||||
if (get_id() == 0) {
|
||||
b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
|
||||
|
||||
b->values();
|
||||
b->eval(_nameui);
|
||||
b->eval(_emailui);
|
||||
b->val(_rank);
|
||||
b->val(_pre_salt);
|
||||
b->val(_post_salt);
|
||||
b->val(_password_hash);
|
||||
b->val(_banned);
|
||||
b->val(_password_reset_token);
|
||||
b->val(_locked);
|
||||
b->cvalues();
|
||||
|
||||
b->end_command();
|
||||
b->select_last_insert_id();
|
||||
|
||||
QueryResult *r = b->run();
|
||||
|
||||
set_id(r->get_last_insert_rowid());
|
||||
|
||||
delete r;
|
||||
|
||||
} else {
|
||||
b->udpate(_table_name);
|
||||
b->set();
|
||||
b->esetp("username", _nameui);
|
||||
b->esetp("email", _emailui);
|
||||
b->setp("rank", _rank);
|
||||
b->setp("pre_salt", _pre_salt);
|
||||
b->setp("post_salt", _post_salt);
|
||||
b->setp("password_hash", _password_hash);
|
||||
b->setp("banned", _banned);
|
||||
b->setp("password_reset_token", _password_reset_token);
|
||||
b->setp("locked", _locked);
|
||||
b->cset();
|
||||
b->where()->wp("id", get_id());
|
||||
|
||||
//b->print();
|
||||
|
||||
b->run_query();
|
||||
}
|
||||
|
||||
if (get_id() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
b->reset();
|
||||
|
||||
b->del(_table_name + "_sessions")->where()->wp("user_id", get_id())->end_command();
|
||||
//b->print();
|
||||
|
||||
b->end_command();
|
||||
b->run_query();
|
||||
|
||||
b->reset();
|
||||
|
||||
for (int i = 0; i < _sessions.size(); ++i) {
|
||||
b->insert(_table_name + "_sessions")->values()->val(get_id())->val(_sessions[i])->cvalues()->end_command();
|
||||
}
|
||||
|
||||
//b->print();
|
||||
|
||||
b->run_query();
|
||||
|
||||
delete b;
|
||||
}
|
||||
|
||||
void User::load() {
|
||||
//unregister_sessions()
|
||||
//load_code
|
||||
//register_sessions()
|
||||
|
||||
unregister_sessions();
|
||||
|
||||
if (get_id() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
|
||||
b->select("username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
|
||||
b->from(_table_name);
|
||||
|
||||
b->where()->wp("id", get_id());
|
||||
|
||||
b->end_command();
|
||||
|
||||
QueryResult *r = b->run();
|
||||
|
||||
if (r->next_row()) {
|
||||
_nameui = r->get_cell(0);
|
||||
_emailui = r->get_cell(1);
|
||||
_rank = r->get_cell_int(2);
|
||||
_pre_salt = r->get_cell(3);
|
||||
_post_salt = r->get_cell(4);
|
||||
_password_hash = r->get_cell(5);
|
||||
_banned = r->get_cell_bool(6);
|
||||
_password_reset_token = r->get_cell(7);
|
||||
_locked = r->get_cell_bool(8);
|
||||
}
|
||||
|
||||
delete r;
|
||||
|
||||
b->query_result = "";
|
||||
|
||||
b->select("session_id");
|
||||
b->from(_table_name + "_sessions");
|
||||
b->where()->wp("user_id", get_id());
|
||||
b->end_command();
|
||||
|
||||
r = b->run();
|
||||
|
||||
while (r->next_row()) {
|
||||
_sessions.push_back(r->get_cell(0));
|
||||
}
|
||||
|
||||
delete r;
|
||||
|
||||
delete b;
|
||||
|
||||
register_sessions();
|
||||
}
|
||||
|
||||
void User::load(const std::string &p_name) {
|
||||
@ -117,6 +239,72 @@ void User::changed() {
|
||||
void User::update() {
|
||||
}
|
||||
|
||||
|
||||
void User::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")->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();
|
||||
|
||||
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_query();
|
||||
|
||||
delete tb;
|
||||
}
|
||||
|
||||
void User::db_load_all() {
|
||||
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||
|
||||
b->select("id");
|
||||
b->from(_table_name);
|
||||
b->end_command();
|
||||
b->print();
|
||||
|
||||
QueryResult *r = b->run();
|
||||
|
||||
while (r->next_row()) {
|
||||
User *u = new User();
|
||||
u->set_id(r->get_cell_int(0));
|
||||
u->load();
|
||||
|
||||
//printf("%s\n", u->to_json().c_str());
|
||||
|
||||
UserManager::get_singleton()->add_user(u);
|
||||
}
|
||||
|
||||
delete r;
|
||||
|
||||
delete b;
|
||||
}
|
||||
|
||||
|
||||
bool User::check_password(const std::string &p_password) {
|
||||
return hash_password(p_password) == _password_hash;
|
||||
}
|
||||
@ -763,7 +951,6 @@ void User::file_set_path(const std::string &path) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string User::to_json(rapidjson::Document *into) {
|
||||
rapidjson::Document *document;
|
||||
|
||||
@ -852,4 +1039,5 @@ FormValidator *User::_login_validator = nullptr;
|
||||
FormValidator *User::_registration_validator = nullptr;
|
||||
FormValidator *User::_profile_validator = nullptr;
|
||||
|
||||
std::string User::_path = "./";
|
||||
std::string User::_path = "./";
|
||||
std::string User::_table_name = "users";
|
||||
|
@ -51,6 +51,10 @@ public:
|
||||
virtual void changed();
|
||||
virtual void update();
|
||||
|
||||
void migrate();
|
||||
|
||||
static void db_load_all();
|
||||
|
||||
virtual bool check_password(const std::string &p_password);
|
||||
virtual void create_password(const std::string &p_password);
|
||||
virtual std::string hash_password(const std::string &p_password);
|
||||
@ -105,6 +109,7 @@ protected:
|
||||
std::string _file_path;
|
||||
|
||||
static std::string _path;
|
||||
static std::string _table_name;
|
||||
};
|
||||
|
||||
#endif
|
@ -48,8 +48,19 @@ User *UserManager::create_user() {
|
||||
}
|
||||
|
||||
void UserManager::load_all() {
|
||||
User::db_load_all();
|
||||
}
|
||||
|
||||
void UserManager::set_table_name(const std::string &path) {
|
||||
//DBBasedUser::set_path(path);
|
||||
}
|
||||
|
||||
void UserManager::migrate() {
|
||||
User u;
|
||||
u.migrate();
|
||||
}
|
||||
|
||||
|
||||
void UserManager::clear() {
|
||||
SessionManager *sm = SessionManager::get_singleton();
|
||||
|
||||
@ -75,6 +86,10 @@ UserManager::UserManager() :
|
||||
}
|
||||
|
||||
_self = this;
|
||||
|
||||
printf("Using UserManager.\n");
|
||||
|
||||
User::create_validators();
|
||||
}
|
||||
|
||||
UserManager::~UserManager() {
|
||||
|
@ -18,6 +18,10 @@ public:
|
||||
virtual User *create_user();
|
||||
virtual void load_all();
|
||||
|
||||
void set_table_name(const std::string &name);
|
||||
|
||||
virtual void migrate();
|
||||
|
||||
void clear();
|
||||
|
||||
static UserManager *get_singleton();
|
||||
|
Loading…
Reference in New Issue
Block a user