Added a mutex to the User.

This commit is contained in:
Relintai 2021-08-20 19:53:58 +02:00
parent 95be0cf631
commit 1b4ecafa2c
3 changed files with 37 additions and 1 deletions

View File

@ -8,6 +8,8 @@
#include <cstdio>
void DBBasedUser::save() {
_mutex.lock();
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
if (id == 0) {
@ -77,11 +79,15 @@ void DBBasedUser::save() {
b->run_query();
delete b;
_mutex.unlock();
}
void DBBasedUser::load() {
unregister_sessions();
_mutex.lock();
if (id == 0) {
return;
}
@ -128,6 +134,8 @@ void DBBasedUser::load() {
delete b;
_mutex.unlock();
register_sessions();
}

View File

@ -74,6 +74,8 @@ void User::register_sessions() {
return;
}
_mutex.lock();
for (int i = 0; i < sessions.size(); ++i) {
HTTPSession *session = new HTTPSession();
session->session_id = sessions[i];
@ -81,6 +83,8 @@ void User::register_sessions() {
sm->add_session(session);
}
_mutex.unlock();
}
void User::unregister_sessions() {
@ -95,9 +99,13 @@ void User::unregister_sessions() {
return;
}
_mutex.lock();
for (int i = 0; i < sessions.size(); ++i) {
sm->delete_session(sessions[i]);
}
_mutex.unlock();
}
void User::handle_request_default(Request *request) {
@ -159,7 +167,11 @@ void User::handle_login_request_default(Request *request) {
HTTPSession *session = request->get_or_create_session();
session->add_object("user", user);
user->_mutex.lock();
user->sessions.push_back(session->session_id);
user->_mutex.unlock();
user->save();
request->add_cookie(::Cookie("session_id", session->session_id));
@ -399,6 +411,19 @@ void User::handle_password_reset_request(Request *request) {
void User::handle_logout_request(Request *request) {
request->remove_cookie("session_id");
_mutex.lock();
for (int i = 0; i < sessions.size(); ++i) {
if (sessions[i] == request->session->session_id) {
sessions[i] = sessions[sessions.size() - 1];
sessions.pop_back();
}
}
_mutex.unlock();
save();
SessionManager::get_singleton()->delete_session(request->session->session_id);
request->session = nullptr;

View File

@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <mutex>
class Request;
class FormValidator;
@ -58,6 +59,8 @@ public:
static FormValidator *_login_validator;
static FormValidator *_registration_validator;
std::mutex _mutex;
};
#endif