public SetupController(ApplicationUserManager userManager, ApplicationRoleManager roleManager, ApplicationGroupManager groupManager, ApplicationPageManager pageManager) { UserManager = userManager; RoleManager = roleManager; GroupManager = groupManager; PageManager = pageManager; }
public override void OnResultExecuting(ResultExecutingContext filterContext) { var currentUser = filterContext.HttpContext.User; var navbarItems = new List <ApplicationPage>(); if (currentUser.Identity.IsAuthenticated) { var isAdmin = currentUser.IsInRole(AppConstants.AdminRoleName); // navbarItems = filterContext.HttpContext.GetNavbarItems(); var pageManager = new ApplicationPageManager(filterContext.HttpContext.Request.GetOwinContext()); navbarItems = isAdmin ? pageManager.AdminPages.ToList() : pageManager.GetUserPages(currentUser.Identity.GetUserId()).ToList(); } else { var pageManager = new ApplicationPageManager(filterContext.HttpContext.Request.GetOwinContext()); navbarItems = pageManager.GetAnonymousPages().ToList(); } filterContext.HttpContext.Items["NavbarItems"] = navbarItems; filterContext.Controller.ViewBag.NavbarItems = navbarItems; }
protected override bool AuthorizeCore(HttpContextBase httpContext) { bool isUserAuthorized = base.AuthorizeCore(httpContext); httpContext.Items["NavbarItems"] = new List <ApplicationPage>(); // httpContext.Session["NavbarItems"] = new List<ApplicationPage>(); if (isUserAuthorized == false) { return(false); } var pageManager = new ApplicationPageManager(httpContext.GetOwinContext()); // admin have full access rights if (httpContext.User.IsInRole(AppConstants.AdminRoleName)) { httpContext.Items["NavbarItems"] = pageManager.AdminPages.ToList(); return(true); } var rd = httpContext.Request.RequestContext.RouteData; string currentAction = rd.GetRequiredString("action").ToLower(); string currentController = rd.GetRequiredString("controller").ToLower(); var requestArea = httpContext.Request.RequestContext.RouteData.DataTokens["area"]?.ToString() ?? ""; var currentArea = string.IsNullOrEmpty(requestArea) ? null : requestArea.ToLower(); var currentUserId = httpContext.User.Identity.GetUserId(); var navbarItems = pageManager.GetUserPages(currentUserId).ToList(); httpContext.Items["NavbarItems"] = navbarItems; if (navbarItems == null || navbarItems.Any() == false) { return(false); } var isAuthorized = navbarItems.Any( n => n.Controller.ToLower() == currentController && n.Action.ToLower() == currentAction && (n.Area == currentArea)); return(isAuthorized); }