delphimvcframework/unittests/TestServer/WebModuleUnit.pas

107 lines
3.0 KiB
ObjectPascal
Raw Normal View History

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}
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,
MVCFramework.Commons;
type
TSampleAuth = class(TInterfacedObject, IMVCAuthenticationHandler)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean);
2015-04-01 17:01:23 +02:00
procedure OnAuthentication(const UserName: string; const Password: string;
UserRoles: System.Generics.Collections.TList<System.string>;
var IsValid: Boolean; const SessionData: TDictionary<String, String>);
procedure OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
2015-04-01 17:01:23 +02:00
const ControllerQualifiedClassName: string; const ActionName: string;
var IsAuthorized: Boolean);
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';
end);
MVCEngine.AddController(TTestServerController)
.AddController(TTestPrivateServerController)
.AddController(TTestServerControllerExceptionAfterCreate)
.AddController(TTestServerControllerExceptionBeforeDestroy)
2015-04-01 17:01:23 +02:00
.AddMiddleware(TMVCSpeedMiddleware.Create)
.AddMiddleware(TMVCBasicAuthenticationMiddleware.Create
(TSampleAuth.Create));
2015-04-01 17:01:23 +02:00
// MVCEngine.Config[TMVCConfigKey.Messaging] := 'false';
end;
{ TSampleAuth }
procedure TSampleAuth.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;
procedure TSampleAuth.OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
const ControllerQualifiedClassName, ActionName: string;
var IsAuthorized: Boolean);
2015-04-01 17:01:23 +02:00
begin
IsAuthorized := False;
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;
procedure TSampleAuth.OnRequest(const ControllerQualifiedClassName,
ActionName: string; var AuthenticationRequired: Boolean);
2015-04-01 17:01:23 +02:00
begin
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
('TTestPrivateServerController');
2013-10-30 01:09:09 +01:00
end;
end.