DELPHI的权限控制

DELPHI的权限控制

本人DELPHI新手,没事练习写个简单的系统,第一个权限控制就想了好几天不知道怎么去控制,看了很多权限控制的代码,要不是一个一个的MenuItem的去判断要不是遍历MainMenu,还需要搞递归,感觉太不方便了,下面的本人的一个方法,感觉适合新手使用。
第一把所有MenuItem的Name存储到数据库的表中,t_Mod是所有菜单的表ModNo是菜单Name,新增用户后,把t_Mod的数据插入到权限表(t_Power)中,然后再勾选权限
在这里插入图片描述
第二前端控制,前端我们通过TComponent来用名字定位MenuItem,然后再控制MenuItem的显示和不显示这样就可以简单控制权限了

procedure TMan.FormShow(Sender: TObject);
var
  i: Integer;
  j: Integer;
  Menu: TComponent;
begin
  FDQuery1.Close;
  FDQuery1.SQL.Clear;
  FDQuery1.SQL.Add('select * from t_Power where UserNo=:UserNo');
  FDQuery1.ParamByName('UserNo').Value := UserNo;
  FDQuery1.Open;

  while not FDQuery1.Eof do
  begin
    if (UserNo <> 'admin') and (FDQuery1.FieldByName('PSelect').AsBoolean) then
    begin
      Menu := FindComponent(FDQuery1.FieldByName('ModNo').AsString);
      if Menu is TMenuItem then
        TMenuItem(Menu).Visible := true;
    end
    else if (UserNo <> 'admin') and
      (FDQuery1.FieldByName('PSelect').AsBoolean = False) then
    begin
      Menu := FindComponent(FDQuery1.FieldByName('ModNo').AsString);
      if Menu is TMenuItem then
        TMenuItem(Menu).Visible := False;
    end;
    FDQuery1.Next;
  end;

菜鸟新手的方法就这样,如果老鸟有更好的方法请说下,谢谢
补充一下 按钮的权限控制,按钮权限是放到一个单独的Unit里面的,所以是通过传入Form作为对象进行控件的遍历,然后判断控制权限

procedure USysMethodFromPower(Sender: TObject);
var
  i: Integer;
  j: Integer;
begin
  Man.FDQuery1.Close;
  Man.FDQuery1.SQL.Clear;
  Man.FDQuery1.SQL.Add
    ('select * from t_Power where UserNo=:UserNo and ModName=:ModName');
  Man.FDQuery1.ParamByName('UserNo').Value := Man.UserNo;
  Man.FDQuery1.ParamByName('ModName').Value := TForm(Sender).Caption;
  Man.FDQuery1.Open;

  for i := 0 to TForm(Sender).ComponentCount - 1 do
  begin
    if TForm(Sender).Components[i] is TToolButton then
    begin
      // 控制新增权限
      if (TToolButton(TForm(Sender).Components[i]).Caption = '   新增  ') and
        (Man.FDQuery1.FieldByName('PInsert').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := True
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '   新增  ') and
        (not Man.FDQuery1.FieldByName('PInsert').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := False
        // 控制编辑权限
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '编辑') and
        (Man.FDQuery1.FieldByName('PUpdate').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := True
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '编辑') and
        (not Man.FDQuery1.FieldByName('PUpdate').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := False
        // 控制删除权限
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '删除') and
        (Man.FDQuery1.FieldByName('PDelete').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := True
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '删除') and
        (Man.FDQuery1.FieldByName('PDelete').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := False
        // 控制打印权限
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '打印') and
        (Man.FDQuery1.FieldByName('PPrint').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := True
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '打印') and
        (Man.FDQuery1.FieldByName('PPrint').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := False
        // 控制导入权限
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '导入') and
        (Man.FDQuery1.FieldByName('PImport').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := True
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '导入') and
        (Man.FDQuery1.FieldByName('PImport').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := False
        // 控制导出权限
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '导出') and
        (Man.FDQuery1.FieldByName('PExport').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := True
      else if (TToolButton(TForm(Sender).Components[i]).Caption = '导出') and
        (Man.FDQuery1.FieldByName('PExport').AsBoolean) then
        TToolButton(TForm(Sender).Components[i]).Visible := False
      else if TToolButton(TForm(Sender).Components[i]).Caption = '取消' then
        TToolButton(TForm(Sender).Components[i]).Enabled := False
      else if TToolButton(TForm(Sender).Components[i]).Caption = '保存' then
        TToolButton(TForm(Sender).Components[i]).Enabled := False;
    end;
    if TForm(Sender).Components[i] is TDBGridEh then
    begin
      for j := 0 to TDBGridEh(TForm(Sender).Components[i]).Columns.Count - 1 do
      begin
        // 控制金额权限
        if (TDBGridEh(TForm(Sender).Components[i]).Columns[j]
          .FieldName = 'Price') and
          (Man.FDQuery1.FieldByName('PPrice').AsBoolean) then
          TDBGridEh(TForm(Sender).Components[i]).Columns[j].Visible := True
        else if (TDBGridEh(TForm(Sender).Components[i]).Columns[j]
          .FieldName = 'Price') and
          (not Man.FDQuery1.FieldByName('PPrice').AsBoolean) then
          TDBGridEh(TForm(Sender).Components[i]).Columns[j].Visible := False
        else if (TDBGridEh(TForm(Sender).Components[i]).Columns[j]
          .FieldName = 'Money') and
          (Man.FDQuery1.FieldByName('PPrice').AsBoolean) then
          TDBGridEh(TForm(Sender).Components[i]).Columns[j].Visible := True
        else if (TDBGridEh(TForm(Sender).Components[i]).Columns[j]
          .FieldName = 'Money') and
          (not Man.FDQuery1.FieldByName('PPrice').AsBoolean) then
          TDBGridEh(TForm(Sender).Components[i]).Columns[j].Visible := False
      end;

    end;

  end;
end;
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值