2016-05-23 17:26:00 +02:00
|
|
|
unit AppControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework,
|
2017-01-18 21:53:53 +01:00
|
|
|
MVCFramework.Commons,
|
2016-05-23 17:26:00 +02:00
|
|
|
MVCFramework.Logger,
|
|
|
|
Web.HTTPApp;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
TApp1MainController = class(TMVCController)
|
|
|
|
public
|
|
|
|
[MVCPath('/public')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure PublicSection(ctx: TWebContext);
|
|
|
|
[MVCPath('/')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure Index(ctx: TWebContext);
|
|
|
|
end;
|
|
|
|
|
|
|
|
[MVCPath('/admin')]
|
|
|
|
TAdminController = class(TMVCController)
|
2017-09-07 00:10:21 +02:00
|
|
|
protected
|
|
|
|
procedure OnBeforeAction(AContext: TWebContext; const AActionName: string;
|
|
|
|
var AHandled: Boolean); override;
|
2016-05-23 17:26:00 +02:00
|
|
|
public
|
|
|
|
[MVCPath('/role1')]
|
|
|
|
[MVCProduces('text/html')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure OnlyRole1(ctx: TWebContext);
|
|
|
|
[MVCPath('/role1')]
|
|
|
|
[MVCProduces('application/json')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2016-06-23 11:42:16 +02:00
|
|
|
procedure OnlyRole1EmittingJSON;
|
2016-05-23 17:26:00 +02:00
|
|
|
[MVCPath('/role2')]
|
|
|
|
[MVCProduces('text/html')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure OnlyRole2(ctx: TWebContext);
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2017-01-18 21:53:53 +01:00
|
|
|
System.SysUtils, System.JSON, System.Classes;
|
2016-05-23 17:26:00 +02:00
|
|
|
|
|
|
|
{ TApp1MainController }
|
|
|
|
|
|
|
|
procedure TApp1MainController.Index(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
Redirect('/index.html');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TApp1MainController.PublicSection(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
Render('This is a public section');
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TAdminController }
|
|
|
|
|
2017-09-07 00:10:21 +02:00
|
|
|
procedure TAdminController.OnBeforeAction(AContext: TWebContext;
|
|
|
|
const AActionName: string; var AHandled: Boolean);
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
Assert(AContext.LoggedUser.CustomData['customkey1'] = 'customvalue1', 'customkey1 not valid');
|
|
|
|
Assert(AContext.LoggedUser.CustomData['customkey2'] = 'customvalue2', 'customkey2 not valid');
|
|
|
|
AHandled := False;
|
|
|
|
end;
|
|
|
|
|
2016-05-23 17:26:00 +02:00
|
|
|
procedure TAdminController.OnlyRole1(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
ResponseStream.AppendLine('Hey! Hello ' + ctx.LoggedUser.UserName +
|
|
|
|
', now you are a logged user and this is a protected content!');
|
|
|
|
ResponseStream.AppendLine('As logged user you have the following roles: ' +
|
|
|
|
sLineBreak + string.Join(sLineBreak, Context.LoggedUser.Roles.ToArray));
|
2016-11-18 14:09:54 +01:00
|
|
|
RenderResponseStream;
|
2016-05-23 17:26:00 +02:00
|
|
|
end;
|
|
|
|
|
2016-06-23 11:42:16 +02:00
|
|
|
procedure TAdminController.OnlyRole1EmittingJSON;
|
|
|
|
var
|
|
|
|
lJObj: TJSONObject;
|
|
|
|
lJArr: TJSONArray;
|
|
|
|
lQueryParams: TStrings;
|
|
|
|
I: Integer;
|
2016-05-23 17:26:00 +02:00
|
|
|
begin
|
|
|
|
ContentType := TMVCMediaType.APPLICATION_JSON;
|
2016-06-23 11:42:16 +02:00
|
|
|
lJObj := TJSONObject.Create;
|
|
|
|
lJObj.AddPair('message', 'This is protected content accessible only by user1');
|
|
|
|
lJArr := TJSONArray.Create;
|
|
|
|
lJObj.AddPair('querystringparameters', lJArr);
|
|
|
|
|
|
|
|
lQueryParams := Context.Request.QueryStringParams;
|
|
|
|
for I := 0 to lQueryParams.Count - 1 do
|
|
|
|
begin
|
|
|
|
lJArr.AddElement(TJSONObject.Create(TJSONPair.Create(
|
|
|
|
lQueryParams.Names[I],
|
|
|
|
lQueryParams.ValueFromIndex[I])));
|
|
|
|
end;
|
|
|
|
|
|
|
|
Render(lJObj);
|
2016-05-23 17:26:00 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TAdminController.OnlyRole2(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
ResponseStream.AppendLine('Hey! Hello ' + ctx.LoggedUser.UserName +
|
|
|
|
', now you are a logged user and this is a protected content!');
|
|
|
|
ResponseStream.AppendLine('As logged user you have the following roles: ' +
|
|
|
|
sLineBreak + string.Join(sLineBreak, Context.LoggedUser.Roles.ToArray));
|
2016-11-18 14:09:54 +01:00
|
|
|
RenderResponseStream;
|
2016-05-23 17:26:00 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|