From 03276bdc26210a3c62dab36193304c9411c3c9b2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 4 Aug 2021 21:29:19 +0200 Subject: [PATCH] Moved the session registration/deregistration code to users. --- modules/users/file_based_user.cpp | 4 +++ modules/users/user.cpp | 46 +++++++++++++++++++++++++++++-- modules/users/user.h | 3 ++ modules/users/user_manager.cpp | 36 +----------------------- 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/modules/users/file_based_user.cpp b/modules/users/file_based_user.cpp index 1ee1cba..ae9e051 100644 --- a/modules/users/file_based_user.cpp +++ b/modules/users/file_based_user.cpp @@ -69,6 +69,8 @@ void FileBasedUser::load(const std::string &p_name) { } void FileBasedUser::load() { + unregister_sessions(); + FILE *f = fopen(_file_path.c_str(), "r"); if (!f) { @@ -108,6 +110,8 @@ void FileBasedUser::load() { for (rapidjson::Value::ConstValueIterator itr = sess.Begin(); itr != sess.End(); ++itr) { sessions.push_back(itr->GetString()); } + + register_sessions(); } FileBasedUser::FileBasedUser() : diff --git a/modules/users/user.cpp b/modules/users/user.cpp index 4061e13..a078aab 100644 --- a/modules/users/user.cpp +++ b/modules/users/user.cpp @@ -1,11 +1,15 @@ #include "user.h" -void User::save() { +#include "core/http/http_session.h" +#include "core/http/session_manager.h" +void User::save() { } void User::load() { - + //unregister_sessions() + //load_code + //register_sessions() } void User::load(const std::string &p_name) { @@ -19,7 +23,44 @@ void User::changed() { } void User::update() { +} +void User::register_sessions() { + if (sessions.size() == 0) { + return; + } + + SessionManager *sm = SessionManager::get_singleton(); + + if (!sm) { + printf("ERROR: UserManager::add_user SessionManager singleton is null, please allocate one!\n"); + return; + } + + for (int i = 0; i < sessions.size(); ++i) { + HTTPSession *session = new HTTPSession(); + session->session_id = sessions[i]; + session->add_object("user", this); + + sm->add_session(session); + } +} + +void User::unregister_sessions() { + if (sessions.size() == 0) { + return; + } + + SessionManager *sm = SessionManager::get_singleton(); + + if (!sm) { + printf("ERROR: UserManager::remove_user SessionManager singleton is null, please allocate one!\n"); + return; + } + + for (int i = 0; i < sessions.size(); ++i) { + sm->delete_session(sessions[i]); + } } User::User() : @@ -32,4 +73,5 @@ User::User() : } User::~User() { + unregister_sessions(); } diff --git a/modules/users/user.h b/modules/users/user.h index f727cae..be534fa 100644 --- a/modules/users/user.h +++ b/modules/users/user.h @@ -26,6 +26,9 @@ public: virtual void changed(); virtual void update(); + void register_sessions(); + void unregister_sessions(); + User(); ~User(); }; diff --git a/modules/users/user_manager.cpp b/modules/users/user_manager.cpp index 2518c8a..36ba4ec 100644 --- a/modules/users/user_manager.cpp +++ b/modules/users/user_manager.cpp @@ -16,21 +16,6 @@ void UserManager::add_user(User *user) { _users_vec.push_back(user); _users[user->name] = user; - - SessionManager *sm = SessionManager::get_singleton(); - - if (!sm) { - printf("ERROR: UserManager::add_user SessionManager singleton is null, please allocate one!\n"); - return; - } - - for (int i = 0; i < user->sessions.size(); ++i) { - HTTPSession *session = new HTTPSession(); - session->session_id = user->sessions[i]; - session->add_object("user", user); - - sm->add_session(session); - } } void UserManager::remove_user(User *user) { @@ -50,17 +35,6 @@ void UserManager::remove_user(User *user) { break; } } - - SessionManager *sm = SessionManager::get_singleton(); - - if (!sm) { - printf("ERROR: UserManager::remove_user SessionManager singleton is null, please allocate one!\n"); - return; - } - - for (int i = 0; i < user->sessions.size(); ++i) { - sm->delete_session(user->sessions[i]); - } } void UserManager::clear() { @@ -69,15 +43,7 @@ void UserManager::clear() { std::lock_guard lock(_mutex); for (int i = 0; i < _users_vec.size(); ++i) { - User *user = _users_vec[i]; - - if (sm) { - for (int i = 0; i < user->sessions.size(); ++i) { - sm->delete_session(user->sessions[i]); - } - } - - delete user; + delete _users_vec[i]; } _users.clear();