More cleanups and moved save from FileBasedUser to User.

This commit is contained in:
Relintai 2021-08-21 20:15:29 +02:00
parent b309705f10
commit 2c8efb0180
5 changed files with 63 additions and 93 deletions

View File

@ -2,10 +2,7 @@
#include <vector> #include <vector>
#include "rapidjson/filewritestream.h"
#include "rapidjson/rapidjson.h" #include "rapidjson/rapidjson.h"
#include "rapidjson/stringbuffer.h"
#include <rapidjson/writer.h>
#include <tinydir/tinydir.h> #include <tinydir/tinydir.h>
#include <cstdio> #include <cstdio>
@ -101,6 +98,9 @@ void Resource::sql_delete_tables(Database *db) {
void Resource::file_save() { void Resource::file_save() {
/* /*
//todo sanitize name!
_file_path = _resource_name + get_id();
FILE *fp = fopen(_file_path.c_str(), "w"); FILE *fp = fopen(_file_path.c_str(), "w");
@ -108,7 +108,7 @@ void Resource::file_save() {
rapidjson::FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer)); rapidjson::FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
rapidjson::Writer<rapidjson::FileWriteStream> writer(os); rapidjson::Writer<rapidjson::FileWriteStream> writer(os);
document->Accept(writer); document.Accept(writer);
fclose(fp); fclose(fp);
*/ */
@ -135,13 +135,4 @@ Resource::Resource() :
} }
Resource::~Resource() { Resource::~Resource() {
std::map<std::string, ResourcePropertyBase *>::iterator it;
for (it = _property_map.begin(); it != _property_map.end(); it++) {
ResourcePropertyBase *p = it->second;
if (p) {
delete p;
}
}
} }

View File

@ -16,17 +16,6 @@ class Database;
class Resource : public Reference { class Resource : public Reference {
RCPP_OBJECT(Resource, 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: public:
int get_id(); int get_id();
void set_id(const int value); void set_id(const int value);
@ -62,43 +51,12 @@ public:
virtual void file_ensure_directory_exist(); virtual void file_ensure_directory_exist();
virtual std::string file_get_base_path(); virtual std::string file_get_base_path();
std::string to_json(rapidjson::Document *into = nullptr); virtual std::string to_json(rapidjson::Document *into = nullptr);
void from_json(const std::string &data); virtual void from_json(const std::string &data);
Resource(); Resource();
~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 <class G, class S>
struct ResourceProperty : public ResourcePropertyBase {
G getter;
S setter;
};
std::map<std::string, ResourcePropertyBase *> _property_map;
private: private:
int _id; int _id;
bool _dirty; bool _dirty;

View File

@ -23,42 +23,7 @@ void FileBasedUser::set_path(const std::string &path) {
} }
void FileBasedUser::save() { 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<rapidjson::FileWriteStream> writer(os);
document.Accept(writer);
fclose(fp);
} }
void FileBasedUser::load(const std::string &p_name) { void FileBasedUser::load(const std::string &p_name) {

View File

@ -1,5 +1,10 @@
#include "user.h" #include "user.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/stringbuffer.h"
#include <rapidjson/writer.h>
#include "core/hash/sha256.h" #include "core/hash/sha256.h"
#include "core/html/form_validator.h" #include "core/html/form_validator.h"
#include "core/html/html_builder.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<rapidjson::StringBuffer> writer(buffer);
document->Accept(writer);
std::string s = buffer.GetString();
delete document;
return s;
}
void User::from_json(const std::string &data) {
}
User::User() : User::User() :
Resource() { Resource() {

View File

@ -72,7 +72,8 @@ public:
void register_sessions(); void register_sessions();
void unregister_sessions(); void unregister_sessions();
void register_properties(); std::string to_json(rapidjson::Document *into = nullptr);
void from_json(const std::string &data);
User(); User();
~User(); ~User();