2021-08-04 19:03:53 +02:00
|
|
|
#include "user_manager.h"
|
|
|
|
|
2021-08-04 21:23:15 +02:00
|
|
|
#include "core/http/http_session.h"
|
|
|
|
#include "core/http/session_manager.h"
|
|
|
|
|
2021-08-04 19:05:38 +02:00
|
|
|
#include "user.h"
|
2021-08-04 21:23:15 +02:00
|
|
|
#include <stdlib.h>
|
2021-08-04 19:05:38 +02:00
|
|
|
|
2021-08-04 19:03:53 +02:00
|
|
|
void UserManager::add_user(User *user) {
|
|
|
|
if (!user) {
|
|
|
|
printf("UserManager::add_user: ERROR, user is null!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
|
2021-08-04 21:23:15 +02:00
|
|
|
_users_vec.push_back(user);
|
|
|
|
_users[user->name] = user;
|
2021-08-04 19:03:53 +02:00
|
|
|
}
|
2021-08-04 21:23:15 +02:00
|
|
|
|
2021-08-04 19:03:53 +02:00
|
|
|
void UserManager::remove_user(User *user) {
|
|
|
|
if (!user) {
|
|
|
|
printf("UserManager::remove_user: ERROR, user is null!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
|
2021-08-04 21:23:15 +02:00
|
|
|
_users.erase(user->name);
|
2021-08-04 19:03:53 +02:00
|
|
|
|
2021-08-04 21:23:15 +02:00
|
|
|
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;
|
2021-08-04 19:03:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 18:25:12 +02:00
|
|
|
User *UserManager::get_user(const std::string &user_name) {
|
|
|
|
return _users[user_name];
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:23:15 +02:00
|
|
|
void UserManager::clear() {
|
|
|
|
SessionManager *sm = SessionManager::get_singleton();
|
2021-08-04 19:03:53 +02:00
|
|
|
|
2021-08-04 21:23:15 +02:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2021-08-04 19:03:53 +02:00
|
|
|
|
2021-08-04 21:23:15 +02:00
|
|
|
for (int i = 0; i < _users_vec.size(); ++i) {
|
2021-08-04 21:29:19 +02:00
|
|
|
delete _users_vec[i];
|
2021-08-04 19:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_users.clear();
|
2021-08-04 21:23:15 +02:00
|
|
|
_users_vec.clear();
|
2021-08-04 19:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UserManager *UserManager::get_singleton() {
|
|
|
|
return _self;
|
|
|
|
}
|
|
|
|
|
|
|
|
UserManager::UserManager() :
|
|
|
|
Object() {
|
|
|
|
|
|
|
|
if (_self) {
|
|
|
|
printf("UserManager::UserManager(): Error! self is not null!/n");
|
|
|
|
}
|
|
|
|
|
|
|
|
_self = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UserManager::~UserManager() {
|
|
|
|
clear();
|
2021-08-04 21:23:15 +02:00
|
|
|
|
|
|
|
if (_self == this) {
|
|
|
|
_self = nullptr;
|
|
|
|
}
|
2021-08-04 19:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UserManager *UserManager::_self = nullptr;
|