Reworked the UserManager now that i have a SessionManager.

This commit is contained in:
Relintai 2021-08-04 21:23:15 +02:00
parent 8fc5eca9d4
commit fbd442317e
2 changed files with 54 additions and 67 deletions

View File

@ -1,6 +1,10 @@
#include "user_manager.h"
#include "core/http/http_session.h"
#include "core/http/session_manager.h"
#include "user.h"
#include <stdlib.h>
void UserManager::add_user(User *user) {
if (!user) {
@ -10,12 +14,25 @@ void UserManager::add_user(User *user) {
std::lock_guard<std::mutex> lock(_mutex);
_users.push_back(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) {
_sessions[user->sessions[i]] = user;
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) {
if (!user) {
printf("UserManager::remove_user: ERROR, user is null!\n");
@ -24,77 +41,47 @@ void UserManager::remove_user(User *user) {
std::lock_guard<std::mutex> lock(_mutex);
for (int i = 0; i < user->sessions.size(); ++i) {
_sessions.erase(user->sessions[i]);
_users.erase(user->name);
for (int i = 0; i < _users_vec.size(); ++i) {
if (_users_vec[i] == user) {
_users_vec[i] = _users_vec[_users_vec.size() - 1];
_users_vec.pop_back();
break;
}
}
for (int i = 0; i < _users.size(); ++i) {
if (_users[i] == user) {
_users[i] = _users[_users.size() - 1];
_users.pop_back();
return;
}
}
}
void UserManager::logout_user(User *user) {
if (!user) {
printf("UserManager::logout_user: ERROR, user is null!\n");
SessionManager *sm = SessionManager::get_singleton();
if (!sm) {
printf("ERROR: UserManager::remove_user SessionManager singleton is null, please allocate one!\n");
return;
}
std::lock_guard<std::mutex> lock(_mutex);
for (int i = 0; i < user->sessions.size(); ++i) {
_sessions.erase(user->sessions[i]);
}
user->sessions.clear();
}
User *UserManager::get_user_for_session(const std::string &session_id) {
User *u = _sessions[session_id];
if (!u) {
printf("UserManager::get_user_for_session: ERROR, user is null! sessid:%s\n", session_id.c_str());
}
return u;
}
User *UserManager::get_user_with_id(const int user_id) {
for (int i = 0; i < _users.size(); ++i) {
User *u = _users[i];
if (!u) {
printf("UserManager::get_user_with_id: ERROR, user (%d) is null!\n", user_id);
return u;
}
if (u->id == user_id) {
return u;
}
}
printf("UserManager::get_user_with_id: ERROR, user (%d) not found!\n", user_id);
return nullptr;
}
void UserManager::logout_all() {
for (int i = 0; i < _users.size(); ++i) {
logout_user(_users[i]);
sm->delete_session(user->sessions[i]);
}
}
void UserManager::clear() {
SessionManager *sm = SessionManager::get_singleton();
std::lock_guard<std::mutex> lock(_mutex);
for (int i = 0; i < _users.size(); ++i) {
delete _users[i];
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;
}
_sessions.clear();
_users.clear();
_users_vec.clear();
}
UserManager *UserManager::get_singleton() {
@ -113,6 +100,10 @@ UserManager::UserManager() :
UserManager::~UserManager() {
clear();
if (_self == this) {
_self = nullptr;
}
}
UserManager *UserManager::_self = nullptr;

View File

@ -4,9 +4,9 @@
#include "core/object.h"
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include <mutex>
class User;
@ -14,25 +14,21 @@ class UserManager : public Object {
public:
void add_user(User *user);
void remove_user(User *user);
void logout_user(User *user);
User *get_user(const std::string &user_name);
User *get_user_for_session(const std::string &session_id);
User *get_user_with_id(const int user_id);
void logout_all();
void clear();
static UserManager* get_singleton();
static UserManager *get_singleton();
UserManager();
~UserManager();
std::map<std::string, User *> _sessions;
std::vector<User *> _users;
std::map<std::string, User *> _users;
std::vector<User *> _users_vec;
std::mutex _mutex;
protected:
static UserManager* _self;
static UserManager *_self;
};
#endif