From 2c8efb0180d45b339c70ecdc3921746b7d69046d Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 21 Aug 2021 20:15:29 +0200 Subject: [PATCH] More cleanups and moved save from FileBasedUser to User. --- core/resource.cpp | 17 +++------- core/resource.h | 46 ++------------------------ modules/users/file_based_user.cpp | 35 -------------------- modules/users/user.cpp | 55 +++++++++++++++++++++++++++++++ modules/users/user.h | 3 +- 5 files changed, 63 insertions(+), 93 deletions(-) diff --git a/core/resource.cpp b/core/resource.cpp index e01d9a3..cbbb927 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -2,10 +2,7 @@ #include -#include "rapidjson/filewritestream.h" #include "rapidjson/rapidjson.h" -#include "rapidjson/stringbuffer.h" -#include #include #include @@ -101,6 +98,9 @@ void Resource::sql_delete_tables(Database *db) { void Resource::file_save() { /* + //todo sanitize name! + _file_path = _resource_name + get_id(); + FILE *fp = fopen(_file_path.c_str(), "w"); @@ -108,7 +108,7 @@ void Resource::file_save() { rapidjson::FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer)); rapidjson::Writer writer(os); - document->Accept(writer); + document.Accept(writer); fclose(fp); */ @@ -135,13 +135,4 @@ Resource::Resource() : } Resource::~Resource() { - std::map::iterator it; - - for (it = _property_map.begin(); it != _property_map.end(); it++) { - ResourcePropertyBase *p = it->second; - - if (p) { - delete p; - } - } } \ No newline at end of file diff --git a/core/resource.h b/core/resource.h index 5edcaae..33fbe85 100644 --- a/core/resource.h +++ b/core/resource.h @@ -16,17 +16,6 @@ class Database; class Resource : public Reference { RCPP_OBJECT(Resource, Reference); -public: - enum ResourcePropertyFlags { - PROPERTY_FLAG_NONE = 0, - PROPERTY_FLAG_SQL_EXCAPE = 1 << 0, - PROPERTY_FLAG_SANITIZE_HTML_SPECIAL_CHARS = 1 << 1, - PROPERTY_FLAG_DONT_SERIALIZE = 1 << 2, - - PROPERTY_FLAG_USER_INPUT = PROPERTY_FLAG_SQL_EXCAPE, - PROPERTY_FLAG_VISIBLE_INPUT = PROPERTY_FLAG_SQL_EXCAPE | PROPERTY_FLAG_SANITIZE_HTML_SPECIAL_CHARS, - }; - public: int get_id(); void set_id(const int value); @@ -62,43 +51,12 @@ public: virtual void file_ensure_directory_exist(); virtual std::string file_get_base_path(); - std::string to_json(rapidjson::Document *into = nullptr); - void from_json(const std::string &data); + virtual std::string to_json(rapidjson::Document *into = nullptr); + virtual void from_json(const std::string &data); Resource(); ~Resource(); -protected: - enum ResourcePropertyType { - TYPE_NULL = 0, - TYPE_INT, - TYPE_FLOAT, - TYPE_STRING, - TYPE_BOOL, - TYPE_RESOURCE, - TYPE_VECTOR_INT, - TYPE_VECTOR_FLOAT, - TYPE_VECTOR_STRING, - TYPE_VECTOR_BOOL, - TYPE_VECTOR_RESOURCE, - }; - - struct ResourcePropertyBase { - ResourcePropertyType type; - - ResourcePropertyBase() { - type = TYPE_NULL; - } - }; - - template - struct ResourceProperty : public ResourcePropertyBase { - G getter; - S setter; - }; - - std::map _property_map; - private: int _id; bool _dirty; diff --git a/modules/users/file_based_user.cpp b/modules/users/file_based_user.cpp index 6138adb..613c3af 100644 --- a/modules/users/file_based_user.cpp +++ b/modules/users/file_based_user.cpp @@ -23,42 +23,7 @@ void FileBasedUser::set_path(const std::string &path) { } void FileBasedUser::save() { - //todo sanitize name! - _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()); - - 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 writer(os); - document.Accept(writer); - - fclose(fp); } void FileBasedUser::load(const std::string &p_name) { diff --git a/modules/users/user.cpp b/modules/users/user.cpp index c441c0b..5df6c21 100644 --- a/modules/users/user.cpp +++ b/modules/users/user.cpp @@ -1,5 +1,10 @@ #include "user.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/rapidjson.h" +#include "rapidjson/stringbuffer.h" +#include + #include "core/hash/sha256.h" #include "core/html/form_validator.h" #include "core/html/html_builder.h" @@ -697,6 +702,56 @@ void User::create_validators() { } } +std::string User::to_json(rapidjson::Document *into) { + rapidjson::Document *document; + + if (into) { + document = into; + } else { + document = new rapidjson::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()); + + 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()); + + if (into) { + return ""; + } + + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + document->Accept(writer); + + std::string s = buffer.GetString(); + + delete document; + + return s; +} +void User::from_json(const std::string &data) { + +} + User::User() : Resource() { diff --git a/modules/users/user.h b/modules/users/user.h index bcc009d..2dd9779 100644 --- a/modules/users/user.h +++ b/modules/users/user.h @@ -72,7 +72,8 @@ public: void register_sessions(); void unregister_sessions(); - void register_properties(); + std::string to_json(rapidjson::Document *into = nullptr); + void from_json(const std::string &data); User(); ~User();