From d25774687d1cf87140efa6a2cb84422a86c100c9 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 27 Nov 2023 15:08:15 +0800 Subject: [PATCH] Remove user-related data when deleting a user --- .../Volo/Abp/Identity/IdentityUserManager.cs | 18 +++++++++++++++++- .../Identity/IdentityRoleRepository_Tests.cs | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index cdbb289597..af36c9fd7a 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -25,6 +25,7 @@ public class IdentityUserManager : UserManager, IDomainService protected IOrganizationUnitRepository OrganizationUnitRepository { get; } protected ISettingProvider SettingProvider { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; } + protected IIdentityLinkUserRepository IdentityLinkUserRepository { get; } protected override CancellationToken CancellationToken => CancellationTokenProvider.Token; @@ -42,7 +43,8 @@ public class IdentityUserManager : UserManager, IDomainService ILogger logger, ICancellationTokenProvider cancellationTokenProvider, IOrganizationUnitRepository organizationUnitRepository, - ISettingProvider settingProvider) + ISettingProvider settingProvider, + IIdentityLinkUserRepository identityLinkUserRepository) : base( store, optionsAccessor, @@ -58,6 +60,7 @@ public class IdentityUserManager : UserManager, IDomainService SettingProvider = settingProvider; RoleRepository = roleRepository; UserRepository = userRepository; + IdentityLinkUserRepository = identityLinkUserRepository; CancellationTokenProvider = cancellationTokenProvider; } @@ -71,6 +74,19 @@ public class IdentityUserManager : UserManager, IDomainService return await CreateAsync(user); } + + public async override Task DeleteAsync(IdentityUser user) + { + user.Claims.Clear(); + user.Roles.Clear(); + user.Tokens.Clear(); + user.Logins.Clear(); + user.OrganizationUnits.Clear(); + await IdentityLinkUserRepository.DeleteAsync(new IdentityLinkUserInfo(user.Id, user.TenantId), CancellationToken); + await UpdateAsync(user); + + return await base.DeleteAsync(user); + } public virtual async Task GetByIdAsync(Guid id) { diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs index c34e9ad56c..c01bf706e3 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.Modularity; +using Volo.Abp.Uow; using Xunit; namespace Volo.Abp.Identity; @@ -13,11 +14,17 @@ public abstract class IdentityRoleRepository_Tests : AbpIdentity { protected IIdentityRoleRepository RoleRepository { get; } protected ILookupNormalizer LookupNormalizer { get; } + protected IdentityUserManager UserManager { get; } + protected IdentityTestData TestData { get; } + protected IUnitOfWorkManager UnitOfWorkManager { get; } protected IdentityRoleRepository_Tests() { RoleRepository = ServiceProvider.GetRequiredService(); LookupNormalizer = ServiceProvider.GetRequiredService(); + UserManager = ServiceProvider.GetRequiredService(); + TestData = ServiceProvider.GetRequiredService(); + UnitOfWorkManager = ServiceProvider.GetRequiredService(); } [Fact] @@ -77,5 +84,17 @@ public abstract class IdentityRoleRepository_Tests : AbpIdentity roles.ShouldContain(r => r.Role.Name == "moderator" && r.UserCount == 1); roles.ShouldContain(r => r.Role.Name == "supporter" && r.UserCount == 2); roles.ShouldContain(r => r.Role.Name == "manager" && r.UserCount == 1); + + using (var uow = UnitOfWorkManager.Begin()) + { + var userBob = await UserManager.FindByIdAsync(TestData.UserBobId.ToString()); + await UserManager.DeleteAsync(userBob!); + await uow.CompleteAsync(); + } + + roles = await RoleRepository.GetListWithUserCountAsync(); + + roles.Count.ShouldBe(4); + roles.ShouldContain(r => r.Role.Name == "manager" && r.UserCount == 0); } }