Implemented UserWebPage.

This commit is contained in:
Relintai 2022-07-23 19:29:51 +02:00
parent 41d32e3b35
commit 40df361d7c
3 changed files with 215 additions and 2 deletions

View File

@ -1,7 +1,179 @@
#include "user_web_page.h"
#include "core/object.h"
#include "../../users/user.h"
#include "modules/web/http/web_server_request.h"
UserWebPage::RenderType UserWebPage::get_logged_out_render_type() {
return _logged_out_render_type;
}
void UserWebPage::set_logged_out_render_type(const UserWebPage::RenderType val) {
_logged_out_render_type = val;
}
String UserWebPage::get_logged_out_redirect_url() {
return _logged_out_redirect_url;
}
void UserWebPage::set_logged_out_redirect_url(const String &val) {
_logged_out_redirect_url = val;
}
int UserWebPage::get_logged_out_error_code() {
return _logged_out_error_code;
}
void UserWebPage::set_logged_out_error_code(const int val) {
_logged_out_error_code = val;
}
UserWebPage::RenderType UserWebPage::get_logged_in_render_type() {
return _logged_in_render_type;
}
void UserWebPage::set_logged_in_render_type(const UserWebPage::RenderType val) {
_logged_in_render_type = val;
}
String UserWebPage::get_logged_in_redirect_url() {
return _logged_in_redirect_url;
}
void UserWebPage::set_logged_in_redirect_url(const String &val) {
_logged_in_redirect_url = val;
}
int UserWebPage::get_logged_in_error_code() {
return _logged_in_error_code;
}
void UserWebPage::set_logged_in_error_code(const int val) {
_logged_in_error_code = val;
}
bool UserWebPage::get_render_menu() {
return _render_menu;
}
void UserWebPage::set_render_menu(const bool val) {
_render_menu = val;
}
void UserWebPage::_handle_request(Ref<WebServerRequest> request) {
if (!request->has_meta("user")) {
handle_not_logged_in(request);
return;
}
Ref<User> u = request->get_meta("user");
if (!u.is_valid()) {
handle_not_logged_in(request);
return;
}
handle_logged_in(request);
}
void UserWebPage::_render_index(Ref<WebServerRequest> request) {
request->compile_and_send_body();
}
void UserWebPage::handle_not_logged_in(Ref<WebServerRequest> request) {
call("_handle_not_logged_in", request);
}
void UserWebPage::_handle_not_logged_in(Ref<WebServerRequest> request) {
switch (_logged_out_render_type) {
case RENDER_TYPE_RENDER:
if (_render_menu) {
render_menu(request);
}
render_index(request);
break;
case RENDER_TYPE_REDIRECT:
request->send_redirect(_logged_out_redirect_url);
break;
case RENDER_TYPE_ERROR:
request->send_error(_logged_out_error_code);
break;
default:
request->send_error(503);
break;
}
}
void UserWebPage::handle_logged_in(Ref<WebServerRequest> request) {
call("_handle_logged_in", request);
}
void UserWebPage::_handle_logged_in(Ref<WebServerRequest> request) {
switch (_logged_in_render_type) {
case RENDER_TYPE_RENDER:
if (_render_menu) {
render_menu(request);
}
render_index(request);
break;
case RENDER_TYPE_REDIRECT:
request->send_redirect(_logged_in_redirect_url);
break;
case RENDER_TYPE_ERROR:
request->send_error(_logged_in_error_code);
break;
default:
request->send_error(503);
break;
}
}
UserWebPage::UserWebPage() {
_logged_out_render_type = RENDER_TYPE_ERROR;
_logged_out_error_code = 404;
_logged_in_render_type = RENDER_TYPE_RENDER;
_logged_in_error_code = 404;
_render_menu = true;
}
UserWebPage::~UserWebPage() {
}
void UserWebPage::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_logged_out_render_type"), &UserWebPage::get_logged_out_render_type);
ClassDB::bind_method(D_METHOD("set_logged_out_render_type", "val"), &UserWebPage::set_logged_out_render_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "logged_out_render_type", PROPERTY_HINT_ENUM, "Render,Redirect,Error"), "set_logged_out_render_type", "get_logged_out_render_type");
ClassDB::bind_method(D_METHOD("get_logged_out_redirect_url"), &UserWebPage::get_logged_out_redirect_url);
ClassDB::bind_method(D_METHOD("set_logged_out_redirect_url", "val"), &UserWebPage::set_logged_out_redirect_url);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "logged_out_redirect_url"), "set_logged_out_redirect_url", "get_logged_out_redirect_url");
ClassDB::bind_method(D_METHOD("get_logged_out_error_code"), &UserWebPage::get_logged_out_error_code);
ClassDB::bind_method(D_METHOD("set_logged_out_error_code", "val"), &UserWebPage::set_logged_out_error_code);
ADD_PROPERTY(PropertyInfo(Variant::INT, "logged_out_error_code"), "set_logged_out_error_code", "get_logged_out_error_code");
ClassDB::bind_method(D_METHOD("get_logged_in_render_type"), &UserWebPage::get_logged_in_render_type);
ClassDB::bind_method(D_METHOD("set_logged_in_render_type", "val"), &UserWebPage::set_logged_in_render_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "logged_in_render_type", PROPERTY_HINT_ENUM, "Render,Redirect,Error"), "set_logged_in_render_type", "get_logged_in_render_type");
ClassDB::bind_method(D_METHOD("get_logged_in_redirect_url"), &UserWebPage::get_logged_in_redirect_url);
ClassDB::bind_method(D_METHOD("set_logged_in_redirect_url", "val"), &UserWebPage::set_logged_in_redirect_url);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "logged_in_redirect_url"), "set_logged_in_redirect_url", "get_logged_in_redirect_url");
ClassDB::bind_method(D_METHOD("get_logged_in_error_code"), &UserWebPage::get_logged_in_error_code);
ClassDB::bind_method(D_METHOD("set_logged_in_error_code", "val"), &UserWebPage::set_logged_in_error_code);
ADD_PROPERTY(PropertyInfo(Variant::INT, "logged_in_error_code"), "set_logged_in_error_code", "get_logged_in_error_code");
ClassDB::bind_method(D_METHOD("get_render_menu"), &UserWebPage::get_render_menu);
ClassDB::bind_method(D_METHOD("set_render_menu", "val"), &UserWebPage::set_render_menu);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "render_menu"), "set_render_menu", "get_render_menu");
BIND_VMETHOD(MethodInfo("_handle_not_logged_in", PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest")));
ClassDB::bind_method(D_METHOD("handle_not_logged_in", "request"), &UserWebPage::handle_not_logged_in);
ClassDB::bind_method(D_METHOD("_handle_not_logged_in", "request"), &UserWebPage::_handle_not_logged_in);
BIND_VMETHOD(MethodInfo("_handle_logged_in", PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest")));
ClassDB::bind_method(D_METHOD("handle_logged_in", "request"), &UserWebPage::handle_logged_in);
ClassDB::bind_method(D_METHOD("_handle_logged_in", "request"), &UserWebPage::_handle_logged_in);
BIND_ENUM_CONSTANT(RENDER_TYPE_RENDER);
BIND_ENUM_CONSTANT(RENDER_TYPE_REDIRECT);
BIND_ENUM_CONSTANT(RENDER_TYPE_ERROR);
}

View File

@ -19,12 +19,53 @@ public:
RENDER_TYPE_ERROR,
};
//Render type + sopport properties for it, when the user is logged in, and when not logged in
RenderType get_logged_out_render_type();
void set_logged_out_render_type(const RenderType val);
String get_logged_out_redirect_url();
void set_logged_out_redirect_url(const String &val);
int get_logged_out_error_code();
void set_logged_out_error_code(const int val);
RenderType get_logged_in_render_type();
void set_logged_in_render_type(const RenderType val);
String get_logged_in_redirect_url();
void set_logged_in_redirect_url(const String &val);
int get_logged_in_error_code();
void set_logged_in_error_code(const int val);
bool get_render_menu();
void set_render_menu(const bool val);
void _handle_request(Ref<WebServerRequest> request);
void _render_index(Ref<WebServerRequest> request);
void handle_not_logged_in(Ref<WebServerRequest> request);
virtual void _handle_not_logged_in(Ref<WebServerRequest> request);
void handle_logged_in(Ref<WebServerRequest> request);
virtual void _handle_logged_in(Ref<WebServerRequest> request);
UserWebPage();
~UserWebPage();
protected:
static void _bind_methods();
bool _render_menu;
RenderType _logged_out_render_type;
String _logged_out_redirect_url;
int _logged_out_error_code;
RenderType _logged_in_render_type;
String _logged_in_redirect_url;
int _logged_in_error_code;
};
VARIANT_ENUM_CAST(UserWebPage::RenderType);
#endif

View File

@ -208,7 +208,7 @@ void WebNode::_handle_error_send_request(Ref<WebServerRequest> request, const in
// this is a fallback error handler.
// Webroot implements a proper one
request->compiled_body = "<html><body>Internal server error!</body></html>";
request->set_status_code(HTTPServerEnums ::HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE);
request->set_status_code(HTTPServerEnums::HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE);
request->send();
}