Starts in:
2 DAYS
9 HRS
9 MIN
46 SEC
Starts in:
2 D
9 H
9 M
46 S
Open Closed

How to set permissions for dynamic menu items #5962


User avatar
0
duyan11110 created
  • ABP Framework version: v7.3.0
  • UI Type: MVC
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes
  • Exception message and full stack trace:
  • Steps to reproduce the issue: I want to set permissions for dynamic menu items. I can mimic your CMS Kit module to create dynamic menu items but don't know how to set permissions for them. Please suggest me steps to achieve that. Thank you.

25 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    The CMS kit module does not support such feature yet.

    You can custom the module:

    ObjectExtensionManager.Instance.Modules()
        .ConfigureCmsKit(cmsKit =>
      {
          cmsKit.ConfigureMenuItem(menu =>
          {
              menu.AddOrUpdateProperty<string>( //property type: string
                  "PermissionName", //property name
                  property =>
                  {
                     
                  }
              );
          });
      });
    
    public class MyCmsKitPublicMenuContributor : IMenuContributor
    {
        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if (context.Menu.Name == CmsKitMenus.Public)
            {
                await ConfigureMainMenuAsync(context);
            }
        }
    
        private async Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            var featureChecker = context.ServiceProvider.GetRequiredService<IFeatureChecker>();
            if (GlobalFeatureManager.Instance.IsEnabled<MenuFeature>() && await featureChecker.IsEnabledAsync(CmsKitFeatures.MenuEnable))
            {
                var menuAppService = context.ServiceProvider.GetRequiredService<IMenuItemPublicAppService>();
                var permissionCheck = context.ServiceProvider.GetRequiredService<IPermissionChecker>();
    
                var menuItems = await menuAppService.GetListAsync();
    
                if (!menuItems.IsNullOrEmpty())
                {
                    foreach (var menuItemDto in menuItems.Where(x => x.ParentId == null && x.IsActive))
                    {
                        var permissionName = menuItemDto.GetProperty<string>("PermissionName");
                        if (!permissionName.IsNullOrWhiteSpace() && !await permissionCheck.IsGrantedAsync(permissionName))
                        {
                            continue;
                        }
                        
                        AddChildItems(menuItemDto, menuItems, context.Menu);
                    }
                }
            }
        }
    
        private void AddChildItems(MenuItemDto menuItem, List<MenuItemDto> source, IHasMenuItems parent = null)
        {
            var applicationMenuItem = CreateApplicationMenuItem(menuItem);
    
            foreach (var item in source.Where(x => x.ParentId == menuItem.Id && x.IsActive))
            {
                AddChildItems(item, source, applicationMenuItem);
            }
    
            parent?.Items.Add(applicationMenuItem);
        }
    
        private ApplicationMenuItem CreateApplicationMenuItem(MenuItemDto menuItem)
        {
            return new ApplicationMenuItem(
                menuItem.DisplayName,
                menuItem.DisplayName,
                menuItem.Url,
                menuItem.Icon,
                menuItem.Order,
                menuItem.Target,
                menuItem.ElementId,
                menuItem.CssClass
            );
        }
    }
    
    Configure<AbpNavigationOptions>(options =>
    {
        options.MenuContributors.RemoveAll(x => x.GetType() == typeof(CmsKitPublicMenuContributor));
        options.MenuContributors.Add(new MyCmsKitPublicMenuContributor());
    });
    

  • User Avatar
    0
    duyan11110 created

    Hi,

    Thanks for your code. But do I have to create permission name for each menu item before creating that menu item? I want to set permissions dynamically according to creating menu items.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Yes.

    I want to set permissions dynamically according to creating menu items.

    You need to implement it yourself.

    But you can't add permissions dynamically, you can consider creating your own menu permission table and check menu permissions in the middleware

  • User Avatar
    0
    duyan11110 created

    Hi,

    Here is my idea. Please advice if it is feasible.

    1. Create new table: MenuItems (Id, Url, ...)
    2. After saving menu items I will create all permission names for those menu items, with permission name = Id of menu item. These permission names will be under "MenuItem" permission group
    3. Create new page to set permissions of menu items for a role and save into AbpPermissionGrant
    4. Loading dynamic menu items with permissions checking will be like this: (copy from CMS Kit module for example)
    private async Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            var featureChecker = context.ServiceProvider.GetRequiredService<IFeatureChecker>();
            if (GlobalFeatureManager.Instance.IsEnabled<MenuFeature>() && await featureChecker.IsEnabledAsync(CmsKitFeatures.MenuEnable))
            {
                var menuAppService = context.ServiceProvider.GetRequiredService<IMenuItemPublicAppService>();
    
                var menuItems = await menuAppService.GetListAsync();
    
                if (!menuItems.IsNullOrEmpty())
                {
                    foreach (var menuItemDto in menuItems.Where(x => x.ParentId == null && x.IsActive))
                    {
                        AddChildItems(menuItemDto, menuItems, context.Menu);
                    }
                }
            }
        }
    
        private void AddChildItems(MenuItemDto menuItem, List<MenuItemDto> source, IHasMenuItems parent = null)
        {
            var applicationMenuItem = CreateApplicationMenuItem(menuItem);
    
            foreach (var item in source.Where(x => x.ParentId == menuItem.Id && x.IsActive))
            {
                AddChildItems(item, source, applicationMenuItem);
            }
    
            parent?.Items.Add(applicationMenuItem);
        }
    
        private ApplicationMenuItem CreateApplicationMenuItem(MenuItemDto menuItem)
        {
            return new ApplicationMenuItem(
                menuItem.DisplayName,
                menuItem.DisplayName,
                menuItem.Url,
                menuItem.Icon,
                menuItem.Order,
                menuItem.Target,
                menuItem.ElementId,
                menuItem.CssClass
            ).RequirePermissions(menuItem.Id.ToString());
        }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can give it a try

  • User Avatar
    0
    duyan11110 created

    Hi,

    OK. Thanks. I will try it. But I need your guide when inserting data into permission related tables. Is there anything I have to pay attention?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    For performance, ABP will cache permission definitions and check CacheStamp.

    https://github.com/abpframework/abp/blob/1f16df3fd426d3a8ac4ad68c1c59d8f080c5f26a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/DynamicPermissionDefinitionStore.cs#L91-L109

    You should update the CacheStamp value after inserting a permission. https://github.com/abpframework/abp/blob/1f16df3fd426d3a8ac4ad68c1c59d8f080c5f26a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/DynamicPermissionDefinitionStoreInMemoryCache.cs#L17

  • User Avatar
    0
    duyan11110 created

    Hi,

    What is the best way to get role list from IdentityService inAdministraionService?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can inject the IIdentityRoleAppService to get roles.

  • User Avatar
    0
    duyan11110 created

    Hi, I have to add reference to HttpClient proj of IdentityService and configure RemoteService in appsettings.json, right?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    The microservice template is pre-configured:

  • User Avatar
    0
    duyan11110 created

    Hi,

    How to reload granted permissions? I call await _dynamicPermissionDefinitionStore.GetPermissionsAsync(); as your suggestion but not work

    private ApplicationMenuItem CreateApplicationMenuItem(MenuItemDto menuItem)
    {
        return new ApplicationMenuItem(
            menuItem.DisplayName,
            menuItem.DisplayName,
            menuItem.Url,
            menuItem.Icon,
            menuItem.Order,
            menuItem.Target,
            menuItem.ElementId,
            menuItem.CssClass
        ).RequirePermissions(menuItem.Id.ToString());
    }
    

    When I remove .RequirePermissions(menuItem.Id.ToString()) the dynamic menu items show correctly

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    What's the error log?

  • User Avatar
    0
    duyan11110 created

    Hi,

    I don't see any error.

    [administration-service_76245987-4]: [13:30:52 DBG] Executed AbpApplicationConfigurationAppService.GetAsync().
    [administration-service_76245987-4]: [13:30:52 INF] Executed action method Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationConfigurationController.GetAsync (Volo.Abp.AspNetCore.Mvc), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 103.9099ms.
    [administration-service_76245987-4]: [13:30:52 INF] Executing ObjectResult, writing value of type 'Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ApplicationConfigurationDto'.
    [administration-service_76245987-4]: [13:30:52 INF] Executed action Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationConfigurationController.GetAsync (Volo.Abp.AspNetCore.Mvc) in 106.7414ms
    [administration-service_76245987-4]: [13:30:52 INF] Executed endpoint 'Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationConfigurationController.GetAsync (Volo.Abp.AspNetCore.Mvc)'
    [administration-service_76245987-4]: [13:30:52 INF] Request finished HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/abp/application-configuration?IncludeLocalizationResources=False&api-version=1.0 - - - 200 - application/json;+charset=utf-8 110.7096ms
    [administration-service_76245987-4]: [13:30:52 INF] Request starting HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/abp/application-localization?CultureName=en&OnlyDynamics=True&api-version=1.0 - -
    [administration-service_76245987-4]: [13:30:52 INF] Executing endpoint 'Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationLocalizationController.GetAsync (Volo.Abp.AspNetCore.Mvc)'
    [administration-service_76245987-4]: [13:30:52 INF] Route matched with {area = "abp", action = "Get", controller = "AbpApplicationLocalization", page = ""}. Executing controller action with signature System.Threading.Tasks.Task`1[Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ApplicationLocalizationDto] GetAsync(Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ApplicationLocalizationRequestDto) on controller Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationLocalizationController (Volo.Abp.AspNetCore.Mvc).
    [administration-service_76245987-4]: [13:30:52 INF] Executing action method Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationLocalizationController.GetAsync (Volo.Abp.AspNetCore.Mvc) - Validation state: Valid
    [administration-service_76245987-4]: [13:30:52 INF] Executed action method Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationLocalizationController.GetAsync (Volo.Abp.AspNetCore.Mvc), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 132.7568ms.
    [administration-service_76245987-4]: [13:30:52 INF] Executing ObjectResult, writing value of type 'Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ApplicationLocalizationDto'.
    [administration-service_76245987-4]: [13:30:52 INF] Executed action Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationLocalizationController.GetAsync (Volo.Abp.AspNetCore.Mvc) in 134.5931ms
    [administration-service_76245987-4]: [13:30:52 INF] Executed endpoint 'Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.AbpApplicationLocalizationController.GetAsync (Volo.Abp.AspNetCore.Mvc)'
    [administration-service_76245987-4]: [13:30:52 INF] Request finished HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/abp/application-localization?CultureName=en&OnlyDynamics=True&api-version=1.0 - - - 200 - application/json;+charset=utf-8 138.5244ms
    [administration-service_76245987-4]: [13:30:52 INF] Request starting HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/administration-service/menu-item/menu?api-version=1.0 - -
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:U,pk:67382431-cb1b-e66f-3384-3a0a1b180a20,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:U,pk:67382431-cb1b-e66f-3384-3a0a1b180a20,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:R,pk:admin,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:R,pk:admin,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:C,pk:Web.DockerHttpCore,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:C,pk:Web.DockerHttpCore,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 INF] Executing endpoint 'MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application)'
    [administration-service_76245987-4]: [13:30:53 INF] Route matched with {action = "GetAllMenu", controller = "MenuItem", area = "", page = ""}. Executing controller action with signature System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[MZH.MHIBS.AdministrationService.Menus.MenuItemDto]] GetAllMenuAsync() on controller MZH.MHIBS.AdministrationService.Menus.MenuItemAppService (MZH.MHIBS.AdministrationService.Application).
    [administration-service_76245987-4]: [13:30:53 INF] Executing action method MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application) - Validation state: Valid
    [administration-service_76245987-4]: [13:30:53 INF] Executed action method MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 4.5297ms.
    [administration-service_76245987-4]: [13:30:53 DBG] Added 0 entity changes to the current audit log
    [administration-service_76245987-4]: [13:30:53 INF] Executing ObjectResult, writing value of type 'System.Collections.Generic.List`1[[MZH.MHIBS.AdministrationService.Menus.MenuItemDto, MZH.MHIBS.AdministrationService.Application.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
    [administration-service_76245987-4]: [13:30:53 INF] Executed action MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application) in 9.1316ms
    [administration-service_76245987-4]: [13:30:53 INF] Executed endpoint 'MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application)'
    [administration-service_76245987-4]: [13:30:53 DBG] Added 0 entity changes to the current audit log
    [administration-service_76245987-4]: [13:30:53 INF] Request finished HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/administration-service/menu-item/menu?api-version=1.0 - - - 200 - application/json;+charset=utf-8 20.4294ms
    [administration-service_76245987-4]: [13:30:53 INF] Request starting HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/administration-service/menu-item/menu?api-version=1.0 - -
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:U,pk:67382431-cb1b-e66f-3384-3a0a1b180a20,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:U,pk:67382431-cb1b-e66f-3384-3a0a1b180a20,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:R,pk:admin,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:R,pk:admin,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:C,pk:Web.DockerHttpCore,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:C,pk:Web.DockerHttpCore,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 INF] Executing endpoint 'MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application)'
    [administration-service_76245987-4]: [13:30:53 INF] Route matched with {action = "GetAllMenu", controller = "MenuItem", area = "", page = ""}. Executing controller action with signature System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[MZH.MHIBS.AdministrationService.Menus.MenuItemDto]] GetAllMenuAsync() on controller MZH.MHIBS.AdministrationService.Menus.MenuItemAppService (MZH.MHIBS.AdministrationService.Application).
    [administration-service_76245987-4]: [13:30:53 INF] Executing action method MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application) - Validation state: Valid
    [administration-service_76245987-4]: [13:30:53 INF] Executed action method MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 5.0168ms.
    [administration-service_76245987-4]: [13:30:53 DBG] Added 0 entity changes to the current audit log
    [administration-service_76245987-4]: [13:30:53 INF] Executing ObjectResult, writing value of type 'System.Collections.Generic.List`1[[MZH.MHIBS.AdministrationService.Menus.MenuItemDto, MZH.MHIBS.AdministrationService.Application.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
    [administration-service_76245987-4]: [13:30:53 INF] Executed action MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application) in 8.5125ms
    [administration-service_76245987-4]: [13:30:53 INF] Executed endpoint 'MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application)'
    [administration-service_76245987-4]: [13:30:53 DBG] Added 0 entity changes to the current audit log
    [administration-service_76245987-4]: [13:30:53 INF] Request finished HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/administration-service/menu-item/menu?api-version=1.0 - - - 200 - application/json;+charset=utf-8 24.7428ms
    [administration-service_76245987-4]: [13:30:53 INF] Request starting HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/administration-service/menu-item/menu?api-version=1.0 - -
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:U,pk:67382431-cb1b-e66f-3384-3a0a1b180a20,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:U,pk:67382431-cb1b-e66f-3384-3a0a1b180a20,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:R,pk:admin,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:R,pk:admin,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] PermissionStore.GetCacheItemAsync: pn:C,pk:Web.DockerHttpCore,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 DBG] Found in the cache: pn:C,pk:Web.DockerHttpCore,n:AdministrationService.MenuItems
    [administration-service_76245987-4]: [13:30:53 INF] Executing endpoint 'MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application)'
    [administration-service_76245987-4]: [13:30:53 INF] Route matched with {action = "GetAllMenu", controller = "MenuItem", area = "", page = ""}. Executing controller action with signature System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[MZH.MHIBS.AdministrationService.Menus.MenuItemDto]] GetAllMenuAsync() on controller MZH.MHIBS.AdministrationService.Menus.MenuItemAppService (MZH.MHIBS.AdministrationService.Application).
    [administration-service_76245987-4]: [13:30:53 INF] Executing action method MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application) - Validation state: Valid
    [administration-service_76245987-4]: [13:30:53 INF] Executed action method MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 5.5035ms.
    [administration-service_76245987-4]: [13:30:53 DBG] Added 0 entity changes to the current audit log
    [administration-service_76245987-4]: [13:30:53 INF] Executing ObjectResult, writing value of type 'System.Collections.Generic.List`1[[MZH.MHIBS.AdministrationService.Menus.MenuItemDto, MZH.MHIBS.AdministrationService.Application.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
    [administration-service_76245987-4]: [13:30:53 INF] Executed action MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application) in 9.3429ms
    [administration-service_76245987-4]: [13:30:53 INF] Executed endpoint 'MZH.MHIBS.AdministrationService.Menus.MenuItemAppService.GetAllMenuAsync (MZH.MHIBS.AdministrationService.Application)'
    [administration-service_76245987-4]: [13:30:53 DBG] Added 0 entity changes to the current audit log
    [administration-service_76245987-4]: [13:30:53 INF] Request finished HTTP/1.1 GET http://mzh.mhibs.administrationservice.httpapi.host:7001/api/administration-service/menu-item/menu?api-version=1.0 - - - 200 - application/json;+charset=utf-8 19.2255ms
    
  • User Avatar
    0
    duyan11110 created

    Hi,

    It works now. The Id need to be uppercase: .RequirePermissions(menuItem.Id.ToString().ToUpper()), but when I refresh the page, the granted menus are not displayed immediately. I have to manually clear redis cache. So, which is the right method I have to call to refresh the cache when changing permissions?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can call the UpdateProviderKeyAsync to remove old cache. https://github.com/abpframework/abp/blob/dev/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionManager.cs#L172

  • User Avatar
    0
    duyan11110 created

    Hi,

    Thanks for your suggestion. I inject IDistributedCache<PermissionGrantCacheItem> and call it's RemoveManyAsync method => it works perfectly now.

  • User Avatar
    0
    duyan11110 created

    Hi,

    Removing distributed cache with above method call when deploying on K8s does not affect immediately. The cache is refreshed after 5 minutes (I guess). Is there anything I have to do? How to remove old cache immediately after changing permissions?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can change the ApplicationConfigurationDtoCacheAbsoluteExpiration of the AbpAspNetCoreMvcClientCacheOptions.

    The value is 300s by default.

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/AbpAspNetCoreMvcClientCacheOptions.cs

  • User Avatar
    0
    duyan11110 created

    Hi,

    Can I use RefreshManyAsync method of IDistributedCache<PermissionGrantCacheItem> to refresh the cache whenever I change permissions?

    I don't understand why RemoveManyAsync not work when deploying on K8s?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Can I use RefreshManyAsync method of IDistributedCache<PermissionGrantCacheItem> to refresh the cache whenever I change permissions?

    The Web application gets application configuration(permission etc..) from the remote server and will cache it.

    I don't understand why RemoveManyAsync not work when deploying on K8s?

    Because in the Development , the cache time is 5s https://github.com/abpframework/abp/blob/26dbeb6747f54bde28d498ae0848b0974de9a303/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/AbpAspNetCoreMvcClientModule.cs#L19-L22

  • User Avatar
    0
    duyan11110 created

    Hi, So that means there is no way to refresh the cache immediately, right?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can consider using the eventbus to refresh the cache.

    • Publish an event from the microservices
    • Handle the event in the web application to refresh the cache.
  • User Avatar
    0
    duyan11110 created

    Hi, Just call RefreshManyAsync above to refresh or other method?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can just remove the cache

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06