脚本实现权限控制
功能权限和数据权限通过脚本来实现,异常简单。
function TfrmFunction.rights(bar: TUniToolBar; funcid: string; dataset: TClientDataSet = nil; grid: TUniDBGrid = nil): Boolean;
// 权限脚本用于设置没有哪项权限
var
i: integer;
script: string;
fs: TfsScript;
begin
// 获取角色权限脚本
if UniServerModule.cdsRolerRights.Locate('rolerid;funcid', VarArrayOf([gUser.rolerid, funcid]), []) then
script := UniServerModule.cdsRolerRights.FieldByName('rightsscript').Text;
if script = '' then
begin
Result := True;
exit;
end;
fs := TfsScript.Create(nil);
try
// 功能权限
fs.Clear;
if bar <> nil then
begin
fs.AddRTTI;
fs.AddClass(TUniToolButton, 'TUniToolButton');
for i := 0 to bar.ControlCount - 1 do
begin
if bar.Controls[i] is TUniToolButton then
fs.AddObject(bar.Controls[i].Name, TUniToolButton(bar.Controls[i]));
end;
end;
// 数据权限
if dataset <> nil then
begin
fs.AddRTTI;
fs.AddClass(TClientDataSet, 'TClientDataSet');
fs.AddObject(dataset.Name, dataset);
for i := 0 to dataset.FieldCount - 1 do
fs.AddObject(dataset.Fields[i].Name, TField(dataset.Fields[i]));
end;
// 处理begin..end.
script := 'begin ' + script;
script := script + ' end.';
fs.Lines.Add(script);
Result := fs.Run;
// 表格列是否可视
if Result and (grid <> nil) then
for i := 0 to grid.Columns.Count - 1 do
grid.Columns[i].Visible := DataSet.FieldByName(grid.Columns[i].FieldName).Visible;
finally
fs.Free;
end;
end;