Open Closed

Issues Replacing PermissionDataSeedContributor #6448


User avatar
0
pablo@ccalp.net created
  • ABP Framework version: v7.4.0
  • UI Type: Angular
  • Database System: EF Core (SQL Serve)

I've followed some documents online and looked at the source code for how to replace the PermissionDataSeedContributor but when running the DbMigrator I end up with twice the permissions, one set for the default "admin" role and another set for my custom role.

Here's my code:

using CompuCare.Enums; using CompuCare.Permissions; using Microsoft.AspNetCore.Identity; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; using Volo.Abp.Authorization.Permissions; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; using Volo.Abp.PermissionManagement; using Volo.Abp.Uow;

namespace CompuCare.DataSeed;

[Dependency(ReplaceServices = true)] [ExposeServices(typeof(PermissionDataSeedContributor), typeof(IDataSeedContributor))] public class PermissionDataSeedContributor : IDataSeedContributor, ITransientDependency {

private readonly ICurrentTenant _currentTenant; private readonly IPermissionDefinitionManager _permissionDefinitionManager; private readonly IPermissionDataSeeder _permissionDataSeeder;

public PermissionDataSeedContributor( ICurrentTenant currentTenant, IPermissionDefinitionManager permissionDefinitionManager, IPermissionDataSeeder permissionDataSeeder) { _currentTenant = currentTenant; _permissionDefinitionManager = permissionDefinitionManager; _permissionDataSeeder = permissionDataSeeder; }

public async Task SeedAsync(DataSeedContext context) { await PopulateAbpPermissionsAsync(context); }

private async Task PopulateAbpPermissionsAsync(DataSeedContext context) { var multiTenancySide = _currentTenant.GetMultiTenancySide(); var providerKey = multiTenancySide == MultiTenancySides.Host ? "admin" : "Administrators"; var permissionNames = (await _permissionDefinitionManager.GetPermissionsAsync()) .Where(p => p.MultiTenancySide.HasFlag(multiTenancySide)) .Where(p => !p.Providers.Any() || p.Providers.Contains(RolePermissionValueProvider.ProviderName)) .Select(p => p.Name) .ToArray();

await _permissionDataSeeder.SeedAsync( RolePermissionValueProvider.ProviderName, providerKey, permissionNames, context?.TenantId ); }

}


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

    hi

    You can remove the PermissionDataSeedContributor from AbpDataSeedOptions

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        PostConfigure<AbpDataSeedOptions>(options =>
        {
            options.Contributors.RemoveAll(x => x == typeof(PermissionDataSeedContributor));
        });
    }
    
    
    public class YourPermissionDataSeedContributor : IDataSeedContributor, ITransientDependency
    
  • User Avatar
    0
    pablo@ccalp.net created

    Thank you, that worked. Is that the recommended way of doing it? I thought that replacing the service would have been the right approach but for some reason, it looks like it was calling my custom class SeedAsync and the framework's SeedAsync method too.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You should inherit the PermissionDataSeedContributor

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(YourPermissionDataSeedContributor), typeof(PermissionDataSeedContributor), typeof(IDataSeedContributor))]
    
    public class YourPermissionDataSeedContributor : PermissionDataSeedContributor
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11