Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Add a new permission from the database in SetPermissions() method in the AppAuthorizationProvider.cs #12214


User avatar
0
Vidyadhar created

Hi,

We're using ABP Zero Template version 10.0.0.0. We added a new method AddNewPermission() in PermissionAppService.cs to create a new permission from the database table.

Below is the sample code

PermissionAppService.cs: public void AddNewPermission(ImportLogData permissionDto) { var permissionProvider = new AppAuthorizationProvider(true); permissionProvider.AddPermission(_permissionDefinitionContext, permissionDto.Message); }

AppAuthorizationProvider.cs: public void AddPermission(IPermissionDefinitionContext context, string permissionName) { var myGroup = context.GetPermissionOrNull("Pages.Reports"); myGroup?.CreateChildPermission(permissionName, new FixedLocalizableString(permissionName)); }

We're unable to access the method in the application. Please review the screenshots below for reference.

<br> Could you help us how to add a new permission from the database table into SetPermissions() in AppAuthorizationProvider.cs?

Regards, Vidyadhar


6 Answer(s)
  • User Avatar
    0
    Vidyadhar created

    Hi, Any updates?

    Regards, Vidyadhar.

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi Vidyadhar

    Sorry for the late reply. What is the value of the permissionProvider variable in the AddNewPermission method when the method is called?

  • User Avatar
    0
    Vidyadhar created

    Hi oguzhanagir, Thanks for the update, We placed the AddNewPermission() method in the PermissionAppService.cs file, but none of the methods in PermissionAppService.cs are accessible from the application. We added a constructor with IPermissionDefinitionContext: private readonly IPermissionDefinitionContext _permissionDefinitionContext; public PermissionAppService(IPermissionDefinitionContext permissionDefinitionContext) { _permissionDefinitionContext = permissionDefinitionContext; }

    Then, we defined AddNewPermission() to add new permissions to an existing permission section: public void AddNewPermission(string permissionName) { var permissionProvider = new AppAuthorizationProvider(true); permissionProvider.AddPermission(_permissionDefinitionContext, permissionName); }

    However, after adding this code, we are unable to access any methods in PermissionAppService.cs from the application. If the above issue resloves then we can find the value of permissionProvider variable in the AddNewPermission method Note: Add new permissions dynamically from the database table to the permissions tree section.

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Vidyadhar,

    Could you override GetAllPermissions methods at PermissionManager? You can add your permissions at here.

  • User Avatar
    0
    [email protected] created

    Hi @Vidyadhar,

    Could you override GetAllPermissions methods at PermissionManager? You can add your permissions at here.

    here is my code public class CustomPermissionManager : PermissionManager { private readonly IRepository<ModulePermission, long> _modulePermissionRepository; private readonly bool _isMultiTenancyEnabled;

    public CustomPermissionManager(
        IIocManager iocManager,
        IAuthorizationConfiguration authorizationConfiguration,
        IUnitOfWorkManager unitOfWorkManager,
        IRepository&lt;ModulePermission, long&gt; modulePermissionRepository,
        IMultiTenancyConfig multiTenancy)
        : base(iocManager, authorizationConfiguration, unitOfWorkManager, multiTenancy)
    {
        _modulePermissionRepository = modulePermissionRepository;
        _isMultiTenancyEnabled = multiTenancy.IsEnabled;
    }
    
    public override IReadOnlyList&lt;Permission&gt; GetAllPermissions(bool tenancyFilter = true)
    {
        // Fetch default permissions from the base class
        var basePermissions = base.GetAllPermissions(tenancyFilter);
    
        // Add dynamic permissions from the database
        var dynamicPermissions = RegisterDynamicPermissions(basePermissions);
    
        // Combine base permissions with dynamic permissions
        return basePermissions.Concat(dynamicPermissions).ToList();
    }
    
    private List&lt;Permission&gt; RegisterDynamicPermissions(IReadOnlyList&lt;Permission&gt; rootPermissions)
    {
        // Fetch all dynamic permissions from the database
        var modulePermissions = _modulePermissionRepository.GetAll().IgnoreQueryFilters().ToList();
    
        var addedPermissions = new List&lt;Permission&gt;();
    
        // Loop through top-level permissions (ParentName is null or empty)
        foreach (var modulePermission in modulePermissions.Where(mp => string.IsNullOrEmpty(mp.ParentName)))
        {
            // Create a top-level permission
            var parentPermission = new Permission(
                modulePermission.Name,
                new FixedLocalizableString(modulePermission.DisplayName ?? modulePermission.Name),
                modulePermission.Description != null ? new FixedLocalizableString(modulePermission.Description) : null,
                _isMultiTenancyEnabled ? MultiTenancySides.Host | MultiTenancySides.Tenant : MultiTenancySides.Tenant
            );
    
            addedPermissions.Add(parentPermission);
    
            // Add child permissions recursively
            AddChildPermissions(parentPermission, modulePermissions, modulePermission.Name);
        }
    
        return addedPermissions;
    }
    
    private void AddChildPermissions(Permission parentPermission, List&lt;ModulePermission&gt; modulePermissions, string parentName)
    {
        // Fetch child permissions of the given parentName
        var childPermissions = modulePermissions.Where(mp => mp.ParentName == parentName).ToList();
    
        foreach (var childPermission in childPermissions)
        {
            // Create the child permission
            var createdChild = parentPermission.CreateChildPermission(
                childPermission.Name,
                new FixedLocalizableString(childPermission.DisplayName ?? childPermission.Name),
                childPermission.Description != null ? new FixedLocalizableString(childPermission.Description) : null,
                _isMultiTenancyEnabled ? MultiTenancySides.Host | MultiTenancySides.Tenant : MultiTenancySides.Tenant
            );
    
            // Recursively add children of the current child
            AddChildPermissions(createdChild, modulePermissions, childPermission.Name);
        }
    }
    

    } and [DependsOn( typeof(DemoApplicationSharedModule), typeof(DemoCoreModule) )] public class DemoApplicationModule : AbpModule {

    public override void PreInitialize()
    {
        //Adding authorization providers
        Configuration.Authorization.Providers.Add&lt;AppAuthorizationProvider&gt;();
    
        //Adding custom AutoMapper configuration
        Configuration.Modules.AbpAutoMapper().Configurators.Add(CustomDtoMapper.CreateMappings);
    
        Configuration.ReplaceService&lt;IRepository&lt;ModulePermission, long&gt;, ModulePermissionRepository>();
    
        Configuration.ReplaceService&lt;PermissionManager, CustomPermissionManager&gt;(DependencyLifeStyle.Singleton);
    }
    
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(typeof(DemoApplicationModule).GetAssembly());
      
    }
    

    } but application run then get only administration section under not display User ,role and other

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @[email protected],

    We are planning to create a blog post about dynamic permissions. Please stay tuned.