fix: wrap identity permission bindings with execution strategy transactions
This commit is contained in:
@@ -27,6 +27,9 @@ public sealed class EfRolePermissionRepository(IdentityDbContext dbContext) : IR
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task ReplaceRolePermissionsAsync(long tenantId, long roleId, IEnumerable<long> permissionIds, CancellationToken cancellationToken = default)
|
public async Task ReplaceRolePermissionsAsync(long tenantId, long roleId, IEnumerable<long> permissionIds, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var strategy = dbContext.Database.CreateExecutionStrategy();
|
||||||
|
await strategy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
||||||
|
|
||||||
@@ -46,7 +49,9 @@ public sealed class EfRolePermissionRepository(IdentityDbContext dbContext) : IR
|
|||||||
|
|
||||||
await dbContext.RolePermissions.AddRangeAsync(toAdd, cancellationToken);
|
await dbContext.RolePermissions.AddRangeAsync(toAdd, cancellationToken);
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
await trx.CommitAsync(cancellationToken);
|
await trx.CommitAsync(cancellationToken);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||||
|
|||||||
@@ -85,28 +85,33 @@ public sealed class EfRoleTemplateRepository(IdentityDbContext dbContext) : IRol
|
|||||||
=> dbContext.SaveChangesAsync(cancellationToken);
|
=> dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
private async Task ReplacePermissionsInternalAsync(RoleTemplate template, IEnumerable<string> permissionCodes, CancellationToken cancellationToken)
|
private async Task ReplacePermissionsInternalAsync(RoleTemplate template, IEnumerable<string> permissionCodes, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var strategy = dbContext.Database.CreateExecutionStrategy();
|
||||||
|
await strategy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
||||||
|
|
||||||
// 确保模板已持久化,便于 FK 正确填充
|
// 1. 确保模板已持久化,便于 FK 正确填充
|
||||||
if (!dbContext.Entry(template).IsKeySet || template.Id == 0)
|
if (!dbContext.Entry(template).IsKeySet || template.Id == 0)
|
||||||
{
|
{
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. 归一化权限编码
|
||||||
var normalized = permissionCodes
|
var normalized = permissionCodes
|
||||||
.Where(code => !string.IsNullOrWhiteSpace(code))
|
.Where(code => !string.IsNullOrWhiteSpace(code))
|
||||||
.Select(code => code.Trim())
|
.Select(code => code.Trim())
|
||||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
// 3. 清空旧权限
|
||||||
var existing = await dbContext.RoleTemplatePermissions
|
var existing = await dbContext.RoleTemplatePermissions
|
||||||
.Where(x => x.RoleTemplateId == template.Id)
|
.Where(x => x.RoleTemplateId == template.Id)
|
||||||
.ToListAsync(cancellationToken);
|
.ToListAsync(cancellationToken);
|
||||||
|
|
||||||
dbContext.RoleTemplatePermissions.RemoveRange(existing);
|
dbContext.RoleTemplatePermissions.RemoveRange(existing);
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
// 4. 插入新权限
|
||||||
var toAdd = normalized.Select(code => new RoleTemplatePermission
|
var toAdd = normalized.Select(code => new RoleTemplatePermission
|
||||||
{
|
{
|
||||||
RoleTemplateId = template.Id,
|
RoleTemplateId = template.Id,
|
||||||
@@ -115,6 +120,8 @@ public sealed class EfRoleTemplateRepository(IdentityDbContext dbContext) : IRol
|
|||||||
|
|
||||||
await dbContext.RoleTemplatePermissions.AddRangeAsync(toAdd, cancellationToken);
|
await dbContext.RoleTemplatePermissions.AddRangeAsync(toAdd, cancellationToken);
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
await trx.CommitAsync(cancellationToken);
|
await trx.CommitAsync(cancellationToken);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ public sealed class EfUserRoleRepository(IdentityDbContext dbContext) : IUserRol
|
|||||||
.ContinueWith(t => (IReadOnlyList<UserRole>)t.Result, cancellationToken);
|
.ContinueWith(t => (IReadOnlyList<UserRole>)t.Result, cancellationToken);
|
||||||
|
|
||||||
public async Task ReplaceUserRolesAsync(long tenantId, long userId, IEnumerable<long> roleIds, CancellationToken cancellationToken = default)
|
public async Task ReplaceUserRolesAsync(long tenantId, long userId, IEnumerable<long> roleIds, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var strategy = dbContext.Database.CreateExecutionStrategy();
|
||||||
|
await strategy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
await using var trx = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
||||||
|
|
||||||
@@ -41,7 +44,9 @@ public sealed class EfUserRoleRepository(IdentityDbContext dbContext) : IUserRol
|
|||||||
|
|
||||||
await dbContext.UserRoles.AddRangeAsync(toAdd, cancellationToken);
|
await dbContext.UserRoles.AddRangeAsync(toAdd, cancellationToken);
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
await trx.CommitAsync(cancellationToken);
|
await trx.CommitAsync(cancellationToken);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||||
|
|||||||
Reference in New Issue
Block a user