private bool CheckInPath(MvcSiteMapNode node, SiteMapNode currentNode, RouteData currentRouteData) { MvcSiteMapNode currentMvcNode = currentNode as MvcSiteMapNode; if (currentMvcNode != null) { return(currentMvcNode.Key == node.Key || currentMvcNode.IsDescendantOf(node)); } // We might have some unmapped actions. Check routedata if the node is in the path. Only check top-level nodes. if (currentRouteData != null) { return(node.Controller == currentRouteData.Values["controller"].ToString() && node.ParentNode == this._sitemapProvider.RootNode); } return(false); }
public void GetMenus_SetsMenuToActiveIfItBelongsToCurrentAreaAndController() { routeValues["controller"] = "home"; routeValues["action"] = "index"; routeValues["area"] = null; MvcSiteMapNode expected = parser.GetMenuNodes(siteMap).Single(menu => menu.Controller == "Home" && menu.Action == "Index"); MvcSiteMapMenuNode actual = TreeToEnumerable(provider.GetMenus().Where(menu => menu.IsActive)).Single(); Assert.AreEqual(ResourceProvider.GetSiteMapTitle(expected.Area, expected.Controller, expected.Action), actual.Title); Assert.AreEqual(expected.IconClass, actual.IconClass); Assert.AreEqual(expected.Controller, actual.Controller); Assert.AreEqual(expected.Action, actual.Action); Assert.AreEqual(expected.Area, actual.Area); Assert.IsFalse(actual.HasActiveSubMenu); Assert.IsTrue(actual.IsActive); }
private void InitMenuViewData(ActionExecutingContext filterContext) { MenuViewData menuViewData = new MenuViewData(); User user = this._cuyahogaContext.CurrentUser; if (user != null && user.IsAuthenticated) { var nodes = this._sitemapProvider.GetMvcChildNodes(this._sitemapProvider.RootNode); var currentNode = this._sitemapProvider.CurrentNode; UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext); // mainmenu foreach (MvcSiteMapNode node in nodes) { if (this._sitemapProvider.IsAccessibleToUser(filterContext.HttpContext, node, this._cuyahogaContext.CurrentSite)) { MenuItemData menuItemData = GenerateMenuItemFromSiteMapNode(filterContext, node, currentNode, urlHelper); bool isSystem = Convert.ToBoolean(node["system"]); if (isSystem) { menuViewData.AddOptionalMenuItem(menuItemData); } else { menuViewData.AddStandardMenuItem(menuItemData); } if (node.HasChildNodes) { foreach (var childNode in node.ChildNodes) { MvcSiteMapNode mvcChildNode = childNode as MvcSiteMapNode; if (mvcChildNode != null) { if (this._sitemapProvider.IsAccessibleToUser(filterContext.HttpContext, mvcChildNode, this._cuyahogaContext.CurrentSite)) { menuItemData.AddChildMenuItem(GenerateMenuItemFromSiteMapNode(filterContext, mvcChildNode, currentNode, urlHelper)); } } } } } } } filterContext.Controller.ViewData.Add("MenuViewData", menuViewData); }
public void GetMenus_SetsHasActiveSubmenuIfAnyOfItsSubmenusAreActiveOrHasActiveSubmenus() { routeValues["area"] = "administration"; routeValues["controller"] = "roles"; routeValues["action"] = "index"; MvcSiteMapNode expected = parser.GetMenuNodes(siteMap).Single(menu => menu.Area == "Administration" && menu.Action == null); IEnumerable <MvcSiteMapMenuNode> actualMenus = TreeToEnumerable(provider.GetMenus()).Where(menu => menu.HasActiveSubMenu); MvcSiteMapMenuNode actual = actualMenus.Single(); Assert.AreEqual(ResourceProvider.GetSiteMapTitle(expected.Area, expected.Controller, expected.Action), actual.Title); Assert.AreEqual(expected.IconClass, actual.IconClass); Assert.AreEqual(expected.Controller, actual.Controller); Assert.AreEqual(expected.Action, actual.Action); Assert.AreEqual(expected.Area, actual.Area); Assert.IsTrue(actual.HasActiveSubMenu); Assert.IsFalse(actual.IsActive); }
/// <summary> /// Maps to SiteMapNodeModel. /// </summary> /// <param name="node">The node.</param> /// <param name="mvcNode">The MVC node.</param> /// <param name="sourceMetadata">The source metadata provided by the HtmlHelper.</param> /// <returns>SiteMapNodeModel instance.</returns> public static SiteMapNodeModel MapToSiteMapNodeModel(SiteMapNode node, MvcSiteMapNode mvcNode, IDictionary<string, object> sourceMetadata) { var nodeToAdd = new SiteMapNodeModel { Area = (mvcNode != null ? mvcNode.Area : ""), Controller = (mvcNode != null ? mvcNode.Controller : ""), Action = (mvcNode != null ? mvcNode.Action : ""), Title = node.Title, Description = node.Description, TargetFrame =( mvcNode == null ? "" : mvcNode.TargetFrame), Url = node.Url, IsCurrentNode = node == node.Provider.CurrentNode, IsInCurrentPath = node.IsInCurrentPath(), IsRootNode = node == node.Provider.RootNode, IsClickable = (mvcNode == null || mvcNode.Clickable), RouteValues = (mvcNode != null ? mvcNode.RouteValues : new Dictionary<string, object>()), MetaAttributes = (mvcNode != null ? mvcNode.MetaAttributes : new Dictionary<string, string>()), SourceMetadata = sourceMetadata }; return nodeToAdd; }
/// <summary> /// Maps to SiteMapNodeModel. /// </summary> /// <param name="node">The node.</param> /// <param name="mvcNode">The MVC node.</param> /// <param name="sourceMetadata">The source metadata provided by the HtmlHelper.</param> /// <returns>SiteMapNodeModel instance.</returns> public static SiteMapNodeModel MapToSiteMapNodeModel(SiteMapNode node, MvcSiteMapNode mvcNode, IDictionary <string, object> sourceMetadata) { var nodeToAdd = new SiteMapNodeModel { Area = (mvcNode != null ? mvcNode.Area : ""), Controller = (mvcNode != null ? mvcNode.Controller : ""), Action = (mvcNode != null ? mvcNode.Action : ""), Title = node.Title, Description = node.Description, TargetFrame = (mvcNode == null ? "" : mvcNode.TargetFrame), Url = node.Url, IsCurrentNode = node == node.Provider.CurrentNode, IsInCurrentPath = node.IsInCurrentPath(), IsRootNode = node == node.Provider.RootNode, IsClickable = (mvcNode == null || mvcNode.Clickable), RouteValues = (mvcNode != null ? mvcNode.RouteValues : new Dictionary <string, object>()), MetaAttributes = (mvcNode != null ? mvcNode.MetaAttributes : new Dictionary <string, string>()), SourceMetadata = sourceMetadata }; return(nodeToAdd); }
public static SiteMapNodeModel MapToSiteMapNodeModel(SiteMapNode node, MvcSiteMapNode mvcNode, IDictionary <string, object> sourceMetadata) { var model = new SiteMapNodeModel() { Area = mvcNode != null ? mvcNode.Area : "", Controller = mvcNode != null ? mvcNode.Controller : "", Action = mvcNode != null ? mvcNode.Action : "", Title = node.Title, Description = node.Description, TargetFrame = mvcNode == null ? "" : mvcNode.TargetFrame, ImageUrl = mvcNode == null ? "" : mvcNode.ImageUrl, Url = node.Url, IsCurrentNode = node == node.Provider.CurrentNode, IsInCurrentPath = SiteMapNodeExtensions.IsInCurrentPath(node), IsRootNode = node == node.Provider.RootNode, IsClickable = mvcNode == null || mvcNode.Clickable, RouteValues = mvcNode != null ? mvcNode.RouteValues : (IDictionary <string, object>) new Dictionary <string, object>(), MetaAttributes = mvcNode != null ? mvcNode.MetaAttributes : (IDictionary <string, string>) new Dictionary <string, string>(), SourceMetadata = sourceMetadata }; return(model); }
/// <summary> /// Maps an XMLElement from the XML file to a SiteMapNode. /// </summary> /// <param name="node">The element to map.</param> /// <returns>A SiteMapNode which represents the XMLElement.</returns> protected SiteMapNode GetMvcSiteMapNodeFromXMLElement(XElement node) { // Get the ID attribute, need this so we can get the key. string id = "node" + nodeCount.ToString(); // Create a new sitemapnode, setting the key and url var smNode = new MvcSiteMapNode(this, id); // Create a route data dictionary IDictionary<string, object> routeValues = new Dictionary<string, object>(); // Add each attribute to our attributes collection on the sitemapnode // and to a route data dictionary. foreach (XAttribute attribute in node.Attributes()) { string attributeName = attribute.Name.ToString(); string attributeValue = attribute.Value; smNode[attributeName] = attributeValue; if (attributeName != "title" && attributeName != "description" && attributeName != "resourceKey" && attributeName != "id" && attributeName != "paramid") { routeValues.Add(attributeName, attributeValue); } else if (attributeName == "paramid") { routeValues.Add("id", attributeValue); } } smNode["id"] = id; // Set the other properties on the sitemapnode, // these are for title and description, these come // from the nodes attrbutes are we populated all attributes // from the xml to the node. smNode.Title = smNode["title"]; smNode.Description = smNode["description"]; smNode.ResourceKey = smNode["resourceKey"]; smNode.Controller = smNode["controller"]; smNode.Area = smNode["area"]; smNode.Action = smNode["action"] ?? "Index"; // Verify route values if (!routeValues.ContainsKey("controller")) routeValues.Add("controller", "Home"); if (!routeValues.ContainsKey("action")) routeValues.Add("action", "Index"); // Build URL HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current); RouteData routeData = RouteTable.Routes.GetRouteData(httpContext); if (routeData != null) { var request = new RequestContext(httpContext, routeData); var values = new RouteValueDictionary(routeValues); VirtualPathData virtualPath; if (string.IsNullOrWhiteSpace(smNode.Area)) { virtualPath = routeData.Route.GetVirtualPath(request, values); } else { virtualPath = RouteTable.Routes.GetVirtualPathForArea(request, values); } if (virtualPath != null) { smNode.Url = "~/" + virtualPath.VirtualPath; } else { canCache = false; } } return smNode; }
/// <summary> /// Maps an XMLElement from the XML file to a SiteMapNode. /// </summary> /// <param name="node">The element to map.</param> /// <returns>A SiteMapNode which represents the XMLElement.</returns> protected SiteMapNode GetMvcSiteMapNodeFromXMLElement(XElement node) { // Get the ID attribute, need this so we can get the key. string id = GetAttributeValue(node.Attribute("id")); // Create a new sitemapnode, setting the key and url var smNode = new MvcSiteMapNode(this, id); // Create a route data dictionary IDictionary <string, object> routeValues = new Dictionary <string, object>(); // Add each attribute to our attributes collection on the sitemapnode // and to a route data dictionary. foreach (XAttribute attribute in node.Attributes()) { string attributeName = attribute.Name.ToString(); string attributeValue = attribute.Value; smNode[attributeName] = attributeValue; if (attributeName != "title" && attributeName != "description" && attributeName != "resourceKey" && attributeName != "id" && attributeName != "paramid") { routeValues.Add(attributeName, attributeValue); } else if (attributeName == "paramid") { routeValues.Add("id", attributeValue); } } // Set the other properties on the sitemapnode, // these are for title and description, these come // from the nodes attrbutes are we populated all attributes // from the xml to the node. smNode.Title = smNode["title"]; smNode.Description = smNode["description"]; smNode.ResourceKey = smNode["resourceKey"]; smNode.Controller = smNode["controller"]; smNode.Action = smNode["action"] ?? "Index"; // Verify route values if (!routeValues.ContainsKey("controller")) { routeValues.Add("controller", "Home"); } if (!routeValues.ContainsKey("action")) { routeValues.Add("action", "Index"); } // Build URL HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current); RouteData routeData = RouteTable.Routes.GetRouteData(httpContext); if (routeData != null) { VirtualPathData virtualPath = routeData.Route.GetVirtualPath(new RequestContext(httpContext, routeData), new RouteValueDictionary(routeValues)); if (virtualPath != null) { smNode.Url = "~/" + virtualPath.VirtualPath; } else { canCache = false; } } return(smNode); }
/// <summary> /// Determine if a node is accessible for a user /// </summary> /// <param name="context">Current HttpContext</param> /// <param name="node">Sitemap node</param> /// <returns>True/false if the node is accessible</returns> public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node) { // Is security trimming enabled? if (!this.SecurityTrimmingEnabled) { return(true); } // Is it a regular node? No need for more things to do! MvcSiteMapNode mvcNode = node as MvcSiteMapNode; if (mvcNode == null) { return(base.IsAccessibleToUser(context, node)); } // Find current handler MvcHandler handler = context.Handler as MvcHandler; if (handler != null) { // It's an MvcSiteMapNode, try to figure out the controller class IController controller = ControllerBuilder.Current.GetControllerFactory().CreateController(handler.RequestContext, mvcNode.Controller); // Find all AuthorizeAttributes on the controller class and action method ControllerActionInvoker i = new ControllerActionInvoker(); ArrayList controllerAttributes = new ArrayList(controller.GetType().GetCustomAttributes(typeof(AuthorizeAttribute), true)); ArrayList actionAttributes = new ArrayList(); MethodInfo[] methods = controller.GetType().GetMethods(); foreach (MethodInfo method in methods) { object[] attributes = method.GetCustomAttributes(typeof(ActionNameAttribute), true); if ( (attributes.Length == 0 && method.Name == mvcNode.Action) || (attributes.Length > 0 && ((ActionNameAttribute)attributes[0]).Name == mvcNode.Action) ) { actionAttributes.AddRange(method.GetCustomAttributes(typeof(AuthorizeAttribute), true)); } } // Attributes found? if (controllerAttributes.Count == 0 && actionAttributes.Count == 0) { return(true); } // Find out current principal IPrincipal principal = handler.RequestContext.HttpContext.User; // Find out configuration string roles = ""; string users = ""; if (controllerAttributes.Count > 0) { AuthorizeAttribute attribute = controllerAttributes[0] as AuthorizeAttribute; roles += attribute.Roles; users += attribute.Users; } if (actionAttributes.Count > 0) { AuthorizeAttribute attribute = actionAttributes[0] as AuthorizeAttribute; roles += attribute.Roles; users += attribute.Users; } // Still need security trimming? if (string.IsNullOrEmpty(roles) && string.IsNullOrEmpty(users) && principal.Identity.IsAuthenticated) { return(true); } // Determine if the current user is allowed to access the current node string[] roleArray = roles.Split(','); string[] usersArray = users.Split(','); foreach (string role in roleArray) { if (role != "*" && !principal.IsInRole(role)) { return(false); } } foreach (string user in usersArray) { if (user != "*" && (principal.Identity.Name == "" || principal.Identity.Name != user)) { return(false); } } return(true); } return(false); }
/// <summary> /// Resolves the URL. /// </summary> /// <param name="mvcSiteMapNode">The MVC site map node.</param> /// <param name="area">The area.</param> /// <param name="controller">The controller.</param> /// <param name="action">The action.</param> /// <param name="routeValues">The route values.</param> /// <returns>The resolved URL.</returns> public override string ResolveUrl(MvcSiteMapNode mvcSiteMapNode, string area, string controller, string action, IDictionary <string, object> routeValues) { return(base.ResolveUrl(mvcSiteMapNode, area, controller, action, routeValues).ToUpperInvariant()); }
private MenuItemData GenerateMenuItemFromSiteMapNode(ActionExecutingContext filterContext, MvcSiteMapNode node, SiteMapNode currentNode, UrlHelper urlHelper) { // HACK: it's possible that we have a querystring ?container=true. This is required when for a top-level // menu item with the same action and controller as one of the children. Leaving the parameter out causes the // sitemapprovider to crash because it doesn't allow duplicate url's. string url = node.Url.Replace("?container=true", String.Empty); return(new MenuItemData(VirtualPathUtility.ToAbsolute(url) , GlobalResources.ResourceManager.GetString(node.ResourceKey, Thread.CurrentThread.CurrentUICulture) , CheckInPath(node, currentNode, filterContext.RouteData) , node.Icon != null ? urlHelper.Content("~/manager/Content/images/" + node.Icon) : null)); }