我知道这是一个旧帖子,但想提供我的解决方案。这可能不是最好的方式,但它适用于我,可能对某人有用。
我在Delphi编写应用程序的7
管理员数据库应用程序(Windows GUI和MySQL),可以创建具有不同级别的访问权限的用户。该应用程序管理工厂生产(原材料),产品,销售&服务,以及在3个国家设有工厂和办事处的公司的会计。
用户可以根据其安全权限访问选项卡,表格和功能。
基本上,某些功能根据权限被禁用或隐藏。在某些情况下,我会向用户显示一条消息,其他时候,如果他们的权限不足,他们的行为将被忽略。每个用户可以拥有单一权限或权限组合。
例如,某人可能有;
- “只读销售&服务”和“读/写生产”,
- “读/写销售&服务”,
- 或者,任何或所有的任意组合可用权利。
它的工作原理是这样的;
unit bitwise; // Found this unit on stackoverflow - All credit to original author
interface
Const // Added constants that suit me
Adm = 01; // Administrator
Rws = 02; // Read Write Sales
Ros = 04; // Read Only Sale
Rwp = 08; // Read Write Production
Rop = 16; // Read Only Production
roa = 32; // Read Only All
acc = 64; // Accounting
function IsBitSet(const val: byte; const TheBit: Byte): Boolean;
function BitOn(const val: byte; const TheBit: Byte): byte;
function BitOff(const val: byte; const TheBit: Byte): byte;
function BitToggle(const val: byte; const TheBit: Byte): byte;
implementation
function IsBitSet(const val: byte; const TheBit: Byte): Boolean;
begin
Result := (val and (TheBit)) <> 0;
end;
function BitOn(const val: byte; const TheBit: Byte): byte;
begin
Result := val or (TheBit);
end;
function BitOff(const val: byte; const TheBit: Byte): byte;
begin
Result := val and not (TheBit);
end;
function BitToggle(const val: byte; const TheBit: Byte): byte;
begin
Result := val xor (TheBit);
end;
end. // End of Unit
如果我要当用户尝试的东西,他们没有获得,我使用下面的函数显示一条消息。
Function TForm1.HasRights(Need: Byte; Msg: String;): Boolean;
Begin
If Not IsBitSet(rights, Need) Then
Begin
showdialog('Security', 'You have insufficient Security Rights!', 'You must have ' +
Msg + ' access to perform the action you have attempted.', '', '', false, False, True);
Result := False;
End
Else
Result := True;
End;
我称这样上面的函数;
If HasRights(Rop Or Rwp Or Adm, '"Read Only Production" or "Read/Write Production"') Then
Begin
// Do something they are allowed to do
End // else ignore them
如果我不需要一个消息框,显示我打电话IsBitSet这样;
If IsBitSet(rights, Adm) Then
Begin
// Do stuff
end;
只是为了清晰,这里是ShowDialog的功能。它显示我创建的自定义窗体,非常适合我的应用程序。
Function TForm1.showdialog(Const DialogTitle: WideString; Const FirstCaption: WideString;
Const SecondCaption: widestring; Const ConfirmBCaption: widestring; Const CancelBCaption:
widestring; LeftButton, RightButton, MiddleButton: Boolean): boolean;
Var
whattheysaid: boolean;
craigsdialog: Tcraigsdialog;
Begin
// Modal1Button and Modal2Button can have modified captions whereas Modal3Button
// is always "Ok". If the only button a user needs is "Ok" then make it visible
// and receive a modalresult of 3 when clicked. This 3rd button is for appearance
// only and just makes it a bit neater.
Whattheysaid := False;
Craigsdialog := Tcraigsdialog.Create(nil);
With Craigsdialog Do
Begin
// Set the Dialog details as required
Caption := DialogTitle;
Label1.Caption := FirstCaption;
Label2.Caption := SecondCaption;
Modal1Button.Visible := leftbutton;
Modal2Button.Visible := rightbutton;
Modal3Button.Visible := Middlebutton;
modal1button.Caption := ConfirmBCaption;
modal2button.Caption := CancelBCaption;
Case ShowModal Of
1: whattheysaid := True
2: whattheysaid := False
3: whattheysaid := True
End;
End;
FreeAndNil(craigsdialog);
Result := whattheysaid;
End;
正如我在咆哮中所说的那样,这可能有用也可能没有用,但它对我来说非常有用。
你将如何处理诸如“查看用户权利”之类的权利?如果用户不被允许查看用户权限,则该对话框甚至不应该打开。对话框是否应尝试打开安全设置,并在失败时显示错误消息? – 2009-01-21 23:06:52