diff --git a/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ApplicationSingleDataSeederWorker.cs b/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ApplicationSingleDataSeederWorker.cs new file mode 100644 index 000000000..c83f9be12 --- /dev/null +++ b/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(); + } +} diff --git a/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ClientDataSeederContributor.cs b/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ClientDataSeederContributor.cs index f0293b42e..975ce7852 100644 --- a/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/ClientDataSeederContributor.cs +++ b/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); } } diff --git a/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/IdentityDataSeedContributor.cs b/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/DataSeeder/IdentityDataSeedContributor.cs new file mode 100644 index 000000000..7ef4134a7 --- /dev/null +++ b/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 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.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, + }); + } + } + } +} diff --git a/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/SingleMigrationsEntityFrameworkCoreModule.cs b/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/SingleMigrationsEntityFrameworkCoreModule.cs index 432e9da66..d95916852 100644 --- a/aspnet-core/migrations/LY.MicroService.Applications.Single.EntityFrameworkCore/SingleMigrationsEntityFrameworkCoreModule.cs +++ b/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(); + context.Services.AddHostedService(); } }