diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs index 825266e993..dac173719a 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRole.cs @@ -100,6 +100,22 @@ namespace Volo.Abp.Identity Claims.RemoveAll(c => c.ClaimType == claim.Type && c.ClaimValue == claim.Value); } + public virtual void ChangeName(string name) + { + Check.NotNullOrWhiteSpace(name, nameof(name)); + + var oldName = Name; + Name = name; + + AddLocalEvent( + new IdentityRoleNameChangedEvent + { + IdentityRole = this, + OldName = oldName + } + ); + } + public override string ToString() { return $"{base.ToString()}, Name = {Name}"; diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleNameChangedEvent.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleNameChangedEvent.cs new file mode 100644 index 0000000000..f14f0a71ed --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleNameChangedEvent.cs @@ -0,0 +1,8 @@ +namespace Volo.Abp.Identity +{ + public class IdentityRoleNameChangedEvent + { + public IdentityRole IdentityRole { get; set; } + public string OldName { get; set; } + } +} diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleStore.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleStore.cs index 1ca46b0830..004fc0c20c 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleStore.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityRoleStore.cs @@ -165,7 +165,7 @@ namespace Volo.Abp.Identity Check.NotNull(role, nameof(role)); - role.Name = roleName; + role.ChangeName(roleName); return Task.CompletedTask; } diff --git a/modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo.Abp.PermissionManagement.Domain.Identity.csproj b/modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo.Abp.PermissionManagement.Domain.Identity.csproj index ea663ba49b..ee8e336088 100644 --- a/modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo.Abp.PermissionManagement.Domain.Identity.csproj +++ b/modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo.Abp.PermissionManagement.Domain.Identity.csproj @@ -16,6 +16,7 @@ + diff --git a/modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleUpdateEventHandler.cs b/modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleUpdateEventHandler.cs new file mode 100644 index 0000000000..8ee0d4c6d6 --- /dev/null +++ b/modules/identity/src/Volo.Abp.PermissionManagement.Domain.Identity/Volo/Abp/PermissionManagement/Identity/RoleUpdateEventHandler.cs @@ -0,0 +1,42 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events; +using Volo.Abp.EventBus; +using Volo.Abp.Identity; + +namespace Volo.Abp.PermissionManagement.Identity +{ + public class RoleUpdateEventHandler : + ILocalEventHandler, + ITransientDependency + { + protected IIdentityRoleRepository RoleRepository { get; } + protected IPermissionManager PermissionManager { get; } + protected IPermissionGrantRepository PermissionGrantRepository { get; } + + public RoleUpdateEventHandler( + IIdentityRoleRepository roleRepository, + IPermissionManager permissionManager, + IPermissionGrantRepository permissionGrantRepository) + { + RoleRepository = roleRepository; + PermissionManager = permissionManager; + PermissionGrantRepository = permissionGrantRepository; + } + + public async Task HandleEventAsync(IdentityRoleNameChangedEvent eventData) + { + var role = await RoleRepository.FindAsync(eventData.IdentityRole.Id, false).ConfigureAwait(false); + if (role == null) + { + return; + } + + var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", eventData.OldName).ConfigureAwait(false); + foreach (var permissionGrant in permissionGrantsInRole) + { + await PermissionManager.UpdateProviderKeyAsync(permissionGrant, eventData.IdentityRole.Name).ConfigureAwait(false); + } + } + } +} diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/RoleChangingEvents_Test.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/RoleChangingEvents_Test.cs new file mode 100644 index 0000000000..1a4405c9e4 --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/RoleChangingEvents_Test.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Identity; +using Shouldly; +using System.Threading.Tasks; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Guids; +using Volo.Abp.PermissionManagement; +using Volo.Abp.Uow; +using Xunit; + +namespace Volo.Abp.Identity +{ + public class RoleChangingEvents_Test : AbpIdentityDomainTestBase + { + protected readonly IIdentityRoleRepository RoleRepository; + protected readonly IPermissionGrantRepository PermissionGrantRepository; + protected readonly IdentityRoleManager RoleManager; + protected readonly ILookupNormalizer LookupNormalizer; + protected readonly IGuidGenerator GuidGenerator; + protected readonly IUnitOfWorkManager UowManager; + + public RoleChangingEvents_Test() + { + RoleRepository = GetRequiredService(); ; + PermissionGrantRepository = GetRequiredService(); ; + RoleManager = GetRequiredService(); ; + LookupNormalizer = GetRequiredService(); ; + GuidGenerator = GetRequiredService(); + UowManager = GetRequiredService(); + } + + [Fact] + public async Task Role_Update_Event_Test() + { + var role = await RoleRepository + .FindByNormalizedNameAsync(LookupNormalizer.NormalizeName("moderator")) + .ConfigureAwait(false); + + var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name).ConfigureAwait(false); + permissionGrantsInRole.ShouldNotBeNull(); + permissionGrantsInRole.Count.ShouldBeGreaterThan(0); + var count = permissionGrantsInRole.Count; + + using (var uow = UowManager.Begin()) + { + var identityResult = await RoleManager.SetRoleNameAsync(role, "TestModerator").ConfigureAwait(false); + identityResult.Succeeded.ShouldBeTrue(); + var xx = await RoleRepository.UpdateAsync(role).ConfigureAwait(false); + await uow.CompleteAsync().ConfigureAwait(false); + } + + role = await RoleRepository.GetAsync(role.Id).ConfigureAwait(false); + role.Name.ShouldBe("TestModerator"); + + permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name).ConfigureAwait(false); + permissionGrantsInRole.Count.ShouldBe(count); + } + } +}