delphimvcframework/samples/jsonwebtoken/AuthenticationU.pas

83 lines
2.5 KiB
ObjectPascal
Raw Normal View History

2016-05-23 17:26:00 +02:00
unit AuthenticationU;
interface
uses
System.SysUtils, MVCFramework.Commons, System.Generics.Collections,
MVCFramework;
type
TAuthenticationSample = class(TInterfacedObject, IMVCAuthenticationHandler)
protected
procedure OnRequest(const AContext: TWebContext; const ControllerQualifiedClassName: string;
2016-05-23 17:26:00 +02:00
const ActionName: string; var AuthenticationRequired: Boolean);
procedure OnAuthentication(const AContext: TWebContext; const UserName: string; const Password: string;
2017-09-21 23:11:19 +02:00
UserRoles: TList<System.string>;
2016-05-23 17:26:00 +02:00
var IsValid: Boolean; const SessionData: TSessionData);
procedure OnAuthorization(const AContext: TWebContext; UserRoles: TList<System.string>;
2016-05-23 17:26:00 +02:00
const ControllerQualifiedClassName: string; const ActionName: string;
var IsAuthorized: Boolean);
end;
implementation
{ TMVCAuthorization }
procedure TAuthenticationSample.OnAuthentication(const AContext: TWebContext; const UserName: string; const Password: string;
UserRoles: TList<System.string>;
var IsValid: Boolean; const SessionData: TSessionData);
2016-05-23 17:26:00 +02:00
begin
IsValid := UserName.Equals(Password); // hey!, this is just a demo!!!
if IsValid then
begin
if UserName = 'user1' then
begin
UserRoles.Add('role1');
end;
if UserName = 'user2' then
begin
UserRoles.Add('role2');
end;
if UserName = 'user3' then // all the roles
begin
UserRoles.Add('role1');
UserRoles.Add('role2');
end;
// You can add custom data to the logged user
SessionData.AddOrSetValue('customkey1', 'customvalue1');
SessionData.AddOrSetValue('customkey2', 'customvalue2');
2016-05-23 17:26:00 +02:00
end
else
begin
UserRoles.Clear;
end;
end;
procedure TAuthenticationSample.OnAuthorization
(const AContext: TWebContext; UserRoles: TList<System.string>;
const ControllerQualifiedClassName: string; const ActionName: string;
var IsAuthorized: Boolean);
2016-05-23 17:26:00 +02:00
begin
IsAuthorized := False;
if ActionName = 'Logout' then
IsAuthorized := True; // you can always call logout
if ActionName = 'OnlyRole2' then
IsAuthorized := UserRoles.Contains('role2');
if ActionName = 'OnlyRole1' then
IsAuthorized := UserRoles.Contains('role1');
if ActionName = 'OnlyRole1EmittingJSON' then
IsAuthorized := UserRoles.Contains('role1');
end;
procedure TAuthenticationSample.OnRequest(const AContext: TWebContext; const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean);
2016-05-23 17:26:00 +02:00
begin
AuthenticationRequired := ControllerQualifiedClassName =
'AppControllerU.TAdminController';
end;
end.