Added getters to the User.

This commit is contained in:
Relintai 2021-08-21 18:28:29 +02:00
parent c5dcb3106e
commit 242412dd46
5 changed files with 207 additions and 95 deletions

View File

@ -16,15 +16,15 @@ void DBBasedUser::save() {
b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked"); b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
b->values(); b->values();
b->eval(nameui); b->eval(_nameui);
b->eval(emailui); b->eval(_emailui);
b->val(rank); b->val(_rank);
b->val(pre_salt); b->val(_pre_salt);
b->val(post_salt); b->val(_post_salt);
b->val(password_hash); b->val(_password_hash);
b->val(banned); b->val(_banned);
b->val(password_reset_token); b->val(_password_reset_token);
b->val(locked); b->val(_locked);
b->cvalues(); b->cvalues();
b->end_command(); b->end_command();
@ -39,15 +39,15 @@ void DBBasedUser::save() {
} else { } else {
b->udpate(_table_name); b->udpate(_table_name);
b->set(); b->set();
b->esetp("username", nameui); b->esetp("username", _nameui);
b->esetp("email", emailui); b->esetp("email", _emailui);
b->setp("rank", rank); b->setp("rank", _rank);
b->setp("pre_salt", pre_salt); b->setp("pre_salt", _pre_salt);
b->setp("post_salt", post_salt); b->setp("post_salt", _post_salt);
b->setp("password_hash", password_hash); b->setp("password_hash", _password_hash);
b->setp("banned", banned); b->setp("banned", _banned);
b->setp("password_reset_token", password_reset_token); b->setp("password_reset_token", _password_reset_token);
b->setp("locked", locked); b->setp("locked", _locked);
b->cset(); b->cset();
b->where()->wp("id", get_id()); b->where()->wp("id", get_id());
@ -70,8 +70,8 @@ void DBBasedUser::save() {
b->reset(); b->reset();
for (int i = 0; i < sessions.size(); ++i) { for (int i = 0; i < _sessions.size(); ++i) {
b->insert(_table_name + "_sessions")->values()->val(get_id())->val(sessions[i])->cvalues()->end_command(); b->insert(_table_name + "_sessions")->values()->val(get_id())->val(_sessions[i])->cvalues()->end_command();
} }
//b->print(); //b->print();
@ -104,15 +104,15 @@ void DBBasedUser::load() {
QueryResult *r = b->run(); QueryResult *r = b->run();
if (r->next_row()) { if (r->next_row()) {
nameui = r->get_cell(0); _nameui = r->get_cell(0);
emailui = r->get_cell(1); _emailui = r->get_cell(1);
rank = r->get_cell_int(2); _rank = r->get_cell_int(2);
pre_salt = r->get_cell(3); _pre_salt = r->get_cell(3);
post_salt = r->get_cell(4); _post_salt = r->get_cell(4);
password_hash = r->get_cell(5); _password_hash = r->get_cell(5);
banned = r->get_cell_bool(6); _banned = r->get_cell_bool(6);
password_reset_token = r->get_cell(7); _password_reset_token = r->get_cell(7);
locked = r->get_cell_bool(8); _locked = r->get_cell_bool(8);
} }
delete r; delete r;
@ -127,7 +127,7 @@ void DBBasedUser::load() {
r = b->run(); r = b->run();
while (r->next_row()) { while (r->next_row()) {
sessions.push_back(r->get_cell(0)); _sessions.push_back(r->get_cell(0));
} }
delete r; delete r;

View File

@ -24,28 +24,28 @@ void FileBasedUser::set_path(const std::string &path) {
void FileBasedUser::save() { void FileBasedUser::save() {
//todo sanitize name! //todo sanitize name!
_file_path = _path + nameui; _file_path = _path + _nameui;
rapidjson::Document document; rapidjson::Document document;
document.SetObject(); document.SetObject();
document.AddMember("id", get_id(), document.GetAllocator()); document.AddMember("id", get_id(), document.GetAllocator());
document.AddMember("name", rapidjson::Value(nameui.c_str(), document.GetAllocator()), document.GetAllocator()); document.AddMember("name", rapidjson::Value(_nameui.c_str(), document.GetAllocator()), document.GetAllocator());
document.AddMember("email", rapidjson::Value(emailui.c_str(), document.GetAllocator()), document.GetAllocator()); document.AddMember("email", rapidjson::Value(_emailui.c_str(), document.GetAllocator()), document.GetAllocator());
document.AddMember("rank", rank, document.GetAllocator()); document.AddMember("rank", _rank, document.GetAllocator());
document.AddMember("pre_salt", rapidjson::Value(pre_salt.c_str(), document.GetAllocator()), 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("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("password_hash", rapidjson::Value(_password_hash.c_str(), document.GetAllocator()), document.GetAllocator());
document.AddMember("banned", banned, 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("password_reset_token", rapidjson::Value(_password_reset_token.c_str(), document.GetAllocator()), document.GetAllocator());
document.AddMember("locked", locked, document.GetAllocator()); document.AddMember("locked", _locked, document.GetAllocator());
rapidjson::Value sa(rapidjson::Type::kArrayType); rapidjson::Value sa(rapidjson::Type::kArrayType);
rapidjson::Document::AllocatorType &allocator = document.GetAllocator(); rapidjson::Document::AllocatorType &allocator = document.GetAllocator();
for (int i = 0; i < sessions.size(); i++) { for (int i = 0; i < _sessions.size(); i++) {
sa.PushBack(rapidjson::Value(sessions[i].c_str(), document.GetAllocator()), allocator); sa.PushBack(rapidjson::Value(_sessions[i].c_str(), document.GetAllocator()), allocator);
} }
document.AddMember("sessions", sa, document.GetAllocator()); document.AddMember("sessions", sa, document.GetAllocator());
@ -94,21 +94,21 @@ void FileBasedUser::load() {
rapidjson::Value uobj = data.GetObject(); rapidjson::Value uobj = data.GetObject();
set_id(uobj["id"].GetInt()); set_id(uobj["id"].GetInt());
nameui = uobj["name"].GetString(); _nameui = uobj["name"].GetString();
emailui = uobj["email"].GetString(); _emailui = uobj["email"].GetString();
rank = uobj["rank"].GetInt(); _rank = uobj["rank"].GetInt();
pre_salt = uobj["pre_salt"].GetString(); _pre_salt = uobj["pre_salt"].GetString();
post_salt = uobj["post_salt"].GetString(); _post_salt = uobj["post_salt"].GetString();
password_hash = uobj["password_hash"].GetString(); _password_hash = uobj["password_hash"].GetString();
banned = uobj["banned"].GetBool(); _banned = uobj["banned"].GetBool();
password_reset_token = uobj["password_reset_token"].GetString(); _password_reset_token = uobj["password_reset_token"].GetString();
locked = uobj["locked"].GetBool(); _locked = uobj["locked"].GetBool();
const rapidjson::Value &sess = uobj["sessions"].GetArray(); const rapidjson::Value &sess = uobj["sessions"].GetArray();
for (rapidjson::Value::ConstValueIterator itr = sess.Begin(); itr != sess.End(); ++itr) { for (rapidjson::Value::ConstValueIterator itr = sess.Begin(); itr != sess.End(); ++itr) {
sessions.push_back(itr->GetString()); _sessions.push_back(itr->GetString());
} }
register_sessions(); register_sessions();

View File

@ -10,6 +10,76 @@
#include "core/utils.h" #include "core/utils.h"
#include "user_manager.h" #include "user_manager.h"
std::string User::get_name_ui() {
return _nameui;
}
void User::set_name_ui(const std::string &value) {
_nameui = value;
}
std::string User::get_email_ui() {
return _emailui;
}
void User::set_email_ui(const std::string &value) {
_emailui = value;
}
int User::get_rank() {
return _rank;
}
void User::set_rank(const int value) {
_rank = value;
}
std::string User::get_pre_salt() {
return _pre_salt;
}
void User::set_pre_salt(const std::string &value) {
_pre_salt = value;
}
std::string User::get_post_salt() {
return _post_salt;
}
void User::set_post_salt(const std::string &value) {
_post_salt = value;
}
std::string User::get_password_hash() {
return _password_hash;
}
void User::set_password_hash(const std::string &value) {
_password_hash = value;
}
bool User::get_banned() {
return _banned;
}
void User::set_banned(const bool value) {
_banned = value;
}
std::vector<std::string> User::get_sessions() {
return _sessions;
}
void User::set_sessions(const std::vector<std::string> &value) {
_sessions = value;
}
std::string User::get_password_reset_token() {
return _password_reset_token;
}
void User::set_password_reset_token(const std::string &value) {
_password_reset_token = value;
}
bool User::get_locked() {
return _locked;
}
void User::set_locked(const bool value) {
_locked = value;
}
void User::save() { void User::save() {
} }
@ -39,21 +109,21 @@ void User::update() {
} }
bool User::check_password(const std::string &p_password) { bool User::check_password(const std::string &p_password) {
return hash_password(p_password) == password_hash; return hash_password(p_password) == _password_hash;
} }
void User::create_password(const std::string &p_password) { void User::create_password(const std::string &p_password) {
//todo improve a bit //todo improve a bit
pre_salt = hash_password(nameui + emailui); _pre_salt = hash_password(_nameui + _emailui);
post_salt = hash_password(emailui + nameui); _post_salt = hash_password(_emailui + _nameui);
password_hash = hash_password(p_password); _password_hash = hash_password(p_password);
} }
std::string User::hash_password(const std::string &p_password) { std::string User::hash_password(const std::string &p_password) {
SHA256 *s = SHA256::get(); SHA256 *s = SHA256::get();
std::string p = pre_salt + p_password + post_salt; std::string p = _pre_salt + p_password + _post_salt;
std::string c = s->compute(p); std::string c = s->compute(p);
@ -63,7 +133,7 @@ std::string User::hash_password(const std::string &p_password) {
} }
void User::register_sessions() { void User::register_sessions() {
if (sessions.size() == 0) { if (_sessions.size() == 0) {
return; return;
} }
@ -76,9 +146,9 @@ void User::register_sessions() {
_mutex.lock(); _mutex.lock();
for (int i = 0; i < sessions.size(); ++i) { for (int i = 0; i < _sessions.size(); ++i) {
HTTPSession *session = new HTTPSession(); HTTPSession *session = new HTTPSession();
session->session_id = sessions[i]; session->session_id = _sessions[i];
session->add_object("user", this); session->add_object("user", this);
sm->add_session(session); sm->add_session(session);
@ -88,7 +158,7 @@ void User::register_sessions() {
} }
void User::unregister_sessions() { void User::unregister_sessions() {
if (sessions.size() == 0) { if (_sessions.size() == 0) {
return; return;
} }
@ -101,8 +171,8 @@ void User::unregister_sessions() {
_mutex.lock(); _mutex.lock();
for (int i = 0; i < sessions.size(); ++i) { for (int i = 0; i < _sessions.size(); ++i) {
sm->delete_session(sessions[i]); sm->delete_session(_sessions[i]);
} }
_mutex.unlock(); _mutex.unlock();
@ -169,7 +239,7 @@ void User::handle_login_request_default(Request *request) {
session->add_object("user", user); session->add_object("user", user);
user->_mutex.lock(); user->_mutex.lock();
user->sessions.push_back(session->session_id); user->_sessions.push_back(session->session_id);
user->_mutex.unlock(); user->_mutex.unlock();
user->save(); user->save();
@ -270,7 +340,7 @@ void User::handle_register_request_default(Request *request) {
continue; continue;
} }
if (u->emailui == email_val) { if (u->_emailui == email_val) {
email_found = true; email_found = true;
break; break;
} }
@ -287,10 +357,10 @@ void User::handle_register_request_default(Request *request) {
if (error_str.size() == 0) { if (error_str.size() == 0) {
user = UserManager::get_singleton()->create_user(); user = UserManager::get_singleton()->create_user();
user->nameui = uname_val; user->_nameui = uname_val;
user->emailui = email_val; user->_emailui = email_val;
//todo //todo
user->rank = 1; user->_rank = 1;
user->create_password(pass_val); user->create_password(pass_val);
user->save(); user->save();
@ -425,11 +495,11 @@ void User::handle_settings_request(Request *request) {
} }
if (valid) { if (valid) {
if (uname_val == nameui) { if (uname_val == _nameui) {
uname_val = ""; uname_val = "";
} }
if (email_val == emailui) { if (email_val == _emailui) {
email_val = ""; email_val = "";
} }
@ -440,7 +510,7 @@ void User::handle_settings_request(Request *request) {
error_str += "Username already taken!<br>"; error_str += "Username already taken!<br>";
} else { } else {
//todo sanitize for html special chars! //todo sanitize for html special chars!
nameui = uname_val; _nameui = uname_val;
changed = true; changed = true;
uname_val = ""; uname_val = "";
} }
@ -463,7 +533,7 @@ void User::handle_settings_request(Request *request) {
continue; continue;
} }
if (u->emailui == email_val) { if (u->_emailui == email_val) {
email_found = true; email_found = true;
break; break;
} }
@ -474,7 +544,7 @@ void User::handle_settings_request(Request *request) {
} else { } else {
//todo sanitize for html special chars! //todo sanitize for html special chars!
//also send email //also send email
emailui = email_val; _emailui = email_val;
changed = true; changed = true;
email_val = ""; email_val = "";
} }
@ -515,13 +585,13 @@ void User::handle_settings_request(Request *request) {
b.w("Username"); b.w("Username");
b.br(); b.br();
b.input()->type("text")->name("username")->placeholder(nameui)->value(uname_val); b.input()->type("text")->name("username")->placeholder(_nameui)->value(uname_val);
b.cinput(); b.cinput();
b.br(); b.br();
b.w("Email"); b.w("Email");
b.br(); b.br();
b.input()->type("email")->name("email")->placeholder(emailui)->value(email_val); b.input()->type("email")->name("email")->placeholder(_emailui)->value(email_val);
b.cinput(); b.cinput();
b.br(); b.br();
@ -557,10 +627,10 @@ void User::handle_logout_request(Request *request) {
_mutex.lock(); _mutex.lock();
for (int i = 0; i < sessions.size(); ++i) { for (int i = 0; i < _sessions.size(); ++i) {
if (sessions[i] == request->session->session_id) { if (_sessions[i] == request->session->session_id) {
sessions[i] = sessions[sessions.size() - 1]; _sessions[i] = _sessions[_sessions.size() - 1];
sessions.pop_back(); _sessions.pop_back();
} }
} }
@ -627,12 +697,21 @@ void User::create_validators() {
} }
} }
void User::register_properties() {
Resource::register_properties();
//add_property_int("id", &Resource::get_id, &Resource::set_id);
//add_property_string("nameui", &User::get_name_ui, &User::set_name_ui);
//add_property_string("nameui", &User::get_name_ui, &User::set_name_ui);
}
User::User() : User::User() :
Resource() { Resource() {
rank = 0; _rank = 0;
banned = false; _banned = false;
locked = false; _locked = false;
} }
User::~User() { User::~User() {

View File

@ -3,9 +3,9 @@
#include "core/resource.h" #include "core/resource.h"
#include <mutex>
#include <string> #include <string>
#include <vector> #include <vector>
#include <mutex>
class Request; class Request;
class FormValidator; class FormValidator;
@ -14,16 +14,35 @@ class User : public Resource {
RCPP_OBJECT(User, Resource); RCPP_OBJECT(User, Resource);
public: public:
std::string nameui; std::string get_name_ui();
std::string emailui; void set_name_ui(const std::string &value);
int rank;
std::string pre_salt; std::string get_email_ui();
std::string post_salt; void set_email_ui(const std::string &value);
std::string password_hash;
bool banned; int get_rank();
std::vector<std::string> sessions; void set_rank(const int value);
std::string password_reset_token;
bool locked; std::string get_pre_salt();
void set_pre_salt(const std::string &value);
std::string get_post_salt();
void set_post_salt(const std::string &value);
std::string get_password_hash();
void set_password_hash(const std::string &value);
bool get_banned();
void set_banned(const bool value);
std::vector<std::string> get_sessions();
void set_sessions(const std::vector<std::string> &value);
std::string get_password_reset_token();
void set_password_reset_token(const std::string &value);
bool get_locked();
void set_locked(const bool value);
virtual void save(); virtual void save();
virtual void load(); virtual void load();
@ -53,9 +72,23 @@ public:
void register_sessions(); void register_sessions();
void unregister_sessions(); void unregister_sessions();
void register_properties();
User(); User();
~User(); ~User();
protected:
std::string _nameui;
std::string _emailui;
int _rank;
std::string _pre_salt;
std::string _post_salt;
std::string _password_hash;
bool _banned;
std::vector<std::string> _sessions;
std::string _password_reset_token;
bool _locked;
static FormValidator *_login_validator; static FormValidator *_login_validator;
static FormValidator *_registration_validator; static FormValidator *_registration_validator;
static FormValidator *_profile_validator; static FormValidator *_profile_validator;

View File

@ -15,7 +15,7 @@ void UserManager::add_user(User *user) {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
_users_vec.push_back(user); _users_vec.push_back(user);
_users[user->nameui] = user; _users[user->get_name_ui()] = user;
} }
void UserManager::remove_user(User *user) { void UserManager::remove_user(User *user) {
@ -26,7 +26,7 @@ void UserManager::remove_user(User *user) {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
_users.erase(user->nameui); _users.erase(user->get_name_ui());
for (int i = 0; i < _users_vec.size(); ++i) { for (int i = 0; i < _users_vec.size(); ++i) {
if (_users_vec[i] == user) { if (_users_vec[i] == user) {