Add `UpdateRolesAsync` to `IIdentityUserRepository`.

pull/17171/head
maliming 2 years ago
parent 6039c28330
commit 5918032d2d
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

@ -131,4 +131,10 @@ public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid>
bool includeDetails = false,
CancellationToken cancellationToken = default
);
Task UpdateRolesAsync(
Guid sourceRoleId,
Guid? targetRoleId,
CancellationToken cancellationToken = default
);
}

@ -356,4 +356,18 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
.Where(x => ids.Contains(x.Id))
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task UpdateRolesAsync(Guid sourceRoleId, Guid? targetRoleId, CancellationToken cancellationToken = default)
{
if (targetRoleId != null)
{
var users = await (await GetDbContextAsync()).Set<IdentityUserRole>().Where(x => x.RoleId == targetRoleId).Select(x => x.UserId).ToArrayAsync(cancellationToken: cancellationToken);
await (await GetDbContextAsync()).Set<IdentityUserRole>().Where(x => x.RoleId == sourceRoleId && !users.Contains(x.UserId)).ExecuteUpdateAsync(t => t.SetProperty(e => e.RoleId, targetRoleId), GetCancellationToken(cancellationToken));
await (await GetDbContextAsync()).Set<IdentityUserRole>().Where(x => x.RoleId == sourceRoleId).ExecuteDeleteAsync(GetCancellationToken(cancellationToken));
}
else
{
await (await GetDbContextAsync()).Set<IdentityUserRole>().Where(x => x.RoleId == sourceRoleId).ExecuteDeleteAsync(GetCancellationToken(cancellationToken));
}
}
}

@ -324,4 +324,22 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
.Where(x => ids.Contains(x.Id))
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task UpdateRolesAsync(Guid sourceRoleId, Guid? targetRoleId, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
.Where(x => x.Roles.Any(r => r.RoleId == sourceRoleId))
.ToListAsync(GetCancellationToken(cancellationToken));
foreach (var user in users)
{
user.RemoveRole(sourceRoleId);
if (targetRoleId.HasValue)
{
user.AddRole(targetRoleId.Value);
}
}
await UpdateManyAsync(users, cancellationToken: cancellationToken);
}
}

@ -147,6 +147,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
var bob = new IdentityUser(_testData.UserBobId, "bob", "bob@abp.io");
bob.SetIsActive(false);
bob.AddRole(_managerRole.Id);
await _userManager.CreateAsync(bob, "1q2w3E*");
}
@ -205,24 +206,24 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
private async Task AddUserDelegations()
{
await _identityUserDelegationRepository.InsertAsync(
new IdentityUserDelegation(_guidGenerator.Create(),
new IdentityUserDelegation(_guidGenerator.Create(),
_testData.UserJohnId,
_testData.UserDavidId,
DateTime.Now.AddDays(-2),
_testData.UserDavidId,
DateTime.Now.AddDays(-2),
DateTime.Now.AddDays(-1)));
await _identityUserDelegationRepository.InsertAsync(
new IdentityUserDelegation(_guidGenerator.Create(),
new IdentityUserDelegation(_guidGenerator.Create(),
_testData.UserJohnId,
_testData.UserDavidId,
DateTime.Now.AddDays(-1),
_testData.UserDavidId,
DateTime.Now.AddDays(-1),
DateTime.Now.AddDays(1)));
await _identityUserDelegationRepository.InsertAsync(
new IdentityUserDelegation(_guidGenerator.Create(),
new IdentityUserDelegation(_guidGenerator.Create(),
_testData.UserNeoId,
_testData.UserDavidId,
DateTime.Now.AddDays(-1),
_testData.UserDavidId,
DateTime.Now.AddDays(-1),
DateTime.Now.AddDays(1)));
}
}

@ -16,12 +16,14 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : AbpIdentity
protected IIdentityUserRepository UserRepository { get; }
protected ILookupNormalizer LookupNormalizer { get; }
protected IOrganizationUnitRepository OrganizationUnitRepository { get; }
protected IdentityTestData TestData { get; }
protected IdentityUserRepository_Tests()
{
UserRepository = ServiceProvider.GetRequiredService<IIdentityUserRepository>();
LookupNormalizer = ServiceProvider.GetRequiredService<ILookupNormalizer>();
OrganizationUnitRepository = ServiceProvider.GetRequiredService<IOrganizationUnitRepository>();
TestData = ServiceProvider.GetRequiredService<IdentityTestData>();
}
[Fact]
@ -175,4 +177,36 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : AbpIdentity
organizationUnit.ShouldNotBeNull();
return organizationUnit;
}
[Fact]
public async Task UpdateRolesAsync()
{
var john = await UserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName("john.nash"));
var roles = await UserRepository.GetRolesAsync(john.Id);
roles.Count.ShouldBe(3);
roles.ShouldContain(r => r.Name == "moderator");
roles.ShouldContain(r => r.Name == "supporter");
roles.ShouldContain(r => r.Name == "manager");
var supporter = roles.First(x => x.NormalizedName == LookupNormalizer.NormalizeName("supporter"));
var manager = roles.First(x => x.NormalizedName == LookupNormalizer.NormalizeName("manager"));
await UserRepository.UpdateRolesAsync(supporter.Id, null);
roles = await UserRepository.GetRolesAsync(john.Id);
roles.Count.ShouldBe(2);
roles.ShouldContain(r => r.Name == "moderator");
roles.ShouldContain(r => r.Name == "manager");
var bob = await UserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName("bob"));
roles = await UserRepository.GetRolesAsync(bob.Id);
roles.Count.ShouldBe(1);
roles.ShouldContain(r => r.Name == "manager");
await UserRepository.UpdateRolesAsync(manager.Id, supporter.Id);
roles = await UserRepository.GetRolesAsync(bob.Id);
roles.Count.ShouldBe(1);
roles.ShouldContain(r => r.Name == "supporter");
}
}

Loading…
Cancel
Save