2009-01-21 84 views
3

我正在编写一个GUI应用程序,它将具有用户登录功能。每个用户将属于(至少一个,可能多于一个)组,每个组将具有指示某些权限是否被允许或不允许的属性。权限列表将涵盖诸如编辑过去的事物,打印,删除数据等等。许多不同的动作可以由相同的权利来处理(例如,可以从菜单和工具栏中启动打印)。如何在GUI应用程序中实现安全性?

我的问题是:什么是实施这个安全系统的最佳方式?每个动作都应该有布尔isSecurable属性和权限列表吗?应该如何进行检查,通过中央结构进行检查,还是每项检查都应该检查自己所需的权利?

我在这里的目标是正确的。我知道我可以快速地将一个工作系统拼凑在一起,但我希望有一些不会导致问题的事情。对于详细的解释,我很抱歉,但我甚至不知道该怎么称呼我正在寻找的东西。

编辑:这不是真的GUI特定的,我想,但我已经研究了很多关于这方面的信息,我发现的大部分内容都是针对Web应用程序或一般的“安全编程”技巧。

回答

3

“BCS”是正确的,因为安全检查不应该绑在GUI,应与基础动作/操作/你调用方法。在MVC框架中,模型或其他地方可以在模型调用的动作中使用该框架。

如果你的行为的调度肯定会通过一些常见的机构(例如所有共享一个特定的基类),然后把安全检查有以涵盖所有的基础的好办法。

一个额外的想法:您描述为组可能或可能不是安全术语中的“角色”。

+0

你将如何处理诸如“查看用户权利”之类的权利?如果用户不被允许查看用户权限,则该对话框甚至不应该打开。对话框是否应尝试打开安全设置,并在失败时显示错误消息? – 2009-01-21 23:06:52

1

我会像一个MVC系统,并没有把任何安全的东西放在GUI代码。使它成为模型的一部分,以便它不会如何触发用户触发操作,同一个安全代码路径会运行。

我从来没有做过太多的GUI甚至不太安全的东西,但一个方法是有跟踪是谁登录并仅转发被允许在目前情况下请求的安全代理对象。

如果你想有一个有趣的项目,该代理对象将是生成的代码一个很好的候选人:

考虑到与安全注解的接口,生成实现该接口和块/转发呼叫基于一类传递给它的安全上下文。

1

如果您要编写.NET应用程序,则可以考虑使用成员资格提供程序基础结构。您甚至可以使用此方法为Web客户端和桌面客户端实现身份验证,如this MSDN magazine article中所述。

+0

安全规则#1:不要自己扮演角色。 – BCS 2009-01-21 23:01:58

0

你的问题听起来像状态模式。用户所属的每个组是不同的状态对象。将每个适用的状态对象附加给用户。然后,当用户试图做一些需要权利的事情时,询问状态对象。

因为用户可以在多个组,你也可能会受益于修饰模式。

0

我知道这是一个旧帖子,但想提供我的解决方案。这可能不是最好的方式,但它适用于我,可能对某人有用。

我在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; 

正如我在咆哮中所说的那样,这可能有用也可能没有用,但它对我来说非常有用。

相关问题