메뉴 항목 표시를위한 사용자 액세스 역할을 지정하는 현재 정의 된 컨트롤러/동작 권한 부여 특성을 재사용하려고합니다 (사용자가 액세스 할 수있는 메뉴 항목 만 볼 수 있도록).).MVC 컨트롤러 역할 재사용 메뉴 항목 표시
현재 메뉴 항목과 컨트롤러/동작 권한 부여 역할 역할이 구분되어 있기 때문에 변경 사항이 두 곳에서 업데이트해야하므로 나중에 오류가 발생할 수 있습니다.
나는 사용자 권한 부여 속성으로 찾고있다, 이것은 내가 지금까지 무엇을 가지고 :
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
var routeData = httpContext.Request.RequestContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentController = routeData.GetRequiredString("controller");
var currentUserRoles = GetCurrentUserRoles();
// from the list of menu items, find the menu item with the current action
// and controller and check the current user's roles entitle access
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(
new
{
controller = "Error",
action = "Unauthorised"
})
);
}
}
메뉴 아이템은 기본적으로 메뉴 항목이 프론트 엔드 디스플레이를위한 텍스트 레이블에 사용할 수있는 사용자 역할로 구성된다 그리고 컨트롤러와 액션의 URL. MenuItem은 현재 사용자가 MenuItem을 표시하는 데 필요한 역할에 있는지 여부에 따라 부분 뷰에서 렌더링됩니다.
위에서 볼 수 있듯이 모든 컨트롤러 작업과 관련된 사용자 역할을 모두 두 영역에서 재사용 할 필요가있을 수 있습니다.이를 달성하는보다 우아한 방법이 있습니까?