Skip to content

Commit

Permalink
Create a separate data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Jan 13, 2025
1 parent 718d098 commit a79ade2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IRole> _roleManager;
private readonly ILogger _logger;

public RolesMigrations(
IOptions<SystemRoleOptions> systemRoleOptions,
ISystemRoleNameProvider systemRoleNameProvider,
RoleManager<IRole> roleManager,
ILogger<RolesMigrations> logger)
{
_systemRoleOptions = systemRoleOptions.Value;
_systemRoleNameProvider = systemRoleNameProvider;
_roleManager = roleManager;
_logger = logger;
}

Expand Down Expand Up @@ -132,18 +126,6 @@ await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("MigrateAdminUsersToNewAdm
return 1;
}

public async Task<int> 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<IRole> roles)
{
var counter = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IRole> _roleManager;
private readonly ILogger _logger;

public SystemRolesMigrations(
ISystemRoleNameProvider systemRoleNameProvider,
RoleManager<IRole> roleManager,
ILogger<RolesMigrations> logger)
{
_systemRoleNameProvider = systemRoleNameProvider;
_roleManager = roleManager;
_logger = logger;
}

public async Task<int> 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;
}
}
5 changes: 4 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Roles/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public Startup(IShellConfiguration shellConfiguration)
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IUserClaimsProvider, RoleClaimsProvider>();
services.AddDataMigration<RolesMigrations>();

services.AddDataMigration<SystemRolesMigrations>()
.AddDataMigration<RolesMigrations>();

services.AddScoped<RoleStore>();
services.Replace(ServiceDescriptor.Scoped<IRoleClaimStore<IRole>>(sp => sp.GetRequiredService<RoleStore>()));
services.Replace(ServiceDescriptor.Scoped<IRoleStore<IRole>>(sp => sp.GetRequiredService<RoleStore>()));
Expand Down

0 comments on commit a79ade2

Please sign in to comment.