Cleaned up WebPermission.

This commit is contained in:
Relintai 2022-06-26 17:23:40 +02:00
parent a92f8dd5c0
commit 19cd0fe4e2
3 changed files with 59 additions and 17 deletions

View File

@ -1,11 +1,22 @@
#include "web_permission.h"
#include "request.h"
#include "http_server_enums.h"
#include "web_server_request.h"
bool WebPermission::activate(Request *request) {
bool WebPermission::activate(const Ref<WebServerRequest> &request) {
return call("_activate", request);
}
int WebPermission::get_permissions(const Ref<WebServerRequest> &request) {
return call("_get_permissions", request);
}
void WebPermission::handle_view_permission_missing(const Ref<WebServerRequest> &request) {
call("_handle_view_permission_missing", request);
}
bool WebPermission::_activate(Ref<WebServerRequest> request) {
request->active_permission.reference_ptr(this);
request->permissions = _get_permissions(request);
request->permissions = get_permissions(request);
if (!request->can_view()) {
handle_view_permission_missing(request);
@ -15,17 +26,38 @@ bool WebPermission::activate(Request *request) {
return false;
}
int WebPermission::_get_permissions(Request *request) {
int WebPermission::_get_permissions(Ref<WebServerRequest> request) {
return WEB_PERMISSION_ALL;
}
void WebPermission::handle_view_permission_missing(Request *request) {
request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND);
void WebPermission::_handle_view_permission_missing(Ref<WebServerRequest> request) {
request->send_error(HTTPServerEnums::HTTP_STATUS_CODE_404_NOT_FOUND);
}
WebPermission::WebPermission() :
Reference() {
WebPermission::WebPermission() {
}
WebPermission::~WebPermission() {
}
void WebPermission::_bind_methods() {
BIND_VMETHOD(MethodInfo("_activate", PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest")));
BIND_VMETHOD(MethodInfo("_get_permissions", PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest")));
BIND_VMETHOD(MethodInfo("_handle_view_permission_missing", PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest")));
ClassDB::bind_method(D_METHOD("activate", "request"), &WebPermission::activate);
ClassDB::bind_method(D_METHOD("get_permissions", "request"), &WebPermission::get_permissions);
ClassDB::bind_method(D_METHOD("handle_view_permission_missing", "request"), &WebPermission::handle_view_permission_missing);
ClassDB::bind_method(D_METHOD("_activate", "request"), &WebPermission::_activate);
ClassDB::bind_method(D_METHOD("_get_permissions", "request"), &WebPermission::_get_permissions);
ClassDB::bind_method(D_METHOD("_handle_view_permission_missing", "request"), &WebPermission::_handle_view_permission_missing);
BIND_ENUM_CONSTANT(WEB_PERMISSION_VIEW);
BIND_ENUM_CONSTANT(WEB_PERMISSION_CREATE);
BIND_ENUM_CONSTANT(WEB_PERMISSION_EDIT);
BIND_ENUM_CONSTANT(WEB_PERMISSION_DELETE);
BIND_ENUM_CONSTANT(WEB_PERMISSION_ALL);
BIND_ENUM_CONSTANT(WEB_PERMISSION_NONE);
}

View File

@ -1,14 +1,15 @@
#ifndef WEB_PERMISSION_H
#define WEB_PERMISSION_H
#include "core/string.h"
#include "core/ustring.h"
#include "core/variant.h"
#include "core/reference.h"
class Request;
class WebServerRequest;
class WebPermission : public Reference {
RCPP_OBJECT(WebPermission, Reference);
GDCLASS(WebPermission, Reference);
public:
enum WebPermissions {
@ -22,12 +23,21 @@ public:
};
//like in middlewate returns whether it handled the request or not
virtual bool activate(Request *request);
virtual int _get_permissions(Request *request);
virtual void handle_view_permission_missing(Request *request);
bool activate(const Ref<WebServerRequest> &request);
int get_permissions(const Ref<WebServerRequest> &request);
void handle_view_permission_missing(const Ref<WebServerRequest> &request);
virtual bool _activate(Ref<WebServerRequest> request);
virtual int _get_permissions(Ref<WebServerRequest> request);
virtual void _handle_view_permission_missing(Ref<WebServerRequest> request);
WebPermission();
~WebPermission();
protected:
static void _bind_methods();
};
VARIANT_ENUM_CAST(WebPermission::WebPermissions);
#endif

View File

@ -8,7 +8,7 @@
#include "core/object.h"
#include "core/reference.h"
#include "http_enums.h"
#include "http_server_enums.h"
class WebServer;
class WebServerCookie;