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..6bccab0f1b5 --- /dev/null +++ b/src/OrchardCore.Modules/OrchardCore.Roles/Migrations/SystemRolesMigrations.cs @@ -0,0 +1,40 @@ +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) + { + if (await _roleManager.FindByNameAsync(role) is null) + { + 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())); diff --git a/src/OrchardCore.Themes/TheAdmin/Recipes/blank.recipe.json b/src/OrchardCore.Themes/TheAdmin/Recipes/blank.recipe.json index 5b170e73669..0e9cdc2a774 100644 --- a/src/OrchardCore.Themes/TheAdmin/Recipes/blank.recipe.json +++ b/src/OrchardCore.Themes/TheAdmin/Recipes/blank.recipe.json @@ -69,11 +69,6 @@ { "name": "Roles", "Roles": [ - { - "Name": "Administrator", - "Description": "A system role that grants all permissions to the assigned users.", - "Permissions": [] - }, { "Name": "Moderator", "Description": "Grants users the ability to moderate content.", @@ -93,16 +88,6 @@ "Name": "Contributor", "Description": "Grants users the ability to contribute content.", "Permissions": [] - }, - { - "Name": "Authenticated", - "Description": "A system role representing all authenticated users.", - "Permissions": [] - }, - { - "Name": "Anonymous", - "Description": "A system role representing all non-authenticated users.", - "Permissions": [] } ] } diff --git a/src/OrchardCore.Themes/TheAdmin/Recipes/headless.recipe.json b/src/OrchardCore.Themes/TheAdmin/Recipes/headless.recipe.json index 65a4b0a24d3..a050cdf8920 100644 --- a/src/OrchardCore.Themes/TheAdmin/Recipes/headless.recipe.json +++ b/src/OrchardCore.Themes/TheAdmin/Recipes/headless.recipe.json @@ -64,11 +64,6 @@ { "name": "Roles", "Roles": [ - { - "Name": "Administrator", - "Description": "A system role that grants all permissions to the assigned users.", - "Permissions": [] - }, { "Name": "Moderator", "Description": "Grants users the ability to moderate content.", @@ -97,11 +92,6 @@ "ExecuteGraphQL", "ExecuteApiAll" ] - }, - { - "Name": "Anonymous", - "Description": "A system role representing all non-authenticated users.", - "Permissions": [] } ] }, diff --git a/src/OrchardCore.Themes/TheAgencyTheme/Recipes/agency.recipe.json b/src/OrchardCore.Themes/TheAgencyTheme/Recipes/agency.recipe.json index 9c99ed5b29d..c8dd925d27e 100644 --- a/src/OrchardCore.Themes/TheAgencyTheme/Recipes/agency.recipe.json +++ b/src/OrchardCore.Themes/TheAgencyTheme/Recipes/agency.recipe.json @@ -73,11 +73,6 @@ { "name": "Roles", "Roles": [ - { - "Name": "Administrator", - "Description": "A system role that grants all permissions to the assigned users.", - "Permissions": [] - }, { "Name": "Moderator", "Description": "Grants users the ability to moderate content.", @@ -97,16 +92,6 @@ "Name": "Contributor", "Description": "Grants users the ability to contribute content.", "Permissions": [] - }, - { - "Name": "Authenticated", - "Description": "A system role representing all authenticated users.", - "Permissions": [] - }, - { - "Name": "Anonymous", - "Description": "A system role representing all non-authenticated users.", - "Permissions": [] } ] }, diff --git a/src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json b/src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json index ceabdaee507..662f6ecd704 100644 --- a/src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json +++ b/src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json @@ -87,11 +87,6 @@ { "name": "Roles", "Roles": [ - { - "Name": "Administrator", - "Description": "A system role that grants all permissions to the assigned users.", - "Permissions": [] - }, { "Name": "Moderator", "Description": "Grants users the ability to moderate content.", @@ -111,16 +106,6 @@ "Name": "Contributor", "Description": "Grants users the ability to contribute content.", "Permissions": [] - }, - { - "Name": "Authenticated", - "Description": "A system role representing all authenticated users.", - "Permissions": [] - }, - { - "Name": "Anonymous", - "Description": "A system role representing all non-authenticated users.", - "Permissions": [] } ] }, diff --git a/src/OrchardCore.Themes/TheComingSoonTheme/Recipes/comingsoon.recipe.json b/src/OrchardCore.Themes/TheComingSoonTheme/Recipes/comingsoon.recipe.json index bc8b03a9ebe..03a0016101a 100644 --- a/src/OrchardCore.Themes/TheComingSoonTheme/Recipes/comingsoon.recipe.json +++ b/src/OrchardCore.Themes/TheComingSoonTheme/Recipes/comingsoon.recipe.json @@ -70,11 +70,6 @@ { "name": "Roles", "Roles": [ - { - "Name": "Administrator", - "Description": "A system role that grants all permissions to the assigned users.", - "Permissions": [] - }, { "Name": "Moderator", "Description": "Grants users the ability to moderate content.", @@ -94,16 +89,6 @@ "Name": "Contributor", "Description": "Grants users the ability to contribute content.", "Permissions": [] - }, - { - "Name": "Authenticated", - "Description": "A system role representing all authenticated users.", - "Permissions": [] - }, - { - "Name": "Anonymous", - "Description": "A system role representing all non-authenticated users.", - "Permissions": [] } ] }, diff --git a/test/OrchardCore.Tests.Functional/cms-tests/Recipes/migrations.recipe.json b/test/OrchardCore.Tests.Functional/cms-tests/Recipes/migrations.recipe.json index dcb325626a3..09b72937861 100644 --- a/test/OrchardCore.Tests.Functional/cms-tests/Recipes/migrations.recipe.json +++ b/test/OrchardCore.Tests.Functional/cms-tests/Recipes/migrations.recipe.json @@ -84,26 +84,6 @@ "admin": "TheAdmin", "site": "TheBlogTheme" }, - { - "name": "Roles", - "Roles": [ - { - "Name": "Administrator", - "Description": "A system role that grants all permissions to the assigned users.", - "Permissions": [] - }, - { - "Name": "Authenticated", - "Description": "A system role representing all authenticated users.", - "Permissions": [] - }, - { - "Name": "Anonymous", - "Description": "A system role representing all non-authenticated users.", - "Permissions": [] - } - ] - }, { "name": "settings", "HomeRoute": { diff --git a/test/OrchardCore.Tests.Pages/OrchardCore.Application.Pages/Recipes/pages.recipe.json b/test/OrchardCore.Tests.Pages/OrchardCore.Application.Pages/Recipes/pages.recipe.json index 5b2faf258ed..3ca773de57c 100644 --- a/test/OrchardCore.Tests.Pages/OrchardCore.Application.Pages/Recipes/pages.recipe.json +++ b/test/OrchardCore.Tests.Pages/OrchardCore.Application.Pages/Recipes/pages.recipe.json @@ -74,11 +74,6 @@ { "name": "Roles", "Roles": [ - { - "Name": "Administrator", - "Description": "A system role that grants all permissions to the assigned users.", - "Permissions": [] - }, { "Name": "Moderator", "Description": "Grants users the ability to moderate content.", @@ -98,16 +93,6 @@ "Name": "Contributor", "Description": "Grants users the ability to contribute content.", "Permissions": [] - }, - { - "Name": "Authenticated", - "Description": "A system role representing all authenticated users.", - "Permissions": [] - }, - { - "Name": "Anonymous", - "Description": "A system role representing all non-authenticated users.", - "Permissions": [] } ] }