Open Closed

Permission added at runtime cannot show at Permission Form #7506


User avatar
0
hanntd created

Hi ABP Team, I have customed to add permission at runtime and I can added to Permission Group and Permission Table but when open Permission on UI these added permission were not shown there. Code to add new Permission Group and Permission: Result: Didn't see on UI of Permission Setting:

How can I implement a dynamic Permission Management that I can Add/Remove at runtime? Thanks, Dharma Han Nguyen


13 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can refer to this question first.

    https://support.abp.io/QA/Questions/5490/Need-to-refresh-the-list-of-static-permission-definitions-without-site-reload?CurrentPage=1 https://support.abp.io/QA/Questions/5490/Need-to-refresh-the-list-of-static-permission-definitions-without-site-reload?CurrentPage=2

  • User Avatar
    0
    hanntd created

    Thanks, I have checked but it's very difficult to me for getting a final solution based on that. Do you have a final code of that?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I will share a sample project.

    https://github.com/abpframework/abp-samples/issues/273

  • User Avatar
    0
    hanntd created

    Thank you so much, I'll try it.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    : )

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The core class: https://github.com/abpframework/abp-samples/blob/master/DynamicPermission/src/BookStore.Web/Pages/Index.cshtml.cs

    https://github.com/abpframework/abp-samples/blob/master/DynamicPermission/README.md

  • User Avatar
    0
    hanntd created

    Thank you so much, if I'm using Blazor WebAssembly Application template I must implement this code in Application layer, right? Thanks

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Yes. You can give it a try.

  • User Avatar
    0
    hanntd created

    Hi, I could run well with your code provided but I'm using the new Blazor Web App Template of v8.2 so I have changed as bellow:

    1. Create method to add Group and Permission in Application layer

    2. Create Interface in Contracts

    3. Create Controller in HttpApi

    4. Run application can create Permission Group and Permission in database

    5. Check Permission from User/Role didn't see Group and Permission added Method in Application: public async Task<Task> AddPermissionAsync(string groupName, string permissionName) { var group = await _permissionGroupRepository.GetListAsync();

           if (group.All(x => x.Name != groupName))
           {
               var permissionGroup = new PermissionGroupDefinitionRecord
               {
                   Name = groupName,
                   DisplayName = $"L:{groupName},Permission: {groupName}"
               };
               await _permissionGroupRepository.InsertAsync(permissionGroup);
      
               var permissions = await _permissionRepository.GetListAsync();
               if (permissions.All(x => x.Name != permissionName))
               {
                   var permission = new PermissionDefinitionRecord
                   {
                       Name = permissionName,
                       DisplayName = $"L:{permissionName},Permission: {permissionName}",
                       GroupName = groupName,
                       IsEnabled = true,
                       MultiTenancySide = MultiTenancySides.Both
                   };
                   await _permissionRepository.InsertAsync(permission);
      
                   await _distributedCache.RemoveAsync($"{_cacheOptions.KeyPrefix}_AbpInMemoryPermissionCacheStamp");
               }
           }
           else
           {
               var permissionGroup = group.FirstOrDefault(x => x.Name == groupName);
               var permissions = await _permissionRepository.GetListAsync();
               if (permissions.All(x => x.Name != permissionName))
               {
                   var permission = new PermissionDefinitionRecord
                   {
                       Name = permissionName,
                       DisplayName = $"L:{permissionName},Permission: {permissionName}",
                       GroupName = groupName,
                       IsEnabled = true,
                       MultiTenancySide = MultiTenancySides.Both
                   };
                   await _permissionRepository.InsertAsync(permission);
      
                   await _distributedCache.RemoveAsync($"{_cacheOptions.KeyPrefix}_AbpInMemoryPermissionCacheStamp");
               }
           }
           return Task.CompletedTask;
       }
      

    If adding the following code in Blazor project it will threw the error, remove that code application run without error but cannot see created permission at runtime:

  • User Avatar
    0
    hanntd created

    Hi, I found the problem with LeptonManagment Feature:

    1. I have one Main application create by using Blazor Web App Template (v8.2) and some separated module created by using Module Template.
    2. When running module separately it created in db of related table such Feature, FeatureGroup... for LeptonManagment
    3. After Integrated my Modules to Main Application and applied Dynamic Permission I threw the error "Volo.Abp.AbpException: Undefined feature: LeptonManagement.Enable" so I must delete these information in db and run Main Applicaiton again it will run well and Dynamic Permission run well too.
    4. But if I dev on Module and run it for testing, debug... I will insert LeptonManagement again in Feature, FeatureGroup tables again. How can I prevent Modules insert LeptonManagement again?
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try the DeletedFeatures of AbpFeatureOptions.

    https://github.com/abpframework/abp/blob/d165e16f1d88c603e420d8afe1d5e33348d8af7d/framework/src/Volo.Abp.Features/Volo/Abp/Features/AbpFeatureOptions.cs#L12

    And you can use a different database for different modules and apps.

  • User Avatar
    0
    hanntd created

    Thanks, in my case I cannot use different databases for modules and app so I have tried to install Lepton Theme to my App using ABP Suite instead of deleting LeptonManagement feature and now my App running properly without error of Undefined feature. Thank you so much for your support. Dharma Han Nguyen

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    : )

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35