Search code examples
abp-framework

Show Hide Menus MVC jQuery


Can anyone help me out discovering the correct way to show/hide menu items according to AbpUserRoles?

I am simply adding two menu items that should only be available to role=admin.

My code causes the Admin menu to show when it should not:

Code:

var adminMenu = context.Menu.GetAdministration();
adminMenu.Items.Add(new ApplicationMenuItem("Admin.Sites", "Sites", "/Sites"));
adminMenu.Items.Add(new ApplicationMenuItem("Admin.UserSites", "User Sites", "/UserSites"));            

Solution

  • ok, so the way I ended up doing this was by getting hold of the CurrentUser as follows:

    public class YourProjectMenuContributor : IMenuContributor
    {
        private async Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
    
            if (context.GetHttpContext().User.IsInRole("admin"))
            {
                //....configure as required...
            }
        }
    }