2014-12-09 113 views
0

我有一個控制器,可以由具有管理員權限的用戶或護士訪問。然後,如果我想採取單獨行動,我可以做得更嚴格。現在我擁有的是這樣的東西授權中的多個用戶角色

[AuthorizeUser(UserRole = "Admin", OrganizationType = "Institution")] 

它工作正常。但我會像

[AuthorizeUser(UserRole = "Admin,Nurse", OrganizationType = "Institution")] 

AuthorizeUser是定製授權

public class AuthorizeUser : AuthorizeAttribute 
{ 
    public string UserRole { get; set; } 
    public string OrganizationType { get; set; } 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var isAuthorized = base.AuthorizeCore(httpContext); 
     if (!isAuthorized) 
     { 
      return false; 
     } 

     return CheckOrganizationType 
      .checkRole(this.UserRole, this.OrganizationType, Auth.CurrentUser); 
    } 
} 

    public static bool checkRole(String role, String organizationType, User user) 
    { 
     RolesType rt = null; 
     OrganizationType ot = null; 
     foreach (UserRoles ur in user.GetUserRoles()) 
     { 
      rt = RolesType.Get(ur.organizationTypeId,ur.roleTypeId); 
      ot = OrganizationType.Get(ur.organizationTypeId, "1"); 
     } 

     if (rt != null && rt.Name == role && ot != null && ot.Name == organizationType) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

,然後檢查當前用戶是否有任何定義的角色。如何才能做到這一點?任何想法?

+0

這是什麼'AuthorizeUser'屬性?這不是本地的 – Luizgrs 2014-12-09 13:29:47

+0

@Luizgrs剛剛更新了我的問題。 – mohsinali1317 2014-12-09 13:31:16

+0

我們不能幫你,如果你不包括CheckOrganizationType.checkRole(實施 – InferOn 2014-12-09 13:33:01

回答

1

你剛纔更改此聲明:

if (rt != null && rt.Name == role && ot != null && ot.Name == organizationType) 

與此:

if (rt != null && role.Contains(rt.Name) && ot != null && ot.Name == organizationType)