fix: wrap identity permission bindings with execution strategy transactions

This commit is contained in:
2025-12-06 14:35:06 +08:00
parent 4120aec055
commit ab4e407f9c
3 changed files with 75 additions and 58 deletions

View File

@@ -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)
{
var strategy = dbContext.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(async () =>
{
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.SaveChangesAsync(cancellationToken);
await trx.CommitAsync(cancellationToken);
});
}
public Task SaveChangesAsync(CancellationToken cancellationToken = default)

View File

@@ -85,28 +85,33 @@ public sealed class EfRoleTemplateRepository(IdentityDbContext dbContext) : IRol
=> dbContext.SaveChangesAsync(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);
// 确保模板已持久化,便于 FK 正确填充
// 1. 确保模板已持久化,便于 FK 正确填充
if (!dbContext.Entry(template).IsKeySet || template.Id == 0)
{
await dbContext.SaveChangesAsync(cancellationToken);
}
// 2. 归一化权限编码
var normalized = permissionCodes
.Where(code => !string.IsNullOrWhiteSpace(code))
.Select(code => code.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
// 3. 清空旧权限
var existing = await dbContext.RoleTemplatePermissions
.Where(x => x.RoleTemplateId == template.Id)
.ToListAsync(cancellationToken);
dbContext.RoleTemplatePermissions.RemoveRange(existing);
await dbContext.SaveChangesAsync(cancellationToken);
// 4. 插入新权限
var toAdd = normalized.Select(code => new RoleTemplatePermission
{
RoleTemplateId = template.Id,
@@ -115,6 +120,8 @@ public sealed class EfRoleTemplateRepository(IdentityDbContext dbContext) : IRol
await dbContext.RoleTemplatePermissions.AddRangeAsync(toAdd, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
await trx.CommitAsync(cancellationToken);
});
}
}

View File

@@ -22,6 +22,9 @@ public sealed class EfUserRoleRepository(IdentityDbContext dbContext) : IUserRol
.ContinueWith(t => (IReadOnlyList<UserRole>)t.Result, cancellationToken);
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);
@@ -41,7 +44,9 @@ public sealed class EfUserRoleRepository(IdentityDbContext dbContext) : IUserRol
await dbContext.UserRoles.AddRangeAsync(toAdd, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
await trx.CommitAsync(cancellationToken);
});
}
public Task SaveChangesAsync(CancellationToken cancellationToken = default)