mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
Added a mutex to the User.
This commit is contained in:
parent
95be0cf631
commit
1b4ecafa2c
@ -8,6 +8,8 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
void DBBasedUser::save() {
|
void DBBasedUser::save() {
|
||||||
|
_mutex.lock();
|
||||||
|
|
||||||
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
|
||||||
|
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
@ -77,11 +79,15 @@ void DBBasedUser::save() {
|
|||||||
b->run_query();
|
b->run_query();
|
||||||
|
|
||||||
delete b;
|
delete b;
|
||||||
|
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DBBasedUser::load() {
|
void DBBasedUser::load() {
|
||||||
unregister_sessions();
|
unregister_sessions();
|
||||||
|
|
||||||
|
_mutex.lock();
|
||||||
|
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -128,6 +134,8 @@ void DBBasedUser::load() {
|
|||||||
|
|
||||||
delete b;
|
delete b;
|
||||||
|
|
||||||
|
_mutex.unlock();
|
||||||
|
|
||||||
register_sessions();
|
register_sessions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,6 +74,8 @@ void User::register_sessions() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_mutex.lock();
|
||||||
|
|
||||||
for (int i = 0; i < sessions.size(); ++i) {
|
for (int i = 0; i < sessions.size(); ++i) {
|
||||||
HTTPSession *session = new HTTPSession();
|
HTTPSession *session = new HTTPSession();
|
||||||
session->session_id = sessions[i];
|
session->session_id = sessions[i];
|
||||||
@ -81,6 +83,8 @@ void User::register_sessions() {
|
|||||||
|
|
||||||
sm->add_session(session);
|
sm->add_session(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void User::unregister_sessions() {
|
void User::unregister_sessions() {
|
||||||
@ -95,9 +99,13 @@ void User::unregister_sessions() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_mutex.lock();
|
||||||
|
|
||||||
for (int i = 0; i < sessions.size(); ++i) {
|
for (int i = 0; i < sessions.size(); ++i) {
|
||||||
sm->delete_session(sessions[i]);
|
sm->delete_session(sessions[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void User::handle_request_default(Request *request) {
|
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();
|
HTTPSession *session = request->get_or_create_session();
|
||||||
|
|
||||||
session->add_object("user", user);
|
session->add_object("user", user);
|
||||||
|
|
||||||
|
user->_mutex.lock();
|
||||||
user->sessions.push_back(session->session_id);
|
user->sessions.push_back(session->session_id);
|
||||||
|
user->_mutex.unlock();
|
||||||
|
|
||||||
user->save();
|
user->save();
|
||||||
|
|
||||||
request->add_cookie(::Cookie("session_id", session->session_id));
|
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) {
|
void User::handle_logout_request(Request *request) {
|
||||||
request->remove_cookie("session_id");
|
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);
|
SessionManager::get_singleton()->delete_session(request->session->session_id);
|
||||||
request->session = nullptr;
|
request->session = nullptr;
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
class FormValidator;
|
class FormValidator;
|
||||||
@ -58,6 +59,8 @@ public:
|
|||||||
|
|
||||||
static FormValidator *_login_validator;
|
static FormValidator *_login_validator;
|
||||||
static FormValidator *_registration_validator;
|
static FormValidator *_registration_validator;
|
||||||
|
|
||||||
|
std::mutex _mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user