diff --git a/modules/users/db_based_user.cpp b/modules/users/db_based_user.cpp index a077f13..fb307e1 100644 --- a/modules/users/db_based_user.cpp +++ b/modules/users/db_based_user.cpp @@ -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->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->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(); @@ -39,15 +39,15 @@ void DBBasedUser::save() { } 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->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()); @@ -70,8 +70,8 @@ void DBBasedUser::save() { 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(); + 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(); @@ -104,15 +104,15 @@ void DBBasedUser::load() { 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); + _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; @@ -127,7 +127,7 @@ void DBBasedUser::load() { r = b->run(); while (r->next_row()) { - sessions.push_back(r->get_cell(0)); + _sessions.push_back(r->get_cell(0)); } delete r; diff --git a/modules/users/file_based_user.cpp b/modules/users/file_based_user.cpp index afe16d6..6138adb 100644 --- a/modules/users/file_based_user.cpp +++ b/modules/users/file_based_user.cpp @@ -24,28 +24,28 @@ void FileBasedUser::set_path(const std::string &path) { void FileBasedUser::save() { //todo sanitize name! - _file_path = _path + nameui; + _file_path = _path + _nameui; rapidjson::Document document; document.SetObject(); document.AddMember("id", get_id(), 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("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()); + 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("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); + 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()); @@ -94,21 +94,21 @@ void FileBasedUser::load() { rapidjson::Value uobj = data.GetObject(); set_id(uobj["id"].GetInt()); - nameui = uobj["name"].GetString(); - emailui = 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(); + _nameui = uobj["name"].GetString(); + _emailui = 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(); + _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()); + _sessions.push_back(itr->GetString()); } register_sessions(); diff --git a/modules/users/user.cpp b/modules/users/user.cpp index 330b9ed..d5db48c 100644 --- a/modules/users/user.cpp +++ b/modules/users/user.cpp @@ -10,6 +10,76 @@ #include "core/utils.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 User::get_sessions() { + return _sessions; +} +void User::set_sessions(const std::vector &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() { } @@ -39,21 +109,21 @@ void User::update() { } 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) { //todo improve a bit - pre_salt = hash_password(nameui + emailui); - post_salt = hash_password(emailui + nameui); + _pre_salt = hash_password(_nameui + _emailui); + _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) { 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); @@ -63,7 +133,7 @@ std::string User::hash_password(const std::string &p_password) { } void User::register_sessions() { - if (sessions.size() == 0) { + if (_sessions.size() == 0) { return; } @@ -76,9 +146,9 @@ void User::register_sessions() { _mutex.lock(); - for (int i = 0; i < sessions.size(); ++i) { + for (int i = 0; i < _sessions.size(); ++i) { HTTPSession *session = new HTTPSession(); - session->session_id = sessions[i]; + session->session_id = _sessions[i]; session->add_object("user", this); sm->add_session(session); @@ -88,7 +158,7 @@ void User::register_sessions() { } void User::unregister_sessions() { - if (sessions.size() == 0) { + if (_sessions.size() == 0) { return; } @@ -101,8 +171,8 @@ void User::unregister_sessions() { _mutex.lock(); - for (int i = 0; i < sessions.size(); ++i) { - sm->delete_session(sessions[i]); + for (int i = 0; i < _sessions.size(); ++i) { + sm->delete_session(_sessions[i]); } _mutex.unlock(); @@ -169,7 +239,7 @@ void User::handle_login_request_default(Request *request) { session->add_object("user", user); user->_mutex.lock(); - user->sessions.push_back(session->session_id); + user->_sessions.push_back(session->session_id); user->_mutex.unlock(); user->save(); @@ -270,7 +340,7 @@ void User::handle_register_request_default(Request *request) { continue; } - if (u->emailui == email_val) { + if (u->_emailui == email_val) { email_found = true; break; } @@ -287,10 +357,10 @@ void User::handle_register_request_default(Request *request) { if (error_str.size() == 0) { user = UserManager::get_singleton()->create_user(); - user->nameui = uname_val; - user->emailui = email_val; + user->_nameui = uname_val; + user->_emailui = email_val; //todo - user->rank = 1; + user->_rank = 1; user->create_password(pass_val); user->save(); @@ -425,11 +495,11 @@ void User::handle_settings_request(Request *request) { } if (valid) { - if (uname_val == nameui) { + if (uname_val == _nameui) { uname_val = ""; } - if (email_val == emailui) { + if (email_val == _emailui) { email_val = ""; } @@ -440,7 +510,7 @@ void User::handle_settings_request(Request *request) { error_str += "Username already taken!
"; } else { //todo sanitize for html special chars! - nameui = uname_val; + _nameui = uname_val; changed = true; uname_val = ""; } @@ -463,7 +533,7 @@ void User::handle_settings_request(Request *request) { continue; } - if (u->emailui == email_val) { + if (u->_emailui == email_val) { email_found = true; break; } @@ -474,7 +544,7 @@ void User::handle_settings_request(Request *request) { } else { //todo sanitize for html special chars! //also send email - emailui = email_val; + _emailui = email_val; changed = true; email_val = ""; } @@ -515,13 +585,13 @@ void User::handle_settings_request(Request *request) { b.w("Username"); 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.br(); b.w("Email"); 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.br(); @@ -557,10 +627,10 @@ void User::handle_logout_request(Request *request) { _mutex.lock(); - for (int i = 0; i < sessions.size(); ++i) { - if (sessions[i] == request->session->session_id) { - sessions[i] = sessions[sessions.size() - 1]; - sessions.pop_back(); + for (int i = 0; i < _sessions.size(); ++i) { + if (_sessions[i] == request->session->session_id) { + _sessions[i] = _sessions[_sessions.size() - 1]; + _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() : Resource() { - rank = 0; - banned = false; - locked = false; + _rank = 0; + _banned = false; + _locked = false; } User::~User() { diff --git a/modules/users/user.h b/modules/users/user.h index 6d4dd99..bcc009d 100644 --- a/modules/users/user.h +++ b/modules/users/user.h @@ -3,9 +3,9 @@ #include "core/resource.h" +#include #include #include -#include class Request; class FormValidator; @@ -14,16 +14,35 @@ class User : public Resource { RCPP_OBJECT(User, Resource); public: - std::string nameui; - std::string emailui; - int rank; - std::string pre_salt; - std::string post_salt; - std::string password_hash; - bool banned; - std::vector sessions; - std::string password_reset_token; - bool locked; + std::string get_name_ui(); + void set_name_ui(const std::string &value); + + std::string get_email_ui(); + void set_email_ui(const std::string &value); + + int get_rank(); + void set_rank(const int value); + + 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 get_sessions(); + void set_sessions(const std::vector &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 load(); @@ -53,9 +72,23 @@ public: void register_sessions(); void unregister_sessions(); + void register_properties(); + 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 _sessions; + std::string _password_reset_token; + bool _locked; + static FormValidator *_login_validator; static FormValidator *_registration_validator; static FormValidator *_profile_validator; diff --git a/modules/users/user_manager.cpp b/modules/users/user_manager.cpp index a0a4874..f09d804 100644 --- a/modules/users/user_manager.cpp +++ b/modules/users/user_manager.cpp @@ -15,7 +15,7 @@ void UserManager::add_user(User *user) { std::lock_guard lock(_mutex); _users_vec.push_back(user); - _users[user->nameui] = user; + _users[user->get_name_ui()] = user; } void UserManager::remove_user(User *user) { @@ -26,7 +26,7 @@ void UserManager::remove_user(User *user) { std::lock_guard lock(_mutex); - _users.erase(user->nameui); + _users.erase(user->get_name_ui()); for (int i = 0; i < _users_vec.size(); ++i) { if (_users_vec[i] == user) {