Moved the session registration/deregistration code to users.

This commit is contained in:
Relintai 2021-08-04 21:29:19 +02:00
parent fbd442317e
commit 03276bdc26
4 changed files with 52 additions and 37 deletions

View File

@ -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() :

View File

@ -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();
}

View File

@ -26,6 +26,9 @@ public:
virtual void changed();
virtual void update();
void register_sessions();
void unregister_sessions();
User();
~User();
};

View File

@ -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<std::mutex> 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();