2013-08-22 172 views
1

我一直在做C#的一個月,所以請原諒這個問題的'本地性',但我已經研究了幾個小時,並且我碰到了一堵磚牆。WPF基於權限的授權

我見過的例子左,右爲利用IIdentityIPrincipal WPF應用程序基於角色的授權

我無法找到很多信息,但是,在一個更基於許可的授權方法,在此應用程序想像沒有組,而只是一個權限和用戶的列表,你可以爲任何人的任何許可。

我希望能夠:

  1. 能夠控制UI /元素基於與作爲這些國家的用戶權限:啓用,只讀,看不見的,暈倒(如這裏看到https://uiauth.codeplex.com/
  2. 能夠指定在其所需的權限類或方法的水平(類似於http://lostechies.com/derickbailey/2011/05/24/dont-do-role-based-authorization-checks-do-activity-based-checks/

代替:

[PrincipalPermission(SecurityAction.Demand, Role = "Administrators")] 

我想是這樣的:

[PrincipalPermission(SecurityAction.Demand, Permission = "Can add users")] 

現在我看到如何做到這一點是利用ICommand並且使用了大量的字符串對比,看看用戶是否將授權邏輯在CanExecute方法的唯一途徑所需的權限執行請求樣的作用:

// Employee class 
public bool HasRight(SecurityRight right) 
{ 
    return employee.Permissions.Contains(right); 
} 

// Implementation, check if employee has right to continue 
if (employee.HasRight(db.SecurityRights.Single(sr => sr.Description == "Can edit users"))) 
{ 
    // Allowed to perform action 
} 
else 
{ 
    // User does not have right to continue 
    throw SecurityException; 
} 

我想我明白枚舉/標誌/位,但不足以COM plete實施...

如果我有:

EmployeeModel
EmployeeViewModel

我不知道在那裏一切順利,以及如何將其結合在一起....這裏是我有這麼遠:

[Flags] 
    public enum Permissions 
    { 
     None = 0, 
     Create = 1 << 0, 
     Read = 1 << 1, 
     Update = 1 << 2, 
     Delete = 1 << 3, 

     User = 1 << 4, 
     Group = 1 << 5 
    } 

    public static void testFlag() 
    { 
     Permissions p; 
     var x = p.HasFlag(Permissions.Update) && p.HasFlag(Permissions.User); 
     var desiredPermissions = Permissions.User | Permissions.Read | Permissions.Create; 
     if (x & p == desiredPermissions) 
     { 
      //the user can be created and read by this operator 
     } 
    } 

回答

0

之所以你可能會看到大量的基於角色的解決方案,是因爲這是從長遠來看,一個更容易維護的解決方案。

與其給予個人權利並且不得不管理整個組織中的每個人,您可以爲應用程序的功能集設置具有不同級別授權的少數模板(角色)。

然後,您可以只看到用戶是否在該角色中,或者不知道他是否可以執行某個操作。

我知道它並不像你想要的那樣直接幫助你的問題,但我強烈推薦使用類似的解決方案。

+0

很好,每當你希望用戶X擁有來自X組的自定義權限時創建一個新組,基於角色的就是老闆想要的。 –

+0

這意味着只有一個新組只有一個成員。最終你會用完不同的組合:) – Killnine