使用 IntraWeb (29) - 基本控件之 TIWAutherList、TIWAutherINI、TIWAutherEvent

探讨码农晋升架构师的障碍与路径
本文深入分析了为什么大多数程序员难以成为架构师,详细介绍了三种验证登录方式:列表验证、INI文件验证和事件验证,并通过实例展示了如何在实际项目中应用这些验证方法。同时,文章提供了一个服务器控制器的实现案例,包括如何配置和使用这些验证组件,旨在帮助程序员理解架构师的角色要求及其晋升路径。

TIWAutherList  //通过一组户名与密码验证登陆
TIWAutherINI   //通过记录户名与密码信息的 #Auth.ini 文件验证登陆
TIWAutherEvent //通过其 OnCheck 事件验证登陆

{作为站点级的验证, 验证控件应该是放在 ServerController 的窗体上, 并与其 Auther 属性关联.}


TIWAutherList 所在单元及继承链:
IWAutherList.TIWAutherList < TIWAutherBase < TComponent < TPersistent < TObject

主要成员:
property List: TStrings	//户名与密码表; 每行按 User=Pass 的格式输入
property AutherPolicy: TAutherPolicy //该属性有两个选项 apRestrictAll(默认)、apRestrictNone(选这个表示不执行验证)

property OnAuthenticate: TOnAuthenticate //验证成功后执行的事件


测试 TIWAutherList:
{在 ServerController 的窗体上放置 IWAutherList1, 然后双击该窗体(激活其 OnCreate 事件)}
procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);
begin
  Self.Auther := IWAutherList1;
  IWAutherList1.List.Add('aaa=111');
  IWAutherList1.List.Add('bbb=222');
  IWAutherList1.List.Values['ccc'] := '333';
end;

{这就好了, 如果在设计时完成上面工作会更方便}

26153244_bepZ.png


TIWAutherINI 所在单元及继承链:
IWAutherINI.TIWAutherINI < TIWAutherBase < TComponent < TPersistent < TObject

主要成员:
property AutherPolicy: TAutherPolicy //

property OnAuthenticate: TOnAuthenticate //

{它需要的 ini 文件须命名为 #Auth.ini(它会保证不被用户读取, 应该使用 UTF8 格式保存), 并且和程序文件放在同一目录(而非 wwwroot 下)}

{其格式规范:--------------------
[户名1]
Password=密码1
[户名2]
Password=密码2
...
------------------------------}

{建好文件, 放对地方, 再关联上 Auther 属性就可以了}



TIWAutherEvent 所在单元及继承链:
IWAutherEvent.TIWAutherEvent < TIWAutherBase < TComponent < TPersistent < TObject

主要成员:
property AutherPolicy: TAutherPolicy //

property OnCheck: TOnCheck		 //就是在该事件中验证; 假如要从数据库验证就应该用这种方法
property OnAuthenticate: TOnAuthenticate //

{更多时候可能需要把验证函数写在 UserSessionUnit 单元(譬如通过数据库验证时), 这时应该保证 IWServerController.AuthBeforeNewSession = False(这也是默认值)}


测试 TIWAutherEvent:
{IWAutherEvent1 的 OnCheck 事件}
function TIWServerController.IWAutherEvent1Check(const aUser, aPass: string): Boolean;
begin
  Result := aPass = aUser + '123'; //假如密码是: 用户名+123
end;

{需要保证关联到 Auther 属性}
procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);
begin
  Auther := IWAutherEvent1;
end;


还是上面的例子, 现在改成通过 TIWUserSession 的一个方法来验证:
{UserSessionUnit.pas}

unit UserSessionUnit;

interface

uses
  IWUserSessionBase, SysUtils, Classes;

type
  TIWUserSession = class(TIWUserSessionBase)
  private
  public
    function MyCheck(const AUser, APass: string): Boolean;
  end;

implementation

{$R *.dfm}

{ TIWUserSession }

function TIWUserSession.MyCheck(const AUser, APass: string): Boolean;
begin
  Result := APass.ToLower = AUser.ToLower + '123';
end;

end.


{-------------------------------------------------}


{ServerController.pas, 有注释的是自己添加的代码}

unit ServerController;

interface

uses
  SysUtils, Classes, IWServerControllerBase, IWBaseForm, HTTPApp,
  UserSessionUnit, IWApplication, IWAppForm, IW.Browser.Browser, IWAutherEvent, IWAutherINI, IWAutherBase, IWAutherList;

type
  TIWServerController = class(TIWServerControllerBase)
    IWAutherEvent1: TIWAutherEvent;
    procedure IWServerControllerBaseNewSession(ASession: TIWApplication);
    procedure IWServerControllerBaseCreate(Sender: TObject);
    function IWAutherEvent1Check(const aUser, aPass: string): Boolean;
  private
  public
  end;

function UserSession: TIWUserSession;
function IWServerController: TIWServerController;

implementation

{$R *.dfm}

uses
  IWInit, IWGlobal;

function IWServerController: TIWServerController;
begin
  Result := TIWServerController(GServerController);
end;

function UserSession: TIWUserSession;
begin
  Result := TIWUserSession(WebApplication.Data);
end;

{IWAutherEvent1 的 OnCheck 事件, 调用 UserSessionUnit.TIWUserSession 的验证函数}
function TIWServerController.IWAutherEvent1Check(const aUser, aPass: string): Boolean;
begin
  Result := UserSession.MyCheck(aUser, aPass);
end;

{OnCreate 事件, 这个关联可以在设计时做}
procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);
begin
  Auther := IWAutherEvent1;
end;

procedure TIWServerController.IWServerControllerBaseNewSession(ASession: TIWApplication);
begin
  ASession.Data := TIWUserSession.Create(nil, ASession);
end;

initialization

TIWServerController.SetServerControllerClass;

end.


转载于:https://my.oschina.net/hermer/blog/319616

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值