delphimvcframework/unittests/TestServer/AuthHandlersU.pas
Daniele Teti ccc8ee90ab FIX OnBeforeAction (Thank you to Marco Mottadelli)
More unit tests
Refactoring for XE6
Updated copyright to 2017
2017-01-05 12:45:51 +01:00

114 lines
3.6 KiB
ObjectPascal

// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// *************************************************************************** }
unit AuthHandlersU;
interface
uses
MVCFramework.Commons, System.Generics.Collections;
type
TAuthHandlerBase = class abstract(TInterfacedObject, IMVCAuthenticationHandler)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean); virtual; abstract;
procedure OnAuthentication(const UserName: string; const Password: string;
UserRoles: System.Generics.Collections.TList<System.string>;
var IsValid: Boolean; const SessionData: TDictionary<string, string>); virtual;
procedure OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
const ControllerQualifiedClassName: string; const ActionName: string;
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;
end;
implementation
uses
System.SysUtils;
procedure TAuthHandlerBase.OnAuthentication(
const UserName: string; const Password: string;
UserRoles: System.Generics.Collections.TList<System.string>; var IsValid: Boolean;
const SessionData: TDictionary<string, string>);
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 TAuthHandlerBase.OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
const
ControllerQualifiedClassName, ActionName: string;
var
IsAuthorized:
Boolean);
begin
IsAuthorized := False;
if (ActionName = 'OnlyRole1') or (ActionName = 'OnlyRole1Session') then
IsAuthorized := UserRoles.Contains('role1');
if ActionName = 'OnlyRole2' then
IsAuthorized := UserRoles.Contains('role2');
end;
procedure TBasicAuthHandler.OnRequest(const ControllerQualifiedClassName, ActionName: string;
var AuthenticationRequired: Boolean);
begin
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
('TTestPrivateServerController');
end;
procedure TCustomAuthHandler.OnRequest(const ControllerQualifiedClassName,
ActionName: string; var AuthenticationRequired: Boolean);
begin
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
('TTestPrivateServerControllerCustomAuth');
end;
end.