Open Closed

Menu is not hiding while checking permission on menucontributor class #2810


User avatar
0
safi created

Hi

I need to hide one menu for admin only so I have added a permission check after login that menu is coming but when we reload or refresh page that time it's hiding that menu. Could you please provide me solution for this.

I tried below code but no luck.

 ICurrentUser currentUser = context.ServiceProvider.GetRequiredService<ICurrentUser>();

            bool IsAdmin = currentUser.IsInRole("admin");
 if (!IsAdmin)
                     {
          //menu code

            }
  • ABP Framework version: v4.4.3
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

47 Answer(s)
  • User Avatar
    0
    safi created

    Any update?

  • User Avatar
    0
    safi created

    Any update?

    It's urgent please reply

  • User Avatar
    0
    EngincanV created
    Support Team

    Hi @safi, unfortunately, the menu can't be updated without refreshing the page currently. There is an issue about this, you can follow the issue to see progress.

    Also, you can see this comment if you want to look for a workaround. You can apply a similar solution.

  • User Avatar
    0
    safi created

    Hi @safi, unfortunately, the menu can't be updated without refreshing the page currently. There is an issue about this, you can follow the issue to see progress.

    Also, you can see this comment if you want to look for a workaround. You can apply a similar solution.

    The problem is I have unchecked the permission checkbox and still able to see that menu after login.

  • User Avatar
    0
    safi created

    Hi

    Need your help

  • User Avatar
    0
    EngincanV created
    Support Team

    Hi @safi, sorry for the late response. Currently, there is a problem to check menu items according to role names (because of the ordering problem, the menu is drawn once and not updated according to auth changes properly). We aim to fix it in the next version. There is an open Github issue about it.

    Btw, your credit is refunded.

    In meantime, maybe you can consider using a permission check instead of a role name check, by using the RequirePermission("PermissionName") extension method.

    Example:

    context.Menu.AddItem(
        new ApplicationMenuItem("MyProject.Crm", l["Menu:CRM"])
            .AddItem(new ApplicationMenuItem(
                    name: "MyProject.Crm.Customers",
                    displayName: l["Menu:Customers"],
                    url: "/crm/customers")
                .RequirePermissions("MyProject.Crm.Customers") //require permission
            );
    );
    
  • User Avatar
    0
    safi created

    Hi @safi, sorry for the late response. Currently, there is a problem to check menu items according to role names (because of the ordering problem, the menu is drawn once and not updated according to auth changes properly).
    We aim to fix it in the next version. There is an open Github issue about it.

    Btw, your credit is refunded.

    In meantime, maybe you can consider using a permission check instead of a role name check, by using the RequirePermission("PermissionName") extension method.

    Example:

    context.Menu.AddItem( 
        new ApplicationMenuItem("MyProject.Crm", l["Menu:CRM"]) 
            .AddItem(new ApplicationMenuItem( 
                    name: "MyProject.Crm.Customers", 
                    displayName: l["Menu:Customers"], 
                    url: "/crm/customers") 
                .RequirePermissions("MyProject.Crm.Customers") //require permission 
            ); 
    ); 
    

    No it's not working so can we connect on zoom for this?

  • User Avatar
    0
    maliming created
    Support Team

    hi

    This seems a known problem. it was fixed in 5.0

    https://github.com/abpframework/abp/issues/8851 https://github.com/abpframework/abp/commit/dc9f278c11ed5a017a878cbcd7ce9222ab70ac4e

  • User Avatar
    0
    safi created

    hi

    This seems a known problem. it was fixed in 5.0

    https://github.com/abpframework/abp/issues/8851 https://github.com/abpframework/abp/commit/dc9f278c11ed5a017a878cbcd7ce9222ab70ac4e

    Thanks for the response but the problem is we can't update the version now so can u please suggest what I can do in this case.

  • User Avatar
    0
    maliming created
    Support Team

    You can download Lepton's source code, and change and use it.

    Please leave an email and I will send the details of the changes.

  • User Avatar
    0
    maliming created
    Support Team

    You can override these classes and apply the changes.

    https://docs.abp.io/en/abp/latest/UI/Blazor/Customization-Overriding-Components

    namespace Volo.Abp.AspNetCore.Components.Web.LeptonTheme.Components.ApplicationLayout.Navigation
    class MainSiderbar
    
    namespace Volo.Abp.AspNetCore.Components.WebAssembly.LeptonTheme.Components.ApplicationLayout.MainHeader
    class MainHeaderToolbarUserMenu
    

  • User Avatar
    0
    safi created

    You can override these classes and apply the changes.

    https://docs.abp.io/en/abp/latest/UI/Blazor/Customization-Overriding-Components

    namespace Volo.Abp.AspNetCore.Components.Web.LeptonTheme.Components.ApplicationLayout.Navigation 
    class MainSiderbar 
     
    namespace Volo.Abp.AspNetCore.Components.WebAssembly.LeptonTheme.Components.ApplicationLayout.MainHeader 
    class MainHeaderToolbarUserMenu 
    

    where I need to add this Mainsidebar

  • User Avatar
    0
    maliming created
    Support Team

    Add these to your balzor project.

    https://docs.abp.io/en/abp/latest/UI/Blazor/Customization-Overriding-Components#example-replacing-with-the-code-behind-file

  • User Avatar
    0
    safi created

    Add these to your balzor project.

    https://docs.abp.io/en/abp/latest/UI/Blazor/Customization-Overriding-Components#example-replacing-with-the-code-behind-file

    can we connect on zoom. still getting errors.

  • User Avatar
    0
    maliming created
    Support Team

    What are your steps? And what are the errors?

  • User Avatar
    0
    safi created

    What are your steps? And what are the errors?

    I have added mainsidebar and mainheadertoolbarusermanu inside components folder and getting below error

  • User Avatar
    0
    maliming created
    Support Team

    Please check this document carefully https://docs.abp.io/en/abp/latest/UI/Blazor/Customization-Overriding-Components#example-replacing-with-the-code-behind-file

  • User Avatar
    0
    safi created

    Please check this document carefully https://docs.abp.io/en/abp/latest/UI/Blazor/Customization-Overriding-Components#example-replacing-with-the-code-behind-file

    I copied code from above answer https://support.abp.io/QA/files/c24af87f24acad470c7b3a0333981190.png

    https://support.abp.io/QA/files/91d3c9b655169d2d50cb3a033397ea66.png

    Please it's very important for me to resolve this can we connect on zoom?

  • User Avatar
    0
    maliming created
    Support Team

    hi

    The images are the changes that need to override in the subclass.

    You need to create a subclass to replace the class and make the changs.

  • User Avatar
    0
    safi created

    Ok can u please check this and reply https://support.abp.io/QA/Questions/2901/How-can-I-set-a-landing-page-for-admin-in-Abp-io

  • User Avatar
    0
    maliming created
    Support Team

    ok, the support team will reply as soon as possible.

  • User Avatar
    0
    safi created

    hi

    The images are the changes that need to override in the subclass.

    You need to create a subclass to replace the class and make the changs.

    I changed everything and getting this error now pls help asap.

  • User Avatar
    0
    maliming created
    Support Team

    Your image is broken.

  • User Avatar
    0
    safi created

  • User Avatar
    0
    maliming created
    Support Team

    Please share your code.

  • User Avatar
    0
    safi created

    Mainsidebar.razor

    .cs code

    MainHeadertoolbarUserMenu.razor

    .cs code

    namespace Volo.Abp.AspNetCore.Components.WebAssembly.LeptonTheme.Components.ApplicationLayout.MainHeader { public partial class RKIMainHeaderToolbarUserMenu : IDisposable { [Inject] protected IMenuManager MenuManager { get; set; }

        [Inject]
        protected ICurrentUser CurrentUser { get; set; }
    
        [Inject]
        protected ICurrentTenant CurrentTenant { get; set; }
    
        [Inject]
        protected SignOutSessionStateManager SignOutManager { get; set; }
    
        [Inject]
        protected NavigationManager Navigation { get; set; }
    
        [Inject]
        protected AuthenticationStateProvider AuthenticationStateProvider { get; set; }
    
        [Inject]
        protected IStringLocalizer&lt;AbpUiResource&gt; UiLocalizer { get; set; }
    
        [Inject]
        protected IOptions&lt;AbpRemoteServiceOptions&gt; RemoteServiceOptions { get; set; }
    
        [Inject]
        protected IJSRuntime JsRuntime { get; set; }
    
        protected ApplicationMenu UserMenu { get; set; }
    
        protected Guid? UserId { get; set; }
    
        protected string UserName { get; set; }
    
        protected string TenantName { get; set; }
    
        protected string ProfileImageUrl { get; set; }
    
        protected string UserFullName { get; set; }
    
        protected override async Task OnInitializedAsync()
        {
            await SetUserMenuAndProfileAsync();
    
            Navigation.LocationChanged += OnLocationChanged;
            AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProviderOnAuthenticationStateChanged;
        }
    
        private async Task SetUserMenuAndProfileAsync()
        {
            UserMenu = await MenuManager.GetAsync(StandardMenus.User);
    
            UserId = CurrentUser.Id;
            UserName = CurrentUser.UserName;
            UserFullName = CalculateUserFullName();
            TenantName = CurrentTenant.Name;
    
            if (UserId != null)
            {
                ProfileImageUrl = RemoteServiceOptions.Value.RemoteServices.GetConfigurationOrDefaultOrNull("AbpAccountPublic")?.BaseUrl.TrimEnd('/') +
                                  $"/api/account/profile-picture-file/{UserId}";
            }
        }
    
        protected virtual void OnLocationChanged(object sender, LocationChangedEventArgs e)
        {
            InvokeAsync(StateHasChanged);
        }
    
        private async void AuthenticationStateProviderOnAuthenticationStateChanged(Task&lt;AuthenticationState&gt; task)
        {
            await SetUserMenuAndProfileAsync();
            await InvokeAsync(StateHasChanged);
        }
    
        protected virtual async Task BeginSignOut()
        {
            await SignOutManager.SetSignOutState();
            await NavigateToAsync("authentication/logout");
        }
    
        protected virtual async Task NavigateToAsync(string uri, string target = null)
        {
            if (target == "_blank")
            {
                await JsRuntime.InvokeVoidAsync("open", uri, target);
            }
            else
            {
                Navigation.NavigateTo(uri);
            }
    
        }
    
        protected virtual string CalculateUserFullName()
        {
            //TODO: Should we move this logic to some extension method for the ICurrentUser?
    
            var fullName = new StringBuilder();
    
            if (!CurrentUser.Name.IsNullOrEmpty())
            {
                fullName.Append(CurrentUser.Name);
            }
    
            if (!CurrentUser.SurName.IsNullOrEmpty())
            {
                if (fullName.Length > 0)
                {
                    fullName.Append(" ");
                }
    
                fullName.Append(CurrentUser.SurName);
            }
    
            if (fullName.Length == 0)
            {
                fullName.Append(CurrentUser.UserName);
            }
    
            return fullName.ToString();
        }
    
        public void Dispose()
        {
            Navigation.LocationChanged -= OnLocationChanged;
            AuthenticationStateProvider.AuthenticationStateChanged -= AuthenticationStateProviderOnAuthenticationStateChanged;
        }
    }
    

    }

  • User Avatar
    0
    maliming created
    Support Team

    I can't see any subclass of MainSiderbar and MainHeaderToolbarUserMenu

  • User Avatar
    0
    safi created

    I can't see any subclass of MainSiderbar and MainHeaderToolbarUserMenu

    pls share subclass

  • User Avatar
    0
    maliming created
    Support Team

    You need to create a subclass of these two classes, change some code and replace the built-in services in the dependency injection container.

    [ExposeServices(typeof(MainSiderbar))]
    [Dependency(ReplaceServices = true)]
    MyMainSiderbar : MainSiderbar 
    
  • User Avatar
    0
    safi created

    MyMainSiderbar : MainSiderbar

    I shared my code in email so please check.

  • User Avatar
    0
    safi created

    You need to create a subclass of these two classes, change some code and replace the built-in services in the dependency injection container.

    [ExposeServices(typeof(MainSiderbar))] 
    [Dependency(ReplaceServices = true)] 
    MyMainSiderbar : MainSiderbar  
    

    are u there?

  • User Avatar
    0
    maliming created
    Support Team

    There seems to be no problem, can you test it in the project?

  • User Avatar
    0
    safi created

    There seems to be no problem, can you test it in the project?

    no, it's not working. I am requesting can we connect on zoom because it's taking too much time to check and post and ask for solutions here again and again. Today is my deadline for this project that's why I am bothering you, again and again, hope you can understand this.

    Thanks,

  • User Avatar
    0
    safi created

    There seems to be no problem, can you test it in the project?

    can we check together?

  • User Avatar
    0
    alper created
    Support Team

    @safi we can't solve your problem by iterating every step. send us your project to [email protected]

  • User Avatar
    0
    safi created

    @safi we can't solve your problem by iterating every step. send us your project to [email protected]

    Hi Albert

    I can't share project due to security that's why I am asking to connect on zoom and check this.

  • User Avatar
    0
    maliming created
    Support Team

    Just inherit the base class and override some methods and replace the built-in services.

    What is the problem or bug now?

  • User Avatar
    0
    safi created

    Just inherit the base class and override some methods and replace the built-in services.

    What is the problem or bug now? shared code in mail.

  • User Avatar
    0
    EngincanV created
    Support Team

    Does your .HttpApi.Host and .IdentityServer projects are running? It seems it couldn't get a response from localhost:44358 and localhost:63079

  • User Avatar
    0
    safi created

    Does your .HttpApi.Host and .IdentityServer projects are running? It seems it couldn't get a response from localhost:44358 and localhost:63079

    yes swagger is working

  • User Avatar
    0
    safi created

    Does your .HttpApi.Host and .IdentityServer projects are running? It seems it couldn't get a response from localhost:44358 and localhost:63079

    can u share mail id or can we connect on zoom?

  • User Avatar
    0
    EngincanV created
    Support Team

    Sure, you can send your project to [email protected]

  • User Avatar
    0
    safi created

    Sure, you can send your project to [email protected]

    Hi

    I can't share project but I shared code and error in mail so can you pls check.

  • User Avatar
    0
    safi created

    Sure, you can send your project to [email protected]

    are u checking?

  • User Avatar
    0
    EngincanV created
    Support Team

    Sure, you can send your project to [email protected]

    are u checking?

    I've checked and write you an email back, can you check and apply it?

  • User Avatar
    0
    safi created

    Sure, you can send your project to [email protected]

    are u checking?

    I've checked and write you an email back, can you check and apply it?

    yes it's working now thanks much :)

  • User Avatar
    0
    EngincanV created
    Support Team

    It's nice to hear that :)