Add `UpdateOrganizationAsync ` to `IIdentityUserRepository`.

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

@ -132,9 +132,15 @@ public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid>
CancellationToken cancellationToken = default
);
Task UpdateRolesAsync(
Task UpdateRoleAsync(
Guid sourceRoleId,
Guid? targetRoleId,
CancellationToken cancellationToken = default
);
Task UpdateOrganizationAsync(
Guid sourceOrganizationId,
Guid? targetOrganizationId,
CancellationToken cancellationToken = default
);
}

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

@ -325,7 +325,7 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task UpdateRolesAsync(Guid sourceRoleId, Guid? targetRoleId, CancellationToken cancellationToken = default)
public virtual async Task UpdateRoleAsync(Guid sourceRoleId, Guid? targetRoleId, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
.Where(x => x.Roles.Any(r => r.RoleId == sourceRoleId))
@ -342,4 +342,22 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
await UpdateManyAsync(users, cancellationToken: cancellationToken);
}
public virtual async Task UpdateOrganizationAsync(Guid sourceOrganizationId, Guid? targetOrganizationId, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
.Where(x => x.OrganizationUnits.Any(r => r.OrganizationUnitId == sourceOrganizationId))
.ToListAsync(GetCancellationToken(cancellationToken));
foreach (var user in users)
{
user.RemoveOrganizationUnit(sourceOrganizationId);
if (targetOrganizationId.HasValue)
{
user.AddOrganizationUnit(targetOrganizationId.Value);
}
}
await UpdateManyAsync(users, cancellationToken: cancellationToken);
}
}

@ -16,13 +16,15 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : AbpIdentity
protected IIdentityUserRepository UserRepository { get; }
protected ILookupNormalizer LookupNormalizer { get; }
protected IOrganizationUnitRepository OrganizationUnitRepository { get; }
protected OrganizationUnitManager OrganizationUnitManager { get; }
protected IdentityTestData TestData { get; }
protected IdentityUserRepository_Tests()
{
UserRepository = ServiceProvider.GetRequiredService<IIdentityUserRepository>();
LookupNormalizer = ServiceProvider.GetRequiredService<ILookupNormalizer>();
OrganizationUnitRepository = ServiceProvider.GetRequiredService<IOrganizationUnitRepository>();
UserRepository = GetRequiredService<IIdentityUserRepository>();
LookupNormalizer = GetRequiredService<ILookupNormalizer>();
OrganizationUnitRepository = GetRequiredService<IOrganizationUnitRepository>();
OrganizationUnitManager = GetRequiredService<OrganizationUnitManager>();;
TestData = ServiceProvider.GetRequiredService<IdentityTestData>();
}
@ -191,7 +193,7 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : AbpIdentity
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);
await UserRepository.UpdateRoleAsync(supporter.Id, null);
roles = await UserRepository.GetRolesAsync(john.Id);
roles.Count.ShouldBe(2);
@ -203,10 +205,43 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : AbpIdentity
roles.Count.ShouldBe(1);
roles.ShouldContain(r => r.Name == "manager");
await UserRepository.UpdateRolesAsync(manager.Id, supporter.Id);
await UserRepository.UpdateRoleAsync(manager.Id, supporter.Id);
roles = await UserRepository.GetRolesAsync(bob.Id);
roles.Count.ShouldBe(1);
roles.ShouldContain(r => r.Name == "supporter");
}
[Fact]
public async Task UpdateOrganizationAsync()
{
var david = await UserRepository.FindByNormalizedUserNameAsync(LookupNormalizer.NormalizeName("david"));
var organizationUnits = await UserRepository.GetOrganizationUnitsAsync(david.Id);
var ou111 = await OrganizationUnitRepository.GetAsync("OU111");
var ou112 = await OrganizationUnitRepository.GetAsync("OU112");
organizationUnits.Count.ShouldBe(1);
organizationUnits.ShouldContain(r => r.Id == ou112.Id);
await UserRepository.UpdateOrganizationAsync(ou112.Id, null);
organizationUnits = await UserRepository.GetOrganizationUnitsAsync(david.Id);
organizationUnits.Count.ShouldBe(0);
var ou111Users = await UserRepository.GetUsersInOrganizationUnitAsync(ou111.Id);
ou111Users.Count.ShouldBe(2);
ou111Users.ShouldContain(x => x.UserName == "john.nash");
ou111Users.ShouldContain(x => x.UserName == "neo");
var ou112Users = await UserRepository.GetUsersInOrganizationUnitAsync(ou112.Id);
ou112Users.Count.ShouldBe(0);
await UserRepository.UpdateOrganizationAsync(ou111.Id, ou112.Id);
ou112Users = await UserRepository.GetUsersInOrganizationUnitAsync(ou112.Id);
ou112Users.Count.ShouldBe(2);
ou112Users.ShouldContain(x => x.UserName == "john.nash");
ou112Users.ShouldContain(x => x.UserName == "neo");
}
}

Loading…
Cancel
Save