Browse Source

feat: 完善单体项目数据种子初始化

pull/1166/head
colin 12 months ago
parent
commit
fbc4dacbb9
  1. 21
      aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ApplicationSingleDataSeederWorker.cs
  2. 31
      aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ClientDataSeederContributor.cs
  3. 53
      aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/IdentityDataSeedContributor.cs
  4. 2
      aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/SingleMigrationsEntityFrameworkCoreModule.cs

21
aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ApplicationSingleDataSeederWorker.cs

@ -0,0 +1,21 @@
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Data;
namespace LY.MicroService.Applications.Single.EntityFrameworkCore.DataSeeder;
public class ApplicationSingleDataSeederWorker : BackgroundService
{
protected IDataSeeder DataSeeder { get; }
public ApplicationSingleDataSeederWorker(IDataSeeder dataSeeder)
{
DataSeeder = dataSeeder;
}
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
await DataSeeder.SeedAsync();
}
}

31
aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ClientDataSeederContributor.cs

@ -7,6 +7,8 @@ using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.OpenIddict.Applications;
using Volo.Abp.OpenIddict.Scopes;
using Volo.Abp.PermissionManagement;
namespace LY.MicroService.Applications.Single.EntityFrameworkCore.DataSeeder;
@ -14,21 +16,28 @@ namespace LY.MicroService.Applications.Single.EntityFrameworkCore.DataSeeder;
public class ClientDataSeederContributor : IDataSeedContributor, ITransientDependency
{
private readonly IOpenIddictApplicationManager _applicationManager;
private readonly IOpenIddictApplicationRepository _applicationRepository;
private readonly IOpenIddictScopeManager _scopeManager;
private readonly IOpenIddictScopeRepository _scopeRepository;
private readonly IPermissionDataSeeder _permissionDataSeeder;
private readonly IConfiguration _configuration;
private readonly ICurrentTenant _currentTenant;
public ClientDataSeederContributor(
IOpenIddictApplicationManager applicationManager,
IOpenIddictApplicationManager applicationManager,
IOpenIddictApplicationRepository applicationRepository,
IOpenIddictScopeManager scopeManager,
IOpenIddictScopeRepository scopeRepository,
IPermissionDataSeeder permissionDataSeeder,
IConfiguration configuration,
ICurrentTenant currentTenant)
{
_applicationManager = applicationManager;
_applicationRepository = applicationRepository;
_scopeManager = scopeManager;
_scopeRepository = scopeRepository;
_permissionDataSeeder = permissionDataSeeder;
_configuration = configuration;
_currentTenant = currentTenant;
@ -52,7 +61,7 @@ public class ClientDataSeederContributor : IDataSeedContributor, ITransientDepen
private async Task CreateScopeAsync(string scope)
{
if (await _scopeManager.FindByNameAsync(scope) == null)
if (await _scopeRepository.FindByNameAsync(scope) == null)
{
await _scopeManager.CreateAsync(new OpenIddictScopeDescriptor()
{
@ -80,7 +89,7 @@ public class ClientDataSeederContributor : IDataSeedContributor, ITransientDepen
{
var vueClientRootUrl = configurationSection["VueAdmin:RootUrl"].EnsureEndsWith('/');
if (await _applicationManager.FindByClientIdAsync(vueClientId) == null)
if (await _applicationRepository.FindByClientIdAsync(vueClientId) == null)
{
await _applicationManager.CreateAsync(new OpenIddictApplicationDescriptor
{
@ -103,10 +112,10 @@ public class ClientDataSeederContributor : IDataSeedContributor, ITransientDepen
{
OpenIddictConstants.Permissions.Endpoints.Authorization,
OpenIddictConstants.Permissions.Endpoints.Token,
OpenIddictConstants.Permissions.Endpoints.Device,
OpenIddictConstants.Permissions.Endpoints.DeviceAuthorization,
OpenIddictConstants.Permissions.Endpoints.Introspection,
OpenIddictConstants.Permissions.Endpoints.Revocation,
OpenIddictConstants.Permissions.Endpoints.Logout,
OpenIddictConstants.Permissions.Endpoints.EndSession,
OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode,
OpenIddictConstants.Permissions.GrantTypes.Implicit,
@ -144,7 +153,7 @@ public class ClientDataSeederContributor : IDataSeedContributor, ITransientDepen
var internalServiceClientId = configurationSection["InternalService:ClientId"];
if (!internalServiceClientId.IsNullOrWhiteSpace())
{
if (await _applicationManager.FindByClientIdAsync(internalServiceClientId) == null)
if (await _applicationRepository.FindByClientIdAsync(internalServiceClientId) == null)
{
await _applicationManager.CreateAsync(new OpenIddictApplicationDescriptor
{
@ -158,10 +167,10 @@ public class ClientDataSeederContributor : IDataSeedContributor, ITransientDepen
{
OpenIddictConstants.Permissions.Endpoints.Authorization,
OpenIddictConstants.Permissions.Endpoints.Token,
OpenIddictConstants.Permissions.Endpoints.Device,
OpenIddictConstants.Permissions.Endpoints.DeviceAuthorization,
OpenIddictConstants.Permissions.Endpoints.Introspection,
OpenIddictConstants.Permissions.Endpoints.Revocation,
OpenIddictConstants.Permissions.Endpoints.Logout,
OpenIddictConstants.Permissions.Endpoints.EndSession,
OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode,
OpenIddictConstants.Permissions.GrantTypes.Implicit,
@ -189,9 +198,9 @@ public class ClientDataSeederContributor : IDataSeedContributor, ITransientDepen
});
var internalServicePermissions = new string[2]
{
"AbpIdentity.UserLookup","AbpIdentity.Users"
};
{
"AbpIdentity.UserLookup","AbpIdentity.Users"
};
await _permissionDataSeeder.SeedAsync(ClientPermissionValueProvider.ProviderName, internalServiceClientId, internalServicePermissions);
}
}

53
aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/IdentityDataSeedContributor.cs

@ -0,0 +1,53 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.Applications.Single.EntityFrameworkCore.DataSeeder;
public class IdentityDataSeedContributor : IDataSeedContributor, ITransientDependency
{
public ILogger<IdentityDataSeedContributor> Logger { protected get; set; }
protected ICurrentTenant CurrentTenant { get; }
protected IGuidGenerator GuidGenerator { get; }
protected IdentityRoleManager IdentityRoleManager { get; }
public IdentityDataSeedContributor(
ICurrentTenant currentTenant,
IGuidGenerator guidGenerator,
IdentityRoleManager identityRoleManager)
{
CurrentTenant = currentTenant;
GuidGenerator = guidGenerator;
IdentityRoleManager = identityRoleManager;
Logger = NullLogger<IdentityDataSeedContributor>.Instance;
}
public async virtual Task SeedAsync(DataSeedContext context)
{
using (CurrentTenant.Change(context.TenantId))
{
Logger.LogInformation("Seeding the default role Users...");
if (await IdentityRoleManager.FindByNameAsync("Users") == null)
{
await IdentityRoleManager.CreateAsync(
new IdentityRole(
GuidGenerator.Create(),
"Users",
context.TenantId)
{
IsDefault = true,
IsPublic = true,
IsStatic = true,
});
}
}
}
}

2
aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/SingleMigrationsEntityFrameworkCoreModule.cs

@ -11,6 +11,7 @@ using LINGYUN.Abp.TextTemplating.EntityFrameworkCore;
using LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore;
using LINGYUN.Abp.WeChat;
using LINGYUN.Platform.EntityFrameworkCore;
using LY.MicroService.Applications.Single.EntityFrameworkCore.DataSeeder;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Modularity;
@ -44,5 +45,6 @@ public class SingleMigrationsEntityFrameworkCoreModule : AbpModule
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext<SingleMigrationsDbContext>();
context.Services.AddHostedService<ApplicationSingleDataSeederWorker>();
}
}

Loading…
Cancel
Save