2012-09-22 4 views
1

나는이 내 DB에서 다음과 같은 구조 :사용자 역할 협회

DomainEntities: 
+EntityID 
+Name 
+ParentID 
+... 

Users: 
+UserID 
+Username 
+... 

Roles: 
+RoleID 
+Name 

UserRolesAssociation: 
+RoleID 
+UserID 
+EntityID 

그래서 내가 사용하고자하는 MVC의 다른 의해 만들어진 나의 컨트롤러에서 조치를 필터링 할 권한 부여 특성에 내장 회원.

나는 user1이 entity1 또는 그 아래의 엔티티에 대해 삭제 작업을 수행 할 경우 사용자가 그에 대한 작업을 적절하게 필터링하고 필터링 할 수있는 권리가 있는지 확인할 수 있습니다.

해당 주제를 해결하는 가장 좋은 방법은 무엇입니까? 내가 필요한 답변을 제공하거나 기존 기능을 사용할 수있는 자체 권한 엔진을 만들어야합니까?

답변

2

해당 주제를 해결하는 가장 좋은 방법은 무엇입니까?

맞춤 [Authorize]은이 로직을 구현하기에 좋은 장소 인 것처럼 보입니다.

public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var authorized = base.AuthorizeCore(httpContext); 
     if (!authorized) 
     { 
      // the use ris not authenticated or not authorized - no need to continue 
      return false; 
     } 

     string username = httpContext.User.Identity.Name; 
     // read the entity id that this user is attempting to manipulate 
     string entityId = (string)httpContext.Request.RequestContext.RouteData.Values["id"] ?? httpContext.Request["id"]; 

     return IsAllowed(username, entityId); 
    } 

    private bool IsAllowed(string username, string entityId) 
    { 
     // You know what to do here - hit the database and check whether 
     // the current user is the owner of the entity 
     throw new NotImplementedException(); 
    } 
} 

다음 :

[HttpDelete] 
[MyAuthorize] 
public ActionResult Delete(int id) 
{ 
    ... 
} 
+0

내가 내 자신의 사용자 지정 특성을 만든 적이없는, 즉 연습 할 수있는 좋은 장소가 될 것입니다 .. 즉 실제로 도움이 ... 주셔서 감사합니다 :) .. 하나 개 더 질문을 당신에게 이 사용자가 조작하려고하는 엔티티 ID를 읽었습니다. 그러나 현재 컨텍스트에서 어떻게 사용자가 조작하려고하는 엔티티를 찾을 수 있습니까? .. 쿼리 문자열에서 읽어야합니까? – Mortalus

+0

Yeap, 그 바로 아래에있는 행은 RouteData 또는 요청에서 엔티티 ID를 읽는 것입니다. 귀하의 컨트롤러 조치는 어떻게 든이 정보를 전달해야합니다. –

+0

속성을 재정의하면서 패밀리 데이터에 액세스 할 수 있다고 생각하지 않았습니다 ... 완벽하게 작동했습니다. :) – Mortalus