2013-10-30 01:09:09 +01:00
|
|
|
unit WebModuleUnit;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
Web.HTTPApp,
|
|
|
|
MVCFramework;
|
|
|
|
|
|
|
|
type
|
|
|
|
Twm = class(TWebModule)
|
|
|
|
procedure WebModuleCreate(Sender: TObject);
|
|
|
|
private
|
|
|
|
MVCEngine: TMVCEngine;
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
WebModuleClass: TComponentClass = Twm;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
|
2013-11-08 23:10:25 +01:00
|
|
|
uses
|
2015-04-01 17:01:23 +02:00
|
|
|
TestServerControllerU, TestServerControllerExceptionU, SpeedMiddlewareU,
|
|
|
|
MVCFramework.Middleware.Authentication, System.Generics.Collections,
|
2016-09-16 23:54:54 +02:00
|
|
|
MVCFramework.Commons, TestServerControllerPrivateU;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
type
|
2016-09-16 23:54:54 +02:00
|
|
|
TAuthHandlerBase = class abstract(TInterfacedObject, IMVCAuthenticationHandler)
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
public
|
2016-02-23 23:22:44 +01:00
|
|
|
procedure OnRequest(const ControllerQualifiedClassName: string;
|
2016-09-16 23:54:54 +02:00
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean); virtual; abstract;
|
2015-04-01 17:01:23 +02:00
|
|
|
procedure OnAuthentication(const UserName: string; const Password: string;
|
2016-02-23 23:22:44 +01:00
|
|
|
UserRoles: System.Generics.Collections.TList<System.string>;
|
2016-09-16 23:54:54 +02:00
|
|
|
var IsValid: Boolean; const SessionData: TDictionary<string, string>); virtual;
|
2016-02-23 23:22:44 +01:00
|
|
|
procedure OnAuthorization(UserRoles
|
|
|
|
: System.Generics.Collections.TList<System.string>;
|
2015-04-01 17:01:23 +02:00
|
|
|
const ControllerQualifiedClassName: string; const ActionName: string;
|
2016-09-16 23:54:54 +02:00
|
|
|
var IsAuthorized: Boolean); virtual;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TBasicAuthHandler = class(TAuthHandlerBase)
|
|
|
|
public
|
|
|
|
procedure OnRequest(const ControllerQualifiedClassName: string;
|
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TCustomAuthHandler = class(TAuthHandlerBase)
|
|
|
|
public
|
|
|
|
procedure OnRequest(const ControllerQualifiedClassName: string;
|
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean); override;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
procedure Twm.WebModuleCreate(Sender: TObject);
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
MVCEngine := TMVCEngine.Create(self,
|
|
|
|
procedure(Config: TMVCConfig)
|
|
|
|
begin
|
|
|
|
Config[TMVCConfigKey.Messaging] := 'true';
|
2016-09-06 10:30:52 +02:00
|
|
|
end, nil);
|
2016-02-23 23:22:44 +01:00
|
|
|
MVCEngine.AddController(TTestServerController)
|
|
|
|
.AddController(TTestPrivateServerController)
|
2013-12-05 16:19:01 +01:00
|
|
|
.AddController(TTestServerControllerExceptionAfterCreate)
|
2014-03-31 11:25:16 +02:00
|
|
|
.AddController(TTestServerControllerExceptionBeforeDestroy)
|
2016-09-16 23:54:54 +02:00
|
|
|
.AddController(TTestPrivateServerControllerCustomAuth)
|
2015-04-01 17:01:23 +02:00
|
|
|
.AddMiddleware(TMVCSpeedMiddleware.Create)
|
2016-09-16 23:54:54 +02:00
|
|
|
.AddMiddleware(TMVCBasicAuthenticationMiddleware.Create(TBasicAuthHandler.Create))
|
|
|
|
.AddMiddleware(TMVCCustomAuthenticationMiddleware.Create(TCustomAuthHandler.Create, '/system/users/logged','/login.html'));
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
// MVCEngine.Config[TMVCConfigKey.Messaging] := 'false';
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TSampleAuth }
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
procedure TAuthHandlerBase.OnAuthentication(
|
|
|
|
const UserName: string; const Password: string;
|
|
|
|
UserRoles: System.Generics.Collections.TList<System.string>; var IsValid: Boolean;
|
|
|
|
const SessionData: TDictionary<string, string>);
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
|
|
|
UserRoles.Clear;
|
|
|
|
IsValid := UserName = Password;
|
|
|
|
if not IsValid then
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
if UserName = 'user1' then
|
|
|
|
begin
|
|
|
|
IsValid := True;
|
|
|
|
UserRoles.Add('role1');
|
|
|
|
end;
|
|
|
|
if UserName = 'user2' then
|
|
|
|
begin
|
|
|
|
IsValid := True;
|
|
|
|
UserRoles.Add('role2');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
procedure TAuthHandlerBase.OnAuthorization(UserRoles
|
2016-02-23 23:22:44 +01:00
|
|
|
: System.Generics.Collections.TList<System.string>;
|
2016-09-16 23:54:54 +02:00
|
|
|
const
|
|
|
|
ControllerQualifiedClassName, ActionName: string;
|
|
|
|
var
|
|
|
|
IsAuthorized:
|
|
|
|
Boolean);
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
|
|
|
IsAuthorized := False;
|
2016-04-03 22:35:27 +02:00
|
|
|
if (ActionName = 'OnlyRole1') or (ActionName = 'OnlyRole1Session') then
|
2015-04-01 17:01:23 +02:00
|
|
|
IsAuthorized := UserRoles.Contains('role1');
|
|
|
|
|
|
|
|
if ActionName = 'OnlyRole2' then
|
|
|
|
IsAuthorized := UserRoles.Contains('role2');
|
|
|
|
end;
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
{ TBasicAuthHandler }
|
|
|
|
|
|
|
|
procedure TBasicAuthHandler.OnRequest(const ControllerQualifiedClassName, ActionName: string;
|
|
|
|
var AuthenticationRequired: Boolean);
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2016-02-23 23:22:44 +01:00
|
|
|
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
|
|
|
|
('TTestPrivateServerController');
|
2013-10-30 01:09:09 +01:00
|
|
|
end;
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
{ TCustomAuthHandler }
|
|
|
|
|
|
|
|
procedure TCustomAuthHandler.OnRequest(const ControllerQualifiedClassName,
|
|
|
|
ActionName: string; var AuthenticationRequired: Boolean);
|
|
|
|
begin
|
|
|
|
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
|
|
|
|
('TTestPrivateServerControllerCustomAuth');
|
|
|
|
end;
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
end.
|