chore: 升级依赖并优化种子
This commit is contained in:
@@ -7,6 +7,11 @@ namespace TakeoutSaaS.Infrastructure.Identity.Options;
|
||||
/// </summary>
|
||||
public sealed class AdminSeedOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否启用后台账号与权限种子。
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 初始用户列表。
|
||||
/// </summary>
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Npgsql;
|
||||
using TakeoutSaaS.Infrastructure.Identity.Options;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
using DomainIdentityUser = TakeoutSaaS.Domain.Identity.Entities.IdentityUser;
|
||||
@@ -29,6 +30,11 @@ public sealed class IdentityDataSeeder(IServiceProvider serviceProvider, ILogger
|
||||
var passwordHasher = scope.ServiceProvider.GetRequiredService<IPasswordHasher<DomainIdentityUser>>();
|
||||
var tenantContextAccessor = scope.ServiceProvider.GetRequiredService<ITenantContextAccessor>();
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
logger.LogInformation("AdminSeed 已禁用,跳过后台账号初始化");
|
||||
return;
|
||||
}
|
||||
await context.Database.MigrateAsync(cancellationToken);
|
||||
|
||||
if (options.Users is null or { Count: 0 })
|
||||
@@ -127,15 +133,35 @@ public sealed class IdentityDataSeeder(IServiceProvider serviceProvider, ILogger
|
||||
.Where(ur => ur.TenantId == userOptions.TenantId && ur.UserId == user.Id)
|
||||
.ToListAsync(cancellationToken);
|
||||
context.UserRoles.RemoveRange(existingUserRoles);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
var roleIds = roleEntities.Select(r => r.Id).Distinct().ToArray();
|
||||
var userRoles = roleIds.Select(roleId => new DomainUserRole
|
||||
foreach (var roleId in roleIds)
|
||||
{
|
||||
TenantId = userOptions.TenantId,
|
||||
UserId = user.Id,
|
||||
RoleId = roleId
|
||||
});
|
||||
await context.UserRoles.AddRangeAsync(userRoles, cancellationToken);
|
||||
try
|
||||
{
|
||||
var alreadyExists = await context.UserRoles.AnyAsync(
|
||||
ur => ur.TenantId == userOptions.TenantId && ur.UserId == user.Id && ur.RoleId == roleId,
|
||||
cancellationToken);
|
||||
if (alreadyExists)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
await context.UserRoles.AddAsync(new DomainUserRole
|
||||
{
|
||||
TenantId = userOptions.TenantId,
|
||||
UserId = user.Id,
|
||||
RoleId = roleId
|
||||
}, cancellationToken);
|
||||
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException pg && pg.SqlState == PostgresErrorCodes.UniqueViolation)
|
||||
{
|
||||
context.ChangeTracker.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
// 为种子角色绑定种子权限
|
||||
if (permissions.Length > 0 && roleIds.Length > 0)
|
||||
@@ -145,14 +171,41 @@ public sealed class IdentityDataSeeder(IServiceProvider serviceProvider, ILogger
|
||||
.Where(rp => rp.TenantId == userOptions.TenantId && roleIds.Contains(rp.RoleId))
|
||||
.ToListAsync(cancellationToken);
|
||||
context.RolePermissions.RemoveRange(existingRolePermissions);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
var newRolePermissions = roleIds.SelectMany(roleId => permissionIds.Select(permissionId => new DomainRolePermission
|
||||
var distinctRoleIds = roleIds.Distinct().ToArray();
|
||||
var distinctPermissionIds = permissionIds.Distinct().ToArray();
|
||||
foreach (var roleId in distinctRoleIds)
|
||||
{
|
||||
TenantId = userOptions.TenantId,
|
||||
RoleId = roleId,
|
||||
PermissionId = permissionId
|
||||
}));
|
||||
await context.RolePermissions.AddRangeAsync(newRolePermissions, cancellationToken);
|
||||
foreach (var permissionId in distinctPermissionIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
var exists = await context.RolePermissions.AnyAsync(
|
||||
rp => rp.TenantId == userOptions.TenantId
|
||||
&& rp.RoleId == roleId
|
||||
&& rp.PermissionId == permissionId,
|
||||
cancellationToken);
|
||||
if (exists)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
await context.RolePermissions.AddAsync(new DomainRolePermission
|
||||
{
|
||||
TenantId = userOptions.TenantId,
|
||||
RoleId = roleId,
|
||||
PermissionId = permissionId
|
||||
}, cancellationToken);
|
||||
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException pg && pg.SqlState == PostgresErrorCodes.UniqueViolation)
|
||||
{
|
||||
context.ChangeTracker.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,15 +262,33 @@ public sealed class IdentityDataSeeder(IServiceProvider serviceProvider, ILogger
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
context.RoleTemplatePermissions.RemoveRange(existingPermissions);
|
||||
|
||||
var toAdd = permissionCodes.Select(code => new DomainRoleTemplatePermission
|
||||
{
|
||||
RoleTemplateId = existing.Id,
|
||||
PermissionCode = code
|
||||
});
|
||||
|
||||
await context.RoleTemplatePermissions.AddRangeAsync(toAdd, cancellationToken);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
var distinctPermissionCodes = permissionCodes.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
|
||||
foreach (var permissionCode in distinctPermissionCodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
var alreadyExists = await context.RoleTemplatePermissions.AnyAsync(
|
||||
x => x.RoleTemplateId == existing.Id && x.PermissionCode == permissionCode,
|
||||
cancellationToken);
|
||||
if (alreadyExists)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
await context.RoleTemplatePermissions.AddAsync(new DomainRoleTemplatePermission
|
||||
{
|
||||
RoleTemplateId = existing.Id,
|
||||
PermissionCode = permissionCode
|
||||
}, cancellationToken);
|
||||
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException pg && pg.SqlState == PostgresErrorCodes.UniqueViolation)
|
||||
{
|
||||
context.ChangeTracker.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user