2015-04-01 17:01:23 +02:00
|
|
|
unit AppControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework,
|
|
|
|
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)
|
|
|
|
public
|
|
|
|
[MVCPath('/role1')]
|
2016-03-29 11:04:13 +02:00
|
|
|
[MVCProduces('text/html')]
|
2015-04-01 17:01:23 +02:00
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure OnlyRole1(ctx: TWebContext);
|
2016-03-29 11:04:13 +02:00
|
|
|
[MVCPath('/role1')]
|
|
|
|
[MVCProduces('application/json')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure OnlyRole1EmittingJSON(ctx: TWebContext);
|
2015-04-01 17:01:23 +02:00
|
|
|
[MVCPath('/role2')]
|
2016-03-29 11:04:13 +02:00
|
|
|
[MVCProduces('text/html')]
|
2015-04-01 17:01:23 +02:00
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure OnlyRole2(ctx: TWebContext);
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils, MVCFramework.Commons;
|
|
|
|
|
|
|
|
{ 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 }
|
|
|
|
|
|
|
|
procedure TAdminController.OnlyRole1(ctx: TWebContext);
|
|
|
|
begin
|
2016-01-01 23:00:22 +01:00
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2015-04-01 17:01:23 +02:00
|
|
|
ResponseStream.AppendLine('Hey! Hello ' + ctx.LoggedUser.UserName +
|
|
|
|
', now you are a logged user and this is a protected content!');
|
2016-03-29 11:04:13 +02:00
|
|
|
ResponseStream.AppendLine('As logged user you have the following roles: ' +
|
|
|
|
sLineBreak + string.Join(sLineBreak, Context.LoggedUser.Roles.ToArray));
|
2015-04-01 17:01:23 +02:00
|
|
|
Render;
|
|
|
|
end;
|
|
|
|
|
2016-03-29 11:04:13 +02:00
|
|
|
procedure TAdminController.OnlyRole1EmittingJSON(ctx: TWebContext);
|
|
|
|
begin
|
|
|
|
ContentType := TMVCMediaType.APPLICATION_JSON;
|
|
|
|
Render('This is protected content accessible only by user1: paremeter = ' +
|
|
|
|
ctx.Request.Params['par1']);
|
|
|
|
end;
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
procedure TAdminController.OnlyRole2(ctx: TWebContext);
|
|
|
|
begin
|
2016-01-01 23:00:22 +01:00
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2015-04-01 17:01:23 +02:00
|
|
|
ResponseStream.AppendLine('Hey! Hello ' + ctx.LoggedUser.UserName +
|
|
|
|
', now you are a logged user and this is a protected content!');
|
2016-03-29 11:04:13 +02:00
|
|
|
ResponseStream.AppendLine('As logged user you have the following roles: ' +
|
|
|
|
sLineBreak + string.Join(sLineBreak, Context.LoggedUser.Roles.ToArray));
|
2015-04-01 17:01:23 +02:00
|
|
|
Render;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|