From 17498aaf1d447e71b94e34291b087f02e3934f15 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 5 Aug 2021 20:02:29 +0200 Subject: [PATCH] Implemented loading all file based users. --- modules/users/file_based_user.cpp | 36 +++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/modules/users/file_based_user.cpp b/modules/users/file_based_user.cpp index ba53044..4909174 100644 --- a/modules/users/file_based_user.cpp +++ b/modules/users/file_based_user.cpp @@ -3,10 +3,10 @@ #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/rapidjson.h" +#include "user_manager.h" #include #include #include -#include "user_manager.h" std::string FileBasedUser::get_path() { return _path; @@ -23,6 +23,7 @@ void FileBasedUser::set_path(const std::string &path) { } void FileBasedUser::save() { + //todo sanitize name! _file_path = _path + name; rapidjson::Document document; @@ -30,7 +31,6 @@ void FileBasedUser::save() { document.AddMember("id", id, document.GetAllocator()); - document.AddMember("name", rapidjson::Value(name.c_str(), document.GetAllocator()), document.GetAllocator()); document.AddMember("email", rapidjson::Value(email.c_str(), document.GetAllocator()), document.GetAllocator()); document.AddMember("rank", rank, document.GetAllocator()); @@ -62,9 +62,8 @@ void FileBasedUser::save() { } void FileBasedUser::load(const std::string &p_name) { - name = p_name; - - _file_path = _path + name; + //todo sanitize name! + _file_path = _path + p_name; load(); } @@ -116,7 +115,32 @@ void FileBasedUser::load() { } 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() :