Open Closed

Seed Roles and Permissions #4663


User avatar
0
augustin.couval@manuloc.eu created

Hello, Our project is an application that is composed of two back. (see screenshot)

we want that when launching a database migration, 3 roles are created and that the permissions (already defined) are set for the created roles. Permissions are defined in the folders:

ConfigurateurClient.Admin.Application.Contracts/Permissions and

ConfigurateurClient.Application.Contracts/Permissions

We have created a DataSeeding in the application layer

namespace ConfigurateurClient.Admin.DataSeeding
{
    public class DataSeedingRoleAppService : AdminAppService, IDataSeedingRoleAppService

    {
        private readonly IRepository _userRepository;
        private readonly IRepository _roleRepository;
        private readonly IIdentityRoleAppService _identityAppService;
        private readonly IPermissionAppService _permissionAppService;
        private readonly IPermissionManager _permissionManager;

        public DataSeedingRoleAppService(IRepository userRepository, IRepository roleRepository, IIdentityRoleAppService identityAppService, IPermissionAppService permissionAppService, IPermissionManager permissionManager)
        {
            _userRepository = userRepository;
            _roleRepository = roleRepository;
            _identityAppService = identityAppService;
            _permissionAppService = permissionAppService;
            _permissionManager = permissionManager;
        }

        public async Task PostCreateDefaultRoles()
        {
            var roles = await _roleRepository.GetListAsync();

            await CreateRoleAdmin(roles);
            await CreateRoleClient(roles);
            await CreateRoleManuloc(roles);
        }

        private async Task CreateRoleAdmin(List listRoles)
        {
            var isRoleAdminExist = listRoles.Where(r => r.Name == "admin").ToList();

            IdentityRoleDto roleAdmin = new();
            if(isRoleAdminExist.Count == 0)
            {
                roleAdmin = await _identityAppService.CreateAsync(new IdentityRoleCreateDto
                {
                    Name = "admin",
                    IsDefault = false,
                    IsPublic = true
                });
            }
        }

        private async Task CreateRoleClient(List listRoles)
        {
            var isRoleClientExist = listRoles.Where(r => r.Name == "client").ToList();

            if (isRoleClientExist.Count == 0)
            {
                await _identityAppService.CreateAsync(new IdentityRoleCreateDto
                {
                    Name = "client",
                    IsDefault = false,
                    IsPublic = true
                });

            }
        }

        private async Task CreateRoleManuloc(List listRoles)
        {
            var isRoleManuloxExist = listRoles.Where(r => r.Name == "manuloc").ToList();
            IdentityRoleDto roleManuloc = new();
            if (isRoleManuloxExist.Count == 0)
            {
               roleManuloc = await _identityAppService.CreateAsync(new IdentityRoleCreateDto
                {
                    Name = "manuloc",
                    IsDefault = false,
                    IsPublic = true
                });

              

            }
            await SetPermissionToRoleManuloc(roleManuloc);
        }

        private async Task SetPermissionToRoleManuloc(IdentityRoleDto roleManuloc)
        {

            UpdatePermissionsDto Permissions = new();
            List permission = new List();

            permission.Add(new UpdatePermissionDto { Name = "Admin.Client",IsGranted = true});
            permission.Add(new UpdatePermissionDto { Name = "Admin.Configuration", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.Contact", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.Option", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.GroupeOption", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.Modeles", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.Ulisateur", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.Caracteristique", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.TauxLocation", IsGranted = true });
            permission.Add(new UpdatePermissionDto { Name = "Admin.Ulisateur", IsGranted = true });

            Permissions.Permissions = permission.ToArray();

          
            await _permissionAppService.UpdateAsync("R", "manuloc", Permissions);
        }
    }
}

But, we want that when launching a database migration.

We tried to use the IDataSeedContributor, but some Repositories are not accessible.

 public class DataSeedingRoleAppService : IDataSeedContributor, ITransientDependency
    {
        
        private readonly IIdentityRoleAppService _identityAppService;
        private readonly IPermissionAppService _permissionAppService;

        public DataSeedingRoleAppService(IIdentityRoleAppService identityAppService, IPermissionAppService permissionAppService)
        {
            _identityAppService = identityAppService;
            _permissionAppService = permissionAppService;
        }
    }
    

then I launched a database migration and got this error :

Exception message and stack trace: [16:00:51 INF] Executing host database seed... Unhandled exception. Autofac.Core.DependencyResolutionException: An exception was thrown while activating ConfigurateurClient.Admin.DataSeeding.DataSeedingRoleAppService. ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ConfigurateurClient.Admin.DataSeeding.DataSeedingRoleAppService' can be invoked with the available services and parameters: Cannot resolve parameter 'Volo.Abp.Identity.IIdentityRoleAppService identityAppService' of constructor 'Void .ctor(Volo.Abp.Identity.IIdentityRoleAppService, Volo.Abp.PermissionManagement.IPermissionAppService)'. at Autofac.Core.Activators.Reflection.ReflectionActivator.<>c__DisplayClass12_0.b__0(ResolveRequestContext ctxt, Action1 next) at Autofac.Core.Resolving.Middleware.DelegateMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.b__1(ResolveRequestContext ctxt) at Autofac.Core.Resolving.Middleware.DisposalTrackingMiddleware.Execute(ResolveRequestContext context, Action1 next) at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.

(String[] args)

ABP Framework version: v7.0.0 UI type: Angular and Blazor (not relevant here)

DB provider: EF Core

Tiered (MVC) or Identity Server Separated (Angular): no


3 Answer(s)
  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    It can not find the implementation of IDataSeedingRoleAppService because it is under the application layer.

    I couldn't understand why did you create an application service for data migration. You can create dataseed contributors and instead of an application service, you can create a DataSeeder (implementing IDataSeeder) and seed it.

    See https://docs.abp.io/en/abp/latest/Data-Seeding#idataseeder for more information.

  • User Avatar
    0
    berly created

    Our permissions are defined in both project application.contract.

    Is it possible to provide us with an example to create a role and assign certain permissions to it?

  • User Avatar
    -3
    maliming created
    Support Team Fullstack Developer

    hi

    Do not inject and use application services in IDataSeedContributor

    You can use IPermissionDataSeeder just like PermissionDataSeedContributor

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19