Is there a way to intercept all the requests to RAD Server endpoints ?.
I want to control programmatically the authorization to the endpoints, so I can define dynamically the permissions to those endpoints instead of being fixed and unalterable on the EMSServer.ini.
The documentation says that for programmatic control I have to check and raise my exceptions on my custom resources.
Programmatic Control
You can create a new Extending the RAD Server Engine and add code that prevent accessing to particular RAD Server endpoints. In your code, you can check if the request is from a particular RAD Server user or from a RAD Server user in a particular RAD Server group.
If the RAD Server user identified in the request is not allowed to access the endpoint, the custom resource should raise an exception (to indicate that the request is unauthorized).
It works, I can manually check on every endpoint if the user has permissions to execute that endpoint.
[ResourceSuffix('/item/{id}')]
procedure GetItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
...
...
procedure TEndpointCustomers.GetItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var id: string;
begin
TCustomPermissions.ValidateRequest(AContext);
id := ARequest.Params.Values['id'];
AResponse.Body.SetStream(TCustomer.Item(id), 'application/json', True);
end;
...
...
class procedure TCustomPermissions.ValidateRequest(AContext: TEndpointContext);
var
User, Resource, Endpoint: string;
begin
Resource := AContext.Request.Resource;
Endpoint := AContext.EndpointName;
if Assigned(AContext.User) then
User := AContext.User.UserName;
if not VeryfiyEndpointPermissions(User, Resource, Endpoint) then
raise Exception.CreateFmt(_(rsNoPermissions), [Resource, Endpoint]);
end;
The problem is that having to call that check on every endpoint is cumbersome and easy to forget. Is there a way to intercept, on a single point, all the requests to endpoints ?.
I'm looking for a single event, or similar, like the WebModule.BeforeDispatch that Datasnap has, so I can call my TCustomPermissions.ValidateRequest(AContext: TEndpointContext) there and it will protect all the RAD Server endpoints.