Open Closed

How to remove/disable validation Duplicate Name in Role #6116


User avatar
0
fernando.guilherme created
  • ABP Framework version: v7.2.3
  • UI Type: Angular
  • Database System: EF Core (SQL Serve)
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

Hi,

I downloaded the Identity Pro module. I created an IsActive extaProperties for the IdentityRole entity.

I need to allow duplicate profiles to be registered as long as one of the profiles is deactivated.

However, in the IdentityRoleAppService class, when I create a new profile, it executes this method (await RoleManager.CreateAsync(role)).CheckErrors();

In this CheckErrors() it is validating duplicate names.

How do I disable this validation.

Already tried:

[ExposeServices(typeof(IdentityRoleAppService), typeof(IIdentityRoleAppService))] [DisableValidation] [Authorize(IdentityPermissions.Roles.Default)] public class IdentityRoleAppService : IdentityAppServiceBase, IIdentityRoleAppService

[DisableValidation] [Authorize(IdentityPermissions.Roles.Create)] public virtual async Task<IdentityRoleDto> CreateAsync(IdentityRoleCreateDto input)

In this case it doesn't look like validation, but it also doesn't insert the record //(await RoleManager.CreateAsync(role)).CheckErrors(); await RoleManager.CreateAsync(role);

can you help me?


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

    hi

    There is RoleValidator service that you can override or remove.

    https://github.com/dotnet/aspnetcore/blob/release/7.0/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs#L85 https://github.com/dotnet/aspnetcore/blob/release/7.0/src/Identity/Extensions.Core/src/RoleValidator.cs#L34

  • User Avatar
    0
    fernando.guilherme created

    hi

    There is RoleValidator service that you can override or remove.

    https://github.com/dotnet/aspnetcore/blob/release/7.0/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs#L85 https://github.com/dotnet/aspnetcore/blob/release/7.0/src/Identity/Extensions.Core/src/RoleValidator.cs#L34

    Hi,

    But where would I put the RoleValidor to replace within this identityPro structure?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can put it in Domain module.

  • User Avatar
    0
    fernando.guilherme created

    Hey,

    With reference to the image of the structure above, in which domain module?

    It's not clear where to put it and how, regarding these two links you gave me.

    Could you be more detailed?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    sorry, You can add the class to Identity Domain or FapRat Domain.

  • User Avatar
    0
    fernando.guilherme created

    hi

    sorry, You can add the class to Identity Domain or FapRat Domain.

    Hey,

    I know there are two DomainModules, my question was which one do I have to put it in, what and how?

    You gave me these two in the links:

    https://github.com/dotnet/aspnetcore/blob/release/7.0/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs#L85 https://github.com/dotnet/aspnetcore/blob/release/7.0/src/Identity/Extensions.Core/src/RoleValidator.cs#L34

    Where are these two classes:

    public class RoleValidator<TRole> : IRoleValidator<TRole> where TRole : class { ... }

    public static class IdentityServiceCollectionExtensions { ... }

    I need to know where I put it and how I put it inside the DomainModule so that I can override or disable the RoleValidator.

    Could you show me the code in practice?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    HI

    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Identity;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.Identity;
    
    namespace BookStore;
    
    public class MyRoleValidator : IRoleValidator<IdentityRole>
    {
    
        public MyRoleValidator(IdentityErrorDescriber? errors = null)
        {
            Describer = errors ?? new IdentityErrorDescriber();
        }
    
        private IdentityErrorDescriber Describer { get; set; }
    
    
        public virtual async Task<IdentityResult> ValidateAsync(RoleManager<IdentityRole> manager, IdentityRole role)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
    
            if (role == null)
            {
                throw new ArgumentNullException(nameof(role));
            }
    
            var errors = new List<IdentityError>();
            await ValidateRoleName(manager, role, errors).ConfigureAwait(false);
            if (errors.Count > 0)
            {
                return IdentityResult.Failed(errors.ToArray());
            }
            return IdentityResult.Success;
        }
    
        private async Task ValidateRoleName(RoleManager<IdentityRole> manager, IdentityRole role, ICollection<IdentityError> errors)
        {
            var roleName = await manager.GetRoleNameAsync(role).ConfigureAwait(false);
            if (string.IsNullOrWhiteSpace(roleName))
            {
                errors.Add(Describer.InvalidRoleName(roleName));
            }
            else
            {
                // Add your custom validation here
    
                // var owner = await manager.FindByNameAsync(roleName).ConfigureAwait(false);
                // if (owner != null &&
                //     !string.Equals(await manager.GetRoleIdAsync(owner).ConfigureAwait(false), await manager.GetRoleIdAsync(role).ConfigureAwait(false)))
                // {
                //     errors.Add(Describer.DuplicateRoleName(roleName));
                // }
            }
        }
    }
    
    
    public class BookStoreDomainModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.RemoveAll(x => x.ServiceType == typeof(IRoleValidator<IdentityRole>));
            context.Services.AddScoped<IRoleValidator<IdentityRole>, MyRoleValidator>();
            //....
        }
    }
    
  • User Avatar
    0
    fernando.guilherme created

    context.Services.RemoveAll(x => x.ServiceType == typeof(IRoleValidator<IdentityRole>)); context.Services.AddScoped<IRoleValidator<IdentityRole>, MyRoleValidator>();

    Thank you.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11