Skip to content

Instantly share code, notes, and snippets.

@toburger
Created September 26, 2012 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toburger/3787089 to your computer and use it in GitHub Desktop.
Save toburger/3787089 to your computer and use it in GitHub Desktop.
using Contrib.Navigation.Models;
using Orchard;
using Orchard.ContentManagement;
using Orchard.Logging;
using Orchard.Roles.Models;
using Orchard.Roles.Services;
using Orchard.Security;
using Orchard.Security.Permissions;
using Orchard.UI.Admin;
using Orchard.UI.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Contrib.Navigation
{
public class ActionLinkNavigationFilter : INavigationFilter
{
private IAuthorizationService _authorizationService;
private IOrchardServices _orchardServices;
private UrlHelper _urlHelper;
public ActionLinkNavigationFilter(IAuthorizationService authorizationService, IOrchardServices orchardServices, UrlHelper urlHelper)
{
_authorizationService = authorizationService;
_orchardServices = orchardServices;
_urlHelper = urlHelper;
}
public ILogger Logger { get; set; }
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> menuItems)
{
// if you want to show the menu for users with "DebugShowAllMenuItems permission uncomment line 37 and line 52
//var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
bool isAnonymous = true;
var roles = _orchardServices.WorkContext.CurrentUser.As<IUserRoles>();
if (roles != null)
{
isAnonymous = roles.Roles.Any(r => r == "Anonymous");
}
foreach (var item in menuItems)
{
if (item.Content != null && item.Content.ContentItem.ContentType == "ActionLink")
{
var content = item.Content.As<ActionLinkPart>();
if (//hasDebugShowAllMenuItems ||
AdminFilter.IsApplied(_urlHelper.RequestContext) ||
isAnonymous)
{
yield return new MenuItem
{
Items = Filter(item.Items),
Permissions = item.Permissions,
Position = item.Position,
RouteValues = item.RouteValues,
LocalNav = item.LocalNav,
Culture = item.Culture,
Text = item.Text,
IdHint = item.IdHint,
Classes = item.Classes,
Url = item.Url,
LinkToFirstChild = item.LinkToFirstChild,
Href = item.Href,
Content = item.Content
};
}
}
else
{
yield return item;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment