2013-12-17 51 views
0

我有一種情況,允許特定角色的用戶查看oData供稿的全部內容,而沒有該角色的用戶被允許查看'他們'的記錄。WCF數據服務oData從QueryInterceptor拋出異常

該模型包含一個UserId字段。注意:這是使用內置AD角色提供程序的內部Intranet站點。未處於特定角色的用戶可以查看其用戶標識與UserId字段值相匹配的任何記錄。在沒有特定角色的人嘗試訪問那些值不匹配的記錄的情況下,我想拋出一個Not Authorized異常。

最終,QueryInterceptor返回的表達式看起來似乎稍後會轉入SQL並用於過濾結果。這意味着一個throw語句在表達式中並不真正有意義。

是否有另一種方法可以做我想做的事?

我的查詢攔截器的一個實例:

[QueryInterceptor("Notifications")] 
public Expression<Func<Notification, bool>> OnQueryNotifications() 
{ 
    var user = HttpContext.Current.User; 
    var userId = user.Identity.UserId(); //Extension method 

    // returns boolean if the user is in any of the roles passed in the array 
    var allowed = SiteSecurity.Allowed(user, new string[] { "Administrator", "Technician" }); 

    if (!allowed) 
     // Here is where I need to test if the user is requesting any specific 
     // records, and if they are allowed to view those records. 
     return q => q.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase); 
    else 
     return q => true; 
} 

回答

0

您可以只通過唯一返回他們已經訪問(如你上面做的)中的那些排除記錄。

或者,如果用戶嘗試獲取他們無權訪問的數據,則可以返回403禁止狀態碼。