Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create system roles by default #17341

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<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();
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}
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>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't need to be groped since they don't depend on each other

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will make no difference

.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
15 changes: 0 additions & 15 deletions src/OrchardCore.Themes/TheAdmin/Recipes/blank.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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": []
}
]
}
Expand Down
10 changes: 0 additions & 10 deletions src/OrchardCore.Themes/TheAdmin/Recipes/headless.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -97,11 +92,6 @@
"ExecuteGraphQL",
"ExecuteApiAll"
]
},
{
"Name": "Anonymous",
"Description": "A system role representing all non-authenticated users.",
"Permissions": []
}
]
},
Expand Down
15 changes: 0 additions & 15 deletions src/OrchardCore.Themes/TheAgencyTheme/Recipes/agency.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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": []
}
]
},
Expand Down
15 changes: 0 additions & 15 deletions src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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": []
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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": []
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down