diff --git a/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/RolesMigrations.cs b/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/RolesMigrations.cs index ab2098f10b5..eae78a2ce3a 100644 --- a/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/RolesMigrations.cs +++ b/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/RolesMigrations.cs @@ -16,19 +16,13 @@ public sealed class RolesMigrations : DataMigration private static readonly string _alternativeAdminRoleName = "SiteOwner"; private readonly SystemRoleOptions _systemRoleOptions; - private readonly ISystemRoleNameProvider _systemRoleNameProvider; - private readonly RoleManager _roleManager; private readonly ILogger _logger; public RolesMigrations( IOptions systemRoleOptions, - ISystemRoleNameProvider systemRoleNameProvider, - RoleManager roleManager, ILogger logger) { _systemRoleOptions = systemRoleOptions.Value; - _systemRoleNameProvider = systemRoleNameProvider; - _roleManager = roleManager; _logger = logger; } @@ -132,18 +126,6 @@ await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("MigrateAdminUsersToNewAdm return 1; } - public async Task UpdateFrom1Async() - { - var systemRoles = await _systemRoleNameProvider.GetSystemRolesAsync(); - - foreach (var role in systemRoles) - { - await _roleManager.CreateAsync(new Role { RoleName = role }); - } - - return 2; - } - private static string GenerateNewAdminRoleName(List roles) { var counter = 1; diff --git a/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/SystemRolesMigrations.cs b/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/SystemRolesMigrations.cs new file mode 100644 index 00000000000..5bcb5511be5 --- /dev/null +++ b/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/SystemRolesMigrations.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Logging; +using OrchardCore.Data.Migration; +using OrchardCore.Security; + +namespace OrchardCore.Roles.Migrations; + +public sealed class SystemRolesMigrations : DataMigration +{ + private readonly ISystemRoleNameProvider _systemRoleNameProvider; + private readonly RoleManager _roleManager; + private readonly ILogger _logger; + + public SystemRolesMigrations( + ISystemRoleNameProvider systemRoleNameProvider, + RoleManager roleManager, + ILogger logger) + { + _systemRoleNameProvider = systemRoleNameProvider; + _roleManager = roleManager; + _logger = logger; + } + + public async Task CreateAsync() + { + var systemRoles = await _systemRoleNameProvider.GetSystemRolesAsync(); + + foreach (var role in systemRoles) + { + await _roleManager.CreateAsync(new Role { RoleName = role }); + } + + _logger.LogInformation("The system roles have been created successfully."); + + return 1; + } +} diff --git a/src/OrchardCore.Modules/OrchardCore.Roles/Startup.cs b/src/OrchardCore.Modules/OrchardCore.Roles/Startup.cs index 6adff2c006c..b35f7f3d3ea 100644 --- a/src/OrchardCore.Modules/OrchardCore.Roles/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.Roles/Startup.cs @@ -33,7 +33,10 @@ public Startup(IShellConfiguration shellConfiguration) public override void ConfigureServices(IServiceCollection services) { services.AddScoped(); - services.AddDataMigration(); + + services.AddDataMigration() + .AddDataMigration(); + services.AddScoped(); services.Replace(ServiceDescriptor.Scoped>(sp => sp.GetRequiredService())); services.Replace(ServiceDescriptor.Scoped>(sp => sp.GetRequiredService()));