diff --git a/modules/users/db_based_user.cpp b/modules/users/db_based_user.cpp index 2a26085..868e072 100644 --- a/modules/users/db_based_user.cpp +++ b/modules/users/db_based_user.cpp @@ -8,6 +8,8 @@ #include 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(); } diff --git a/modules/users/user.cpp b/modules/users/user.cpp index a3ec7cd..b244169 100644 --- a/modules/users/user.cpp +++ b/modules/users/user.cpp @@ -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)); @@ -398,7 +410,20 @@ 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; diff --git a/modules/users/user.h b/modules/users/user.h index cea439a..5dbdb0d 100644 --- a/modules/users/user.h +++ b/modules/users/user.h @@ -5,6 +5,7 @@ #include #include +#include class Request; class FormValidator; @@ -58,6 +59,8 @@ public: static FormValidator *_login_validator; static FormValidator *_registration_validator; + + std::mutex _mutex; }; #endif \ No newline at end of file