delphimvcframework/samples/authenticationauthorization/AppControllerU.pas

88 lines
2.1 KiB
ObjectPascal
Raw Normal View History

2015-04-01 17:01:23 +02:00
unit AppControllerU;
interface
uses
MVCFramework,
MVCFramework.Commons,
2015-04-01 17:01:23 +02:00
MVCFramework.Logger,
Web.HTTPApp;
type
[MVCPath('/')]
TApp1MainController = class(TMVCController)
public
[MVCPath('/public')]
[MVCHTTPMethod([httpGET])]
2020-10-24 14:21:02 +02:00
procedure PublicSection;
2015-04-01 17:01:23 +02:00
[MVCPath('/')]
[MVCHTTPMethod([httpGET])]
2020-10-24 14:21:02 +02:00
procedure Index;
2015-04-01 17:01:23 +02:00
end;
[MVCPath('/admin')]
TAdminController = class(TMVCController)
public
[MVCPath('/role1')]
[MVCProduces('text/html')]
2015-04-01 17:01:23 +02:00
[MVCHTTPMethod([httpGET])]
2020-10-24 14:21:02 +02:00
procedure OnlyRole1;
[MVCPath('/role1')]
[MVCProduces('application/json')]
[MVCHTTPMethod([httpGET])]
2020-10-24 14:21:02 +02:00
procedure OnlyRole1EmittingJSON;
2015-04-01 17:01:23 +02:00
[MVCPath('/role2')]
[MVCProduces('text/html')]
2015-04-01 17:01:23 +02:00
[MVCHTTPMethod([httpGET])]
2020-10-24 14:21:02 +02:00
procedure OnlyRole2;
2015-04-01 17:01:23 +02:00
end;
implementation
uses
System.SysUtils;
2015-04-01 17:01:23 +02:00
{ TApp1MainController }
2020-10-24 14:21:02 +02:00
procedure TApp1MainController.Index;
2015-04-01 17:01:23 +02:00
begin
Redirect('/index.html');
end;
2020-10-24 14:21:02 +02:00
procedure TApp1MainController.PublicSection;
2015-04-01 17:01:23 +02:00
begin
Render('This is a public section');
end;
{ TAdminController }
2020-10-24 14:21:02 +02:00
procedure TAdminController.OnlyRole1;
2015-04-01 17:01:23 +02:00
begin
ContentType := TMVCMediaType.TEXT_PLAIN;
2020-10-24 14:21:02 +02:00
ResponseStream.AppendLine('Hey! Hello ' + Context.LoggedUser.UserName +
2015-04-01 17:01:23 +02:00
', 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;
2015-04-01 17:01:23 +02:00
end;
2020-10-24 14:21:02 +02:00
procedure TAdminController.OnlyRole1EmittingJSON;
begin
ContentType := TMVCMediaType.APPLICATION_JSON;
2020-10-24 14:21:02 +02:00
Render('This is protected content accessible only by user1: parameter = ' +
Context.Request.Params['par1']);
end;
2020-10-24 14:21:02 +02:00
procedure TAdminController.OnlyRole2;
2015-04-01 17:01:23 +02:00
begin
ContentType := TMVCMediaType.TEXT_PLAIN;
2020-10-24 14:21:02 +02:00
ResponseStream.AppendLine('Hey! Hello ' + Context.LoggedUser.UserName +
2015-04-01 17:01:23 +02:00
', 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;
2015-04-01 17:01:23 +02:00
end;
end.