⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aclintf.~pas

📁 ACL权限控制软件
💻 ~PAS
字号:
unit ACLIntf;

interface

uses ModuleIntf,ServiceIntf,ExtCtrls, SysUtils, Forms,XMLDOM,Classes,XMLDoc,XMLIntf;

type
   TACLIntf = class(TInterfacedObject, IModuleIntf)
   private
     FForm : TForm;
   public
     constructor Create;
     destructor Destroy; override;

     function getWorkArea : TPanel;
     function getToolArea : TPanel;
     procedure setMessageArea(IArea : IMessageAreaIntf);
     function getModuleName: String;
     procedure setParamToModule(paramName, value : String);
     function getInstanceState: TModuleInstanceState;
     function ExecuteCommand(Command : String) : String;
//=================================================//
     function GetRoleId(FileName: DOMString;UserId: String): TStrings;   //取得用户的相应角色
     function GetReadPermission(FileName: DOMString;RoleId: String): TStrings; //取得角色的可读权限
     function GetWritePermission(FileName: DOMString;RoleId: String): TStrings; //取得角色的可写权限
//=================================================//

     procedure ShowMessage(Msg : String);
   end;

type
   TACLSeriveceIntf = class(TInterfacedObject, IServiceIntf)
   public
     constructor Create;
     destructor Destroy; override;

      { 动作处理 }
     function Start : boolean;   // 启动服务
     function Stop : boolean;    // 停止服务
     function Execute(cmd : String; params : array of Const) : variant;  // 执行服务功能

     function GetRoleId(FileName: DOMString;UserId,Password: String): TStrings;   //取得用户的相应角色
     function GetReadPermission(FileName: DOMString;RoleId: String): TStrings; //取得角色的可读权限
     function GetWritePermission(FileName: DOMString;RoleId: String): TStrings; //取得角色的可写权限


      { 消息处理 }
     procedure NotifyStateChanged;   // 通知关注本接口的对象状态改变的消息
     procedure AddListener(listener : TServiceListener);        // 增加本接口的监听器
     procedure RemoveListener(listener : TServiceListener);     // 删除本接口的指定的监听器

      { 辅助函数 }
     function getState : TServiceState;  // 取得当前服务状态
     function getName : String;  // 取得当前服务名称
     function getDescription : String;  // 取得当前服务的描述信息
   end;

implementation
uses UnitACL,UnitDataM,UnitActionList,UnitXTR;

{ TACLIntf }

constructor TACLIntf.Create;
begin
    FrmXTR := TFrmXTR.Create(nil);
    DataM := TDataM.Create(nil);
    FForm := TFrmACL.Create(nil);
    TFrmACL(FrmACL) := TFrmACL(FForm);
    TFrmACL(FForm).MsgAreaIntf := nil;
    DataActionList := TDataActionList.Create(nil);
end;

destructor TACLIntf.Destroy;
begin
   if Assigned(FForm) then
   begin
   FreeAndNil(FForm);
   FreeAndNil(DataM);
   FreeAndNil(DataActionList);
   FreeAndNil(FrmXTR);
   end;
   inherited;
end;

function TACLIntf.ExecuteCommand(Command: String): String;
begin
    { TODO: do nothing }
end;

function TACLIntf.getInstanceState: TModuleInstanceState;
begin
    result := msDontFree;
end;

function TACLIntf.getModuleName: String;
begin
    result := 'ACL管理器';
end;

function TACLIntf.getToolArea: TPanel;
begin
   result := TFrmACL(FForm).Panel4;
end;

function TACLIntf.getWorkArea: TPanel;
begin
   result := TFrmACL(FForm).PanelMain;
end;

procedure TACLIntf.setMessageArea(IArea: IMessageAreaIntf);
begin
    TFrmACL(FForm).MsgAreaIntf := IArea;
end;

procedure TACLIntf.setParamToModule(paramName, value: String);
begin

end;

procedure TACLIntf.ShowMessage(Msg: String);
begin
    if Assigned(TFrmACL(FForm).MsgAreaIntf) then
          TFrmACL(FForm).MsgAreaIntf.ShowMessage(Msg);
end;

//===================================================//
function TACLIntf.GetRoleId(FileName: DOMString;UserId: String): TStrings;
var
   a: TStrings;
   userlist: IXMLDocument;
   subnode,grandnode: IXMLNode;
   i: integer;
begin
   a := TStrings.Create;
   userlist := TXMLDocument.Create(nil);
   userlist.LoadFromFile(Filename);
   userlist.Active := true;
   i := 0;
   while i<userlist.ChildNodes.Count do
     begin
        subnode := userlist.ChildNodes.Nodes[i];
        if subnode.ChildNodes.FindNode('ID').Text = UserId then
          break;
        Inc(i);
     end;
   i := 3;
   while i<subnode.ChildNodes.Count do
     begin
        grandnode := subnode.ChildNodes.Nodes[i];
        if grandnode.Attributes['access'] = 'True' then
           a.Add(grandnode.Attributes['id']);
        Inc(i);
     end;
   userlist.active := false;
   result := a;
   FreeAndNil(a);
end;



function TACLIntf.GetReadPermission(FileName: DOMString;RoleId: String): TStrings;
var
   a: TStrings;
   rolelist: IXMLDocument;
   subnode,grandnode: IXMLNode;
   i: integer;
begin
   a := TStrings.Create;

   rolelist := TXMLDocument.Create(nil);
   rolelist.LoadFromFile(Filename);
   rolelist.active := true;
   i := 0;
   while i<rolelist.ChildNodes.Count do
     begin
        subnode := rolelist.ChildNodes.Nodes[i];
        if subnode.Attributes['id'] = RoleId then
          break;
        Inc(i);
     end;
   i := 0;
   while i<subnode.ChildNodes.Count do
     begin
        grandnode := subnode.ChildNodes.Nodes[i];
        if grandnode.ChildNodes.FindNode('read').Text = 'True' then
        a.Add(grandnode.ChildNodes.FindNode('code').Text);
        Inc(i);
     end;
   rolelist.active := false;
   result := a;
   FreeAndNil(a);
end;

function TACLIntf.GetWritePermission(FileName: DOMString;RoleId: String): TStrings;
var
   a: TStrings;
   rolelist: IXMLDocument;
   subnode,grandnode: IXMLNode;
   i: integer;
begin
   a := TStrings.Create;

   rolelist := TXMLDocument.Create(nil);
   rolelist.LoadFromFile(Filename);
   rolelist.active := true;
   i := 0;
   while i<rolelist.ChildNodes.Count do
     begin
        subnode := rolelist.ChildNodes.Nodes[i];
        if subnode.Attributes['id'] = RoleId then
          break;
        Inc(i);
     end;
   i := 0;
   while i<subnode.ChildNodes.Count do
     begin
        grandnode := subnode.ChildNodes.Nodes[i];
        if grandnode.ChildNodes.FindNode('write').Text = 'True' then
        a.Add(grandnode.ChildNodes.FindNode('code').Text);
        Inc(i);
     end;
   rolelist.active := false;
   result := a;
   FreeAndNil(a);
end;
//===================================================//

function TACLSeriveceIntf.Start : boolean;
begin
end;

function TACLSeriveceIntf.Stop : boolean;
begin
end;

function TACLSeriveceIntf.Execute(cmd : String; params : array of Const) : variant;
var
   role_result: TStrings;
   readpermission_result,writepermission_result: TStrings; //单一角色对应的权限列表
   i,j,k: integer;
   read_result,write_result: TStrings;      //所有角色对应的权限列表--权限之间不重复
   cf: boolean; //重复标记
   Filename: String;
begin
   if cmd = 'Login' then
      begin
 //====================================================//
      {role_result 有三种情况: 1:用户名不存在 2:密码错误 3: 返回正常的角色ID}

      {判断该用户是否正在使用}
      {f exist_operation(Params[0],Params[2]) then
         begin
            showmessage('该用户正在使用!');
            exit;
         end;}

        role_result := GetRoleId(Filename,Params[0].VChar, Params[1].VChar);
        if role_result.Strings[0] = '用户名不存在!'  then
           begin
              result := '登陆失败,该用户名不存在!'
           end
        else if role_result.Strings[0] = '密码错误!' then
           result :=  '登陆失败,密码错误!'
        else
          begin
             {取出该用户对应的所有角色对应的读权限}
             for i:=0 to role_result.Count do
               begin
                  readpermission_result := GetReadPermission(Filename,role_result.Strings[i]);
                  if readpermission_result <> nil then // 动态readpermission中的权限和read_result中的权限不能重复
                    begin
                       for j:=0 to readpermission_result.Count do
                         begin
                            cf := false;
                            for k:=0 to read_result.Count do
                              begin
                                 if read_result.Strings[k] = readpermission_result.Strings[j] then
                                    cf := true;
                              end;
                            {取出该用户对应的所有角色对应的读权限中的不重复读权限赋值给read_result}
                            if not cf then
                               read_result.Add(readpermission_result.Strings[j]);
                         end;

                    end;
               end;

            {  for i:= 0 to read_result.Count do
               begin
                  cf := false;
                  for j:= 0 to a.Count do
                    begin
                       if a[j] = read_result.Strings[i] then
                       cf := true;
                    end;
                  if not cf  then //如果新的权限和已有的权限不重复,就添加到a里
                       a.Add(read_result.Strings[i]);
               end;  }

             {取出该用户对应的所有角色对应的写权限}
             for i:=0 to role_result.Count do
               begin
                  writepermission_result := GetWritePermission(Filename,role_result.Strings[i]);
                  if writepermission_result <> nil then // 动态writepermission中的权限和write_result中的权限不能重复
                    begin
                       for j:=0 to writepermission_result.Count do
                         begin
                            cf := false;
                            for k:=0 to write_result.Count do
                              begin
                                 if write_result.Strings[k] = writepermission_result.Strings[j] then
                                    cf := true;
                              end;
                            {取出该用户对应的所有角色对应的读权限中的不重复读权限赋值给write_result}
                            if not cf then
                               write_result.Add(writepermission_result.Strings[j]);
                         end;

                    end;
               end;
           end;
 //====================================================//
      end
        {    result :=  登录  返回 -> 用户对应所有角色的所有权限

         else
            result := 登陆失败 返回 1:用户名正确,密码错误 2:用户名不存在 }

   else if cmd = 'Logout' then
      begin
       {  Logout(params[0]);}   //注销函数Logout(UserId)
         result := '注销成功!'
      end;
end;


function TACLSeriveceIntf.GetRoleId(FileName: DOMString;UserId,Password: String): TStrings;
var
   a: TStrings;
   userlist: IXMLDocument;
   subnode,grandnode: IXMLNode;
   i: integer;
   exist_id: boolean;
begin
   exist_id := false;
   a := TStrings.Create;
   userlist := TXMLDocument.Create(nil);
   userlist.LoadFromFile(Filename);
   userlist.Active := true;
   i := 0;
   while i<userlist.ChildNodes.Count do
     begin
        subnode := userlist.ChildNodes.Nodes[i];
        if subnode.ChildNodes.FindNode('ID').Text = UserId then
          begin
             exist_id := true;
             if subnode.ChildNodes.FindNode('PASSWORD').Text = Password then
               break
             else
               begin
                  a.Add('密码错误!');
                  result := a;
                  exit;
               end;
          end;
        Inc(i);
     end;
   if not exist_id then
     begin
        a.Add('用户名不存在!');
        result := a;
        exit;
     end;
   i := 3;
   while i<subnode.ChildNodes.Count do
     begin
        grandnode := subnode.ChildNodes.Nodes[i];
        if grandnode.Attributes['access'] = 'True' then
           a.Add(grandnode.Attributes['id']);
        Inc(i);
     end;
   userlist.active := false;
   result := a;
   FreeAndNil(a);
end;

function TACLSeriveceIntf.GetReadPermission(FileName: DOMString;RoleId: String): TStrings;
var
   a: TStrings;
   rolelist: IXMLDocument;
   subnode,grandnode: IXMLNode;
   i: integer;
begin
   a := TStrings.Create;
   a := nil;
   rolelist := TXMLDocument.Create(nil);
   rolelist.LoadFromFile(Filename);
   rolelist.active := true;
   i := 0;
   while i<rolelist.ChildNodes.Count do
     begin
        subnode := rolelist.ChildNodes.Nodes[i];
        if subnode.Attributes['id'] = RoleId then
          break;
        Inc(i);
     end;
   i := 0;
   while i<subnode.ChildNodes.Count do
     begin
        grandnode := subnode.ChildNodes.Nodes[i];
        if grandnode.ChildNodes.FindNode('read').Text = 'True' then
        a.Add(grandnode.ChildNodes.FindNode('code').Text);
        Inc(i);
     end;
   rolelist.active := false;
   if a = nil then
     begin
        result := nil;
        exit;
     end;
   result := a;
   FreeAndNil(a);
end;

function TACLSeriveceIntf.GetWritePermission(FileName: DOMString;RoleId: String): TStrings;
var
   a: TStrings;
   rolelist: IXMLDocument;
   subnode,grandnode: IXMLNode;
   i: integer;
begin
   a := TStrings.Create;
   a := nil;
   rolelist := TXMLDocument.Create(nil);
   rolelist.LoadFromFile(Filename);
   rolelist.active := true;
   i := 0;
   while i<rolelist.ChildNodes.Count do
     begin
        subnode := rolelist.ChildNodes.Nodes[i];
        if subnode.Attributes['id'] = RoleId then
          break;
        Inc(i);
     end;
   i := 0;
   while i<subnode.ChildNodes.Count do
     begin
        grandnode := subnode.ChildNodes.Nodes[i];
        if grandnode.ChildNodes.FindNode('write').Text = 'True' then
        a.Add(grandnode.ChildNodes.FindNode('code').Text);
        Inc(i);
     end;
   if a = nil then
     begin
        result := nil;
        exit;
     end;
   rolelist.active := false;
   result := a;
   FreeAndNil(a);
end;

procedure TACLSeriveceIntf.NotifyStateChanged;
begin
end;

procedure TACLSeriveceIntf.AddListener(listener : TServiceListener);
begin
end;

procedure TACLSeriveceIntf.RemoveListener(listener : TServiceListener);
begin
end;

function TACLSeriveceIntf.getState : TServiceState;
begin
   result := ssRunning;
end;

function TACLSeriveceIntf.getName : String;
begin
    result := 'ACL管理器';
end;

function TACLSeriveceIntf.getDescription : String;
begin
    result := 'ACL管理器';
end;

constructor TACLSeriveceIntf.Create;
begin
end;

destructor TACLSeriveceIntf.Destroy;
begin
   inherited;
end;

initialization


finalization


end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -