Cleaned up and added the UserSessionSetup Middleware to the build.

This commit is contained in:
Relintai 2022-07-21 23:56:29 +02:00
parent a791c8f4b8
commit 7f58857334
5 changed files with 32 additions and 15 deletions

View File

@ -16,6 +16,9 @@ sources = [
"singleton/user_db.cpp",
]
if env['module_web_enabled']:
sources.append("web/middleware/user_session_setup_middleware.cpp")
if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes':
# Shared lib compilation
module_env.Append(CCFLAGS=['-fPIC'])

View File

@ -15,6 +15,8 @@ def get_doc_classes():
"UserManagerStatic",
"UserManagerFile",
"UserDB",
"UserSessionSetupWebServerMiddleware",
]

View File

@ -32,6 +32,12 @@ SOFTWARE.
#include "singleton/user_db.h"
#include "../modules_enabled.gen.h"
#ifdef MODULE_WEB_ENABLED
#include "web/middleware/user_session_setup_middleware.h"
#endif
UserDB *_user_db = nullptr;
void register_users_types() {
@ -45,6 +51,10 @@ void register_users_types() {
_user_db = memnew(UserDB);
Engine::get_singleton()->add_singleton(Engine::Singleton("UserDB", UserDB::get_singleton()));
#ifdef MODULE_WEB_ENABLED
ClassDB::register_class<UserSessionSetupWebServerMiddleware>();
#endif
}
void unregister_users_types() {

View File

@ -1,18 +1,20 @@
#include "user_session_setup_middleware.h"
#include "../../singleton/user_db.h"
#include "../../users/user.h"
#include "modules/web/http/http_session.h"
#include "modules/web/http/web_server_request.h"
// returnring true means handled, false means continue
bool UserSessionSetupMiddleware::on_before_handle_request_main(Ref<WebServerRequest> request) {
// returning true means handled, false means continue
bool UserSessionSetupWebServerMiddleware::_on_before_handle_request_main(Ref<WebServerRequest> request) {
if (request->get_session().is_valid()) {
int user_id = request->get_session()->get_int("user_id");
int user_id = request->get_session()->get_int("user_id", -1);
if (user_id != 0) {
Ref<User> u = UserController::get_singleton()->db_get_user(user_id);
if (user_id != -1) {
Ref<User> u = UserDB::get_singleton()->get_user(user_id);
if (u.is_valid()) {
request->reference_data["user"] = u;
request->set_meta("user", u);
} else {
// log
request->get_session()->remove("user_id");
@ -23,7 +25,7 @@ bool UserSessionSetupMiddleware::on_before_handle_request_main(Ref<WebServerRequ
return false;
}
UserSessionSetupMiddleware::UserSessionSetupMiddleware() {
UserSessionSetupWebServerMiddleware::UserSessionSetupWebServerMiddleware() {
}
UserSessionSetupMiddleware::~UserSessionSetupMiddleware() {
UserSessionSetupWebServerMiddleware::~UserSessionSetupWebServerMiddleware() {
}

View File

@ -1,5 +1,5 @@
#ifndef USER_SESSION_SETUP_MIDDLEWARE_H
#define USER_SESSION_SETUP_MIDDLEWARE_H
#ifndef USER_SESSION_SETUP_WEB_SERVER_MIDDLEWARE_H
#define USER_SESSION_SETUP_WEB_SERVER_MIDDLEWARE_H
#include "core/reference.h"
#include "core/ustring.h"
@ -7,15 +7,15 @@
#include "modules/web/http/web_server_middleware.h"
// just session setup
class UserSessionSetupMiddleware : public WebServerMiddleware {
GDCLASS(UserSessionSetupMiddleware, WebServerMiddleware);
class UserSessionSetupWebServerMiddleware : public WebServerMiddleware {
GDCLASS(UserSessionSetupWebServerMiddleware, WebServerMiddleware);
public:
//returning true means handled, false means continue
bool on_before_handle_request_main(Ref<WebServerRequest> request);
bool _on_before_handle_request_main(Ref<WebServerRequest> request);
UserSessionSetupMiddleware();
~UserSessionSetupMiddleware();
UserSessionSetupWebServerMiddleware();
~UserSessionSetupWebServerMiddleware();
};
#endif