2012-07-27 6 views
0

은 내가 GET 또는 POST가 기사 페이지를 만들거나 편집 할 호출 될 때마다 다음과 같은 방법을 사용하려면? 내 구조는 다음과 같습니다사용자 정의 사용자 수준 및 권한 MVC

회사 -> 블로그 -> 제품 ->이

내가 특정 회사에 속하고 특정 블로그에 속하고 특정 권한이 있으므로 사용자 만 권한을 만들 댓글 요청 된 작업을 수행 할 수 있습니다.

예를 들어, 내 사용자 모델은 사용자가 연결할 수있는 회사의 ICollection을 가지며 연결할 수있는 블로그의 ICollection을 가질 수 있습니다. 또한 수퍼 유저, 기사 작성자, 기사 편집기, 운영자 등과 같은 사용 권한의 ICollection을 가질 수 있습니다.

UI를 통해 추가 및 제거 할 수 있도록 사용 권한에 대해 별도의 모델을 만듭니다.

이 함수는 요청한 회사, 블로그 및 사용 권한이 사용자가 연결되어있는 ICollection과 일치하는지 여부를 확인해야합니다.

이런 식으로 진행하는 가장 좋은 방법은 무엇입니까? 고맙습니다.

답변

2

맞춤 [Authorize] 속성으로 처리하는 것이 좋습니다. 사용자가 POST 작업에 당신을 속이려고되지 않도록

[MyAuthorize] 
public ActionResult Edit(string companyId, string blogId) 
{ 
    // if we got that far it means that the user is authorized to edit this blog post 
    // and we could allow him to see the edit view 
    EditViewModel model = ... 
    return View(model); 
} 

물론 : 이제이 특성으로 컨트롤러/액션을 장식 할 수

public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var authorized = base.AuthorizeCore(httpContext); 
     if (!authorized) 
     { 
      // The user is not even authenticated => we can't get much further 
      return false; 
     } 

     // At this stage we know that there's an authneticated user 
     // let's see who he is by fecthing his username 
     string username = httpContext.User.Identity.Name; 

     RouteData rd = httpContext.Request.RequestContext.RouteData; 

     // Now, let's read the companyId and blogId parameters that he sent 
     // into the request and ensure that he is not cheating on us 
     string companyId = rd.Values["companyId"] as string; 
     string blogId = rd.Values["blogId"] as string; 

     if (string.IsNullOrEmpty(companyId) || string.IsNullOrEmpty(blogId)) 
     { 
      // One of the required parameters were not supplied when the action was invoked 
      // => we can't get much further 
      return false; 
     } 

     return IsOwner(username, companyId, blogId); 
    } 

    private bool IsOwner(string username, string companyId, string blogId) 
    { 
     // TODO: you know what to do here: 
     // check with your data store or wherever you have stored this info 
     throw new NotImplementedException(); 
    } 
} 

: 다음의 예제를 보자 당신은 또한이 속성으로 그것을 장식 할 수 있습니다 :

[MyAuthorize] 
[HttpPost] 
public ActionResult Edit(EditViewModel model) 
{ 
    // if we got that far it means that the user is authorized to edit this blog post 
    // and we could go ahead and perform the necessary update 
    .... 
} 
+0

그것은 통찰력이었다. 그러나, 나는 IsOwner를위한 부분에 대해 정확히 어떻게 알지 못합니다. (그것은 제가 실제로 묻고있는 것입니다.) 모든 사용자의 관련 회사를 저장하는 사용자 모델에 ICollection이있는 경우 요청한 회사의 ICollection을 어떻게 확인할 수 있습니까? – user1477388

+0

아, 그건 특정 데이터베이스를 사용하고 있거나 특정 질문을 사용하고 있습니다. 이것은 ASP.NET MVC 3과는 아무런 관련이 없습니다. 초기 질문은 ASP.NET MVC 3과 관련이 없습니다. 내 답변에서는 RouteData 매개 변수를 읽고 현재 연결된 사용자 이름을 검색 할 수 있도록 사용자 지정 인증 특성을 작성하는 방법을 설명했습니다. 사용자가 블로그 또는 회사에 속해 있는지 또는 구현에 전적으로 의존 할 것인지를 어떻게 알 수 있습니까? 그래서 나는 새로운 쓰레드를 시작하고, 당신이 사용하고있는 데이터 접근 기술 (EF 추측)을 설명하고 DB 스키마를 보여줄 것을 제안 할 것이다. –

+0

... 주어진 SQL을 수행하는 방법에 대한 매우 구체적인 질문을하거나 어떤 쿼리. –