Remove «Reports» from the global Episerver menu - and the forum is great!

Today, a colleague asked how to remove the «Reports» menu item from the global menu in Episerver, but only for users not belonging to a specific group. I did not know how to do that. But, I do know that the people who hang out in the Episerver World forum, Episerver's developer community, know a great deal. So I asked them.

The menu item to be removed.

I quickly got some great suggestions, but Jake Jones came up with the most elegant solution.

public class LimitedReportsMenuAssembler : MenuAssembler
{
    public LimitedReportsMenuAssembler(IMenuProvider[] menuProviders, IServiceLocator container) : base(menuProviders, container)
    {
    }

    public override IEnumerable<MenuItem> GetMenuItems(string parentPath, int relativeDepth)
    {
        var menuItems = base.GetMenuItems(parentPath, relativeDepth).ToList();

        var reportItem = menuItems.SingleOrDefault(x => x.Path.Equals("/global/cms/report", StringComparison.Ordinal));

        if (reportItem == null)
        {
            return menuItems;
        }

        if (!PrincipalInfo.Current.RoleList.Contains("ReportsPeople"))
        {
            menuItems.Remove(reportItem);
        }

        return menuItems;
    }
}

I would also need to register this class with Episerver's IoC container. That could be done like this.

[InitializableModule]
public class DependencyResolverInitialization : IConfigurableModule
{
    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        context.ConfigurationComplete += (o, e) =>
        {
            context.Services.AddSingleton<MenuAssembler, LimitedReportsMenuAssembler>();
        };
    }
}

Thanks to Jake Jones and all the great people at Episerver World for saving my day, once again.