diff --git a/modules/users/file_based_user.cpp b/modules/users/file_based_user.cpp deleted file mode 100644 index 032dee0..0000000 --- a/modules/users/file_based_user.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#include "file_based_user.h" - -#include "rapidjson/document.h" -#include "rapidjson/filewritestream.h" -#include "rapidjson/rapidjson.h" -#include "user_manager.h" -#include -#include -#include - -std::string FileBasedUser::get_path() { - return _path; -} - -void FileBasedUser::set_path(const std::string &path) { - _path = path; - - if (_path.size() > 0) { - if (_path[_path.size() - 1] != '/') { - _path += '/'; - } - } -} - -void FileBasedUser::save() { - -} - -void FileBasedUser::load(const std::string &p_name) { - //todo sanitize name! - //_file_path = _path + p_name; - - //load(); -} - -void FileBasedUser::load() { - unregister_sessions(); - - FILE *f = fopen(_file_path.c_str(), "r"); - - if (!f) { - printf("FileBasedUser::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(); - - 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(); - - _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 FileBasedUser::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()); - - FileBasedUser *u = new FileBasedUser(); - u->load(np); - - UserManager::get_singleton()->add_user(u); - } - - tinydir_next(&dir); - } - - tinydir_close(&dir); -} - -FileBasedUser::FileBasedUser() : - User() { -} - -FileBasedUser::~FileBasedUser() { -} - -std::string FileBasedUser::_path = "./"; \ No newline at end of file diff --git a/modules/users/file_based_user.h b/modules/users/file_based_user.h deleted file mode 100644 index 63c9632..0000000 --- a/modules/users/file_based_user.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef FILE_BASED_USER_H -#define FILE_BASED_USER_H - -#include "user.h" - -class FileBasedUser : public User { -public: - static std::string get_path(); - static void set_path(const std::string &path); - - void save(); - void load(const std::string &p_name); - void load(); - - static void load_all(); - - FileBasedUser(); - ~FileBasedUser(); - -protected: - std::string _file_path; - - static std::string _path; -}; - -#endif \ No newline at end of file diff --git a/modules/users/file_based_user_manager.cpp b/modules/users/file_based_user_manager.cpp deleted file mode 100644 index d35a0ec..0000000 --- a/modules/users/file_based_user_manager.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "file_based_user_manager.h" - -#include "file_based_user.h" - -User *FileBasedUserManager::create_user() { - User *u = new FileBasedUser(); - - return u; -} - -void FileBasedUserManager::load_all() { - FileBasedUser::load_all(); -} - -void FileBasedUserManager::set_path(const std::string &path) { - FileBasedUser::set_path(path); -} - -FileBasedUserManager::FileBasedUserManager() : - UserManager() { - - printf("Using FileBasedUserManager. Note: Please DON'T USE THIS IN PRODUCTION IT'S NOT SAFE FOR THAT!\n"); -} - -FileBasedUserManager::~FileBasedUserManager() { -} diff --git a/modules/users/file_based_user_manager.h b/modules/users/file_based_user_manager.h deleted file mode 100644 index a073a59..0000000 --- a/modules/users/file_based_user_manager.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef FILE_BASED_USER_MANAGER_H -#define FILE_BASED_USER_MANAGER_H - -#include "core/object.h" - -#include "user_manager.h" - -class User; - -class FileBasedUserManager : public UserManager { - -public: - virtual User *create_user(); - void load_all(); - - void set_path(const std::string &path); - - FileBasedUserManager(); - ~FileBasedUserManager(); - -protected: -}; - -#endif \ No newline at end of file diff --git a/modules/users/user.cpp b/modules/users/user.cpp index c117098..55b38d7 100644 --- a/modules/users/user.cpp +++ b/modules/users/user.cpp @@ -4,6 +4,10 @@ #include "rapidjson/rapidjson.h" #include "rapidjson/stringbuffer.h" #include +#include "rapidjson/document.h" +#include "user_manager.h" +#include +#include #include "core/hash/sha256.h" #include "core/html/form_validator.h" @@ -688,13 +692,85 @@ void User::create_validators() { } } +void User::file_save() { +} +void User::file_load() { + FILE *f = fopen(_file_path.c_str(), "r"); + + if (!f) { + printf("FileBasedUser::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); + + from_json(fd); +} +void User::file_ensure_directory_exist() { +} +std::string User::file_get_base_path() { + return _path; +} + +void User::file_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()); + + User *u = new User(); + u->load(np); + + UserManager::get_singleton()->add_user(u); + } + + tinydir_next(&dir); + } + + tinydir_close(&dir); +} + +std::string User::file_get_path() { + return _path; +} + +void User::file_set_path(const std::string &path) { + _path = path; + + if (_path.size() > 0) { + if (_path[_path.size() - 1] != '/') { + _path += '/'; + } + } +} + + std::string User::to_json(rapidjson::Document *into) { rapidjson::Document *document; if (into) { document = into; } else { - document = new rapidjson::Document(); + document = new rapidjson::Document(); } document->SetObject(); @@ -734,8 +810,30 @@ std::string User::to_json(rapidjson::Document *into) { return s; } -void User::from_json(const std::string &data) { +void User::from_json(const std::string &p_data) { + rapidjson::Document data; + data.Parse(p_data.c_str()); + + 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(); + + _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()); + } } User::User() : @@ -753,3 +851,5 @@ User::~User() { FormValidator *User::_login_validator = nullptr; FormValidator *User::_registration_validator = nullptr; FormValidator *User::_profile_validator = nullptr; + +std::string User::_path = "./"; \ No newline at end of file diff --git a/modules/users/user.h b/modules/users/user.h index 72c2ba1..832574e 100644 --- a/modules/users/user.h +++ b/modules/users/user.h @@ -72,6 +72,14 @@ public: void register_sessions(); void unregister_sessions(); + void file_save(); + void file_load(); + void file_ensure_directory_exist(); + std::string file_get_base_path(); + static void file_load_all(); + static std::string file_get_path(); + static void file_set_path(const std::string &path); + std::string to_json(rapidjson::Document *into = nullptr); void from_json(const std::string &data); @@ -93,6 +101,10 @@ protected: static FormValidator *_login_validator; static FormValidator *_registration_validator; static FormValidator *_profile_validator; + + std::string _file_path; + + static std::string _path; }; #endif \ No newline at end of file