我知道這是一箇舊帖子,但想提供我的解決方案。這可能不是最好的方式,但它適用於我,可能對某人有用。
我在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