2016-09-18 19:19:23 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2017-01-05 12:44:34 +01:00
|
|
|
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team
|
2016-09-18 19:19:23 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
2016-09-18 12:02:24 +02:00
|
|
|
unit AuthHandlersU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2018-12-09 23:03:06 +01:00
|
|
|
MVCFramework.Commons, System.Generics.Collections, MVCFramework;
|
2016-09-18 12:02:24 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TAuthHandlerBase = class abstract(TInterfacedObject, IMVCAuthenticationHandler)
|
|
|
|
public
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure OnRequest(const AContext: TWebContext; const ControllerQualifiedClassName: string;
|
2016-09-18 12:02:24 +02:00
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean); virtual; abstract;
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure OnAuthentication(const AContext: TWebContext; const UserName: string; const Password: string;
|
2016-09-18 12:02:24 +02:00
|
|
|
UserRoles: System.Generics.Collections.TList<System.string>;
|
|
|
|
var IsValid: Boolean; const SessionData: TDictionary<string, string>); virtual;
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure OnAuthorization(const AContext: TWebContext;
|
|
|
|
UserRoles: System.Generics.Collections.TList<System.string>;
|
2016-09-18 12:02:24 +02:00
|
|
|
const ControllerQualifiedClassName: string; const ActionName: string;
|
|
|
|
var IsAuthorized: Boolean); virtual;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TBasicAuthHandler = class(TAuthHandlerBase)
|
|
|
|
public
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure OnRequest(const AContext: TWebContext; const ControllerQualifiedClassName: string;
|
2016-09-18 12:02:24 +02:00
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TCustomAuthHandler = class(TAuthHandlerBase)
|
|
|
|
public
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure OnRequest(const AContext: TWebContext; const ControllerQualifiedClassName: string;
|
2016-09-18 12:02:24 +02:00
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils;
|
|
|
|
|
|
|
|
procedure TAuthHandlerBase.OnAuthentication(
|
2018-12-09 23:03:06 +01:00
|
|
|
const AContext: TWebContext;
|
2016-09-18 12:02:24 +02:00
|
|
|
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;
|
|
|
|
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure TAuthHandlerBase.OnAuthorization(
|
|
|
|
const AContext: TWebContext;
|
|
|
|
UserRoles: System.Generics.Collections.TList<System.string>;
|
|
|
|
const ControllerQualifiedClassName, ActionName: string;
|
|
|
|
var IsAuthorized: Boolean);
|
2016-09-18 12:02:24 +02:00
|
|
|
begin
|
|
|
|
IsAuthorized := False;
|
|
|
|
if (ActionName = 'OnlyRole1') or (ActionName = 'OnlyRole1Session') then
|
|
|
|
IsAuthorized := UserRoles.Contains('role1');
|
|
|
|
if ActionName = 'OnlyRole2' then
|
|
|
|
IsAuthorized := UserRoles.Contains('role2');
|
|
|
|
end;
|
|
|
|
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure TBasicAuthHandler.OnRequest(const AContext: TWebContext; const ControllerQualifiedClassName: string;
|
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean);
|
2016-09-18 12:02:24 +02:00
|
|
|
begin
|
|
|
|
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
|
|
|
|
('TTestPrivateServerController');
|
|
|
|
end;
|
|
|
|
|
2018-12-09 23:03:06 +01:00
|
|
|
procedure TCustomAuthHandler.OnRequest(const AContext: TWebContext; const ControllerQualifiedClassName: string;
|
|
|
|
const ActionName: string; var AuthenticationRequired: Boolean);
|
2016-09-18 12:02:24 +02:00
|
|
|
begin
|
|
|
|
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
|
|
|
|
('TTestPrivateServerControllerCustomAuth');
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|