Browse Source

Remove all user related thing when deleting a user.

pull/18051/head
maliming 3 years ago
parent
commit
605054835d
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityLinkUserInfo.cs
  2. 21
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs
  3. 79
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

2
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityLinkUserInfo.cs

@ -8,7 +8,7 @@ public class IdentityLinkUserInfo
public virtual Guid? TenantId { get; set; } public virtual Guid? TenantId { get; set; }
public IdentityLinkUserInfo(Guid userId, Guid? tenantId) public IdentityLinkUserInfo(Guid userId, Guid? tenantId = null)
{ {
UserId = userId; UserId = userId;
TenantId = tenantId; TenantId = tenantId;

21
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

@ -11,10 +11,10 @@ using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services; using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Identity.Settings; using Volo.Abp.Identity.Settings;
using Volo.Abp.Settings; using Volo.Abp.Settings;
using Volo.Abp.Threading; using Volo.Abp.Threading;
using Volo.Abp.Timing;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace Volo.Abp.Identity; namespace Volo.Abp.Identity;
@ -27,7 +27,7 @@ public class IdentityUserManager : UserManager<IdentityUser>, IDomainService
protected ISettingProvider SettingProvider { get; } protected ISettingProvider SettingProvider { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; }
protected IDistributedEventBus DistributedEventBus { get; } protected IDistributedEventBus DistributedEventBus { get; }
protected IIdentityLinkUserRepository IdentityLinkUserRepository { get; }
protected override CancellationToken CancellationToken => CancellationTokenProvider.Token; protected override CancellationToken CancellationToken => CancellationTokenProvider.Token;
public IdentityUserManager( public IdentityUserManager(
@ -45,7 +45,8 @@ public class IdentityUserManager : UserManager<IdentityUser>, IDomainService
ICancellationTokenProvider cancellationTokenProvider, ICancellationTokenProvider cancellationTokenProvider,
IOrganizationUnitRepository organizationUnitRepository, IOrganizationUnitRepository organizationUnitRepository,
ISettingProvider settingProvider, ISettingProvider settingProvider,
IDistributedEventBus distributedEventBus) IDistributedEventBus distributedEventBus,
IIdentityLinkUserRepository identityLinkUserRepository)
: base( : base(
store, store,
optionsAccessor, optionsAccessor,
@ -62,6 +63,7 @@ public class IdentityUserManager : UserManager<IdentityUser>, IDomainService
DistributedEventBus = distributedEventBus; DistributedEventBus = distributedEventBus;
RoleRepository = roleRepository; RoleRepository = roleRepository;
UserRepository = userRepository; UserRepository = userRepository;
IdentityLinkUserRepository = identityLinkUserRepository;
CancellationTokenProvider = cancellationTokenProvider; CancellationTokenProvider = cancellationTokenProvider;
} }
@ -76,6 +78,19 @@ public class IdentityUserManager : UserManager<IdentityUser>, IDomainService
return await CreateAsync(user); return await CreateAsync(user);
} }
public async override Task<IdentityResult> 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<IdentityUser> GetByIdAsync(Guid id) public virtual async Task<IdentityUser> GetByIdAsync(Guid id)
{ {
var user = await Store.FindByIdAsync(id.ToString(), CancellationToken); var user = await Store.FindByIdAsync(id.ToString(), CancellationToken);

79
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -26,8 +27,10 @@ public class IdentityUserManager_Tests : AbpIdentityDomainTestBase
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IdentityTestData _testData; private readonly IdentityTestData _testData;
private readonly ICurrentTenant _currentTenant; private readonly ICurrentTenant _currentTenant;
protected IOptions<IdentityOptions> _identityOptions { get; } private readonly IOptions<IdentityOptions> _identityOptions;
protected IDistributedEventBus DistributedEventBus { get; set; } private readonly IdentityLinkUserManager _identityLinkUserManager;
private IDistributedEventBus _distributedEventBus { get; set; }
public IdentityUserManager_Tests() public IdentityUserManager_Tests()
{ {
@ -40,12 +43,13 @@ public class IdentityUserManager_Tests : AbpIdentityDomainTestBase
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); _unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_currentTenant = GetRequiredService<ICurrentTenant>(); _currentTenant = GetRequiredService<ICurrentTenant>();
_identityOptions = GetRequiredService<IOptions<IdentityOptions>>(); _identityOptions = GetRequiredService<IOptions<IdentityOptions>>();
_identityLinkUserManager = GetRequiredService<IdentityLinkUserManager>();
} }
protected override void AfterAddApplication(IServiceCollection services) protected override void AfterAddApplication(IServiceCollection services)
{ {
DistributedEventBus = Substitute.For<IDistributedEventBus>(); _distributedEventBus = Substitute.For<IDistributedEventBus>();
services.Replace(ServiceDescriptor.Singleton(DistributedEventBus)); services.Replace(ServiceDescriptor.Singleton(_distributedEventBus));
} }
[Fact] [Fact]
@ -270,7 +274,7 @@ public class IdentityUserManager_Tests : AbpIdentityDomainTestBase
(await _identityUserManager.CreateAsync(user)).CheckErrors(); (await _identityUserManager.CreateAsync(user)).CheckErrors();
await DistributedEventBus.DidNotReceive() await _distributedEventBus.DidNotReceive()
.PublishAsync(Arg.Any<IdentityUserUserNameChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>()); .PublishAsync(Arg.Any<IdentityUserUserNameChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>());
var newUser = await _identityUserManager.FindByIdAsync(user.Id.ToString()); var newUser = await _identityUserManager.FindByIdAsync(user.Id.ToString());
@ -280,16 +284,16 @@ public class IdentityUserManager_Tests : AbpIdentityDomainTestBase
{ {
var oldUsername = newUser.UserName; var oldUsername = newUser.UserName;
await _identityUserManager.SetUserNameAsync(newUser, "newUserName"); await _identityUserManager.SetUserNameAsync(newUser, "newUserName");
await DistributedEventBus.Received() await _distributedEventBus.Received()
.PublishAsync( .PublishAsync(
Arg.Is<IdentityUserUserNameChangedEto>(x => Arg.Is<IdentityUserUserNameChangedEto>(x =>
x.Id == newUser.Id && x.TenantId == newUser.TenantId && x.OldUserName == oldUsername && x.UserName == "newUserName"), x.Id == newUser.Id && x.TenantId == newUser.TenantId && x.OldUserName == oldUsername && x.UserName == "newUserName"),
Arg.Any<bool>(), Arg.Any<bool>()); Arg.Any<bool>(), Arg.Any<bool>());
} }
DistributedEventBus.ClearReceivedCalls(); _distributedEventBus.ClearReceivedCalls();
await _identityUserManager.SetUserNameAsync(newUser, newUser.UserName); await _identityUserManager.SetUserNameAsync(newUser, newUser.UserName);
await DistributedEventBus.DidNotReceive() await _distributedEventBus.DidNotReceive()
.PublishAsync(Arg.Any<IdentityUserUserNameChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>()); .PublishAsync(Arg.Any<IdentityUserUserNameChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>());
} }
@ -300,7 +304,7 @@ public class IdentityUserManager_Tests : AbpIdentityDomainTestBase
(await _identityUserManager.CreateAsync(user)).CheckErrors(); (await _identityUserManager.CreateAsync(user)).CheckErrors();
await DistributedEventBus.DidNotReceive() await _distributedEventBus.DidNotReceive()
.PublishAsync(Arg.Any<IdentityUserEmailChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>()); .PublishAsync(Arg.Any<IdentityUserEmailChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>());
var newUser = await _identityUserManager.FindByIdAsync(user.Id.ToString()); var newUser = await _identityUserManager.FindByIdAsync(user.Id.ToString());
@ -310,19 +314,70 @@ public class IdentityUserManager_Tests : AbpIdentityDomainTestBase
{ {
var oldEmail = newUser.Email; var oldEmail = newUser.Email;
await _identityUserManager.SetEmailAsync(newUser, "newEmail@abp.io"); await _identityUserManager.SetEmailAsync(newUser, "newEmail@abp.io");
await DistributedEventBus.Received() await _distributedEventBus.Received()
.PublishAsync( .PublishAsync(
Arg.Is<IdentityUserEmailChangedEto>(x => Arg.Is<IdentityUserEmailChangedEto>(x =>
x.Id == newUser.Id && x.TenantId == newUser.TenantId && x.OldEmail == oldEmail && x.Email == "newEmail@abp.io"), x.Id == newUser.Id && x.TenantId == newUser.TenantId && x.OldEmail == oldEmail && x.Email == "newEmail@abp.io"),
Arg.Any<bool>(), Arg.Any<bool>()); Arg.Any<bool>(), Arg.Any<bool>());
} }
DistributedEventBus.ClearReceivedCalls(); _distributedEventBus.ClearReceivedCalls();
await _identityUserManager.SetEmailAsync(newUser, newUser.Email); await _identityUserManager.SetEmailAsync(newUser, newUser.Email);
await DistributedEventBus.DidNotReceive() await _distributedEventBus.DidNotReceive()
.PublishAsync(Arg.Any<IdentityUserEmailChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>()); .PublishAsync(Arg.Any<IdentityUserEmailChangedEto>(), Arg.Any<bool>(), Arg.Any<bool>());
} }
[Fact]
public async Task DeleteAsync()
{
await CreateRandomDefaultRoleAsync();
var user = CreateRandomUser();
(await _identityUserManager.CreateAsync(user)).CheckErrors();
var user2 = CreateRandomUser();
(await _identityUserManager.CreateAsync(user2)).CheckErrors();
using (var uow = _unitOfWorkManager.Begin())
{
user = await _identityUserManager.FindByIdAsync(user.Id.ToString());
user.ShouldNotBeNull();
await _identityUserManager.AddClaimAsync(user, new Claim("test", "test"));
await _identityUserManager.AddLoginAsync(user, new UserLoginInfo("test", "test", "test"));
await _identityUserManager.AddDefaultRolesAsync(user);
user.SetToken("test", "test", "test");
var ou = await _organizationUnitRepository.GetAsync(_lookupNormalizer.NormalizeName("OU11"));
await _identityUserManager.AddToOrganizationUnitAsync(user, ou);
await _identityLinkUserManager.LinkAsync(new IdentityLinkUserInfo(user.Id), new IdentityLinkUserInfo(user2.Id));
await uow.CompleteAsync();
}
using (var uow = _unitOfWorkManager.Begin())
{
user = await _identityUserManager.FindByIdAsync(user.Id.ToString());
user.ShouldNotBeNull();
user.Claims.Count.ShouldBeGreaterThan(0);
user.Logins.Count.ShouldBeGreaterThan(0);
user.Roles.Count.ShouldBeGreaterThan(0);
user.Tokens.Count.ShouldBeGreaterThan(0);
user.OrganizationUnits.Count.ShouldBeGreaterThan(0);
(await _identityLinkUserManager.IsLinkedAsync(new IdentityLinkUserInfo(user.Id), new IdentityLinkUserInfo(user2.Id))).ShouldBeTrue();
await _identityUserManager.DeleteAsync(user);
user.Claims.Count.ShouldBe(0);
user.Logins.Count.ShouldBe(0);
user.Roles.Count.ShouldBe(0);
user.Tokens.Count.ShouldBe(0);
user.OrganizationUnits.Count.ShouldBe(0);
(await _identityLinkUserManager.IsLinkedAsync(new IdentityLinkUserInfo(user.Id), new IdentityLinkUserInfo(user2.Id))).ShouldBeFalse();
await uow.CompleteAsync();
}
}
private async Task CreateRandomDefaultRoleAsync() private async Task CreateRandomDefaultRoleAsync()
{ {
await _identityRoleRepository.InsertAsync( await _identityRoleRepository.InsertAsync(

Loading…
Cancel
Save