procedure TfrmMain.SetUserExecute(Sender: TObject);
begin
frmUser.showModal;
end;
在理解了Delphi中的TActionList及TAction之后,就可以看看功能权限的具体实现方法。
第一步,建立两张表,一张表存储用户信息,另一张表存储权限定义。
UserRight表结构如下:
procedure TfrmUser.N1Click(Sender: TObject);
var
i:Integer;
Action:TAction;
begin
//Add Action into user right cds.
with frmMain do begin
for i:=0 to ActionList1.actioncount-1 do begin
Action:=ActionList1.Actions[i];
cdsUserRight.AppendRecord([cdsUser.FieldByName(’userName’).AsString,TAction(Action).Caption,TAction(Action).Enabled,i]);
end;
end;
end;
第三步,可以用Grid对上一步产生的表进行编辑操作
procedure TfrmMain.FormCreate(Sender: TObject);
const
testUser=’yh’;
var
cdsRight:TClientDataSet;
i:Integer;
begin
//set right of function
cdsRight:=TClientDataSet.Create(self);
try
cdsRight.LoadFromFile(’Right.CDS’);
cdsRight.AddIndex(’id’,’UserName;ActionCaption’,[],’’,’’,0);
cdsRight.IndexName:=’id’;
for i:=0 to ActionList1.ActionCount-1 do begin
if cdsRight.FindKey([TestUser,TAction(ActionList1.Actions[i]).Caption]) then
TAction(ActionList1.Actions[i]).Enabled:=cdsRight.FieldByName(’ActionEnable’).AsBoolean;
end;
finally
cdsRight.Close;
cdsRight.Free;
end;
end;
这段代码中,假设当前的用户ID为yh,同时只设定了功能的Enable属性
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/177598.html原文链接:https://javaforall.net
