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

Merged
merged 33 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bd65eed
Create system roles by default
hishamco Jan 12, 2025
151ad09
Add missing roles
hishamco Jan 12, 2025
6117d19
Remove roles from *.recipe.json
hishamco Jan 12, 2025
da19704
Address feedback
hishamco Jan 13, 2025
718d098
Merge branch 'main' into hishamco/system-roles
hishamco Jan 13, 2025
a79ade2
Create a separate data migration
hishamco Jan 13, 2025
5e21655
Remove system roles from migrations.recipe.json
hishamco Jan 13, 2025
e5e0c0d
Address feedback
hishamco Jan 13, 2025
915a554
Merge branch 'main' into hishamco/system-roles
hishamco Jan 13, 2025
5b70ef2
Remove roles from pages.recipe.json
hishamco Jan 14, 2025
784e912
Introduce ISystemRoleProvider
hishamco Jan 15, 2025
7ee0c37
Remove warnings
hishamco Jan 15, 2025
9d38a6c
Merge branch 'main' into hishamco/system-roles
hishamco Jan 15, 2025
56ffeb3
Apply suggestions from code review
hishamco Jan 15, 2025
cb0fd77
localize the description
hishamco Jan 18, 2025
0a49bae
Merge branch 'main' into hishamco/system-roles
hishamco Jan 18, 2025
7a2da40
Remove obsolete from DefaultSystemRoleNameProvider
hishamco Jan 18, 2025
b97848f
Don't depend on role position
hishamco Jan 18, 2025
9026f46
Add GetAdminRoleAsync() to ISystemRoleProvider
hishamco Jan 18, 2025
fa7775a
Merge branch 'main' into hishamco/system-roles
hishamco Jan 18, 2025
48a3719
Remove ISystemRoleNameProvider injection
hishamco Jan 18, 2025
3f104ab
Add more unit tests
hishamco Jan 18, 2025
674c862
Fix the build
hishamco Jan 18, 2025
748e6d8
localizer -> stringLocalizer
hishamco Jan 18, 2025
313651e
Obsolete SystemRoleNameProviderExtensions
hishamco Jan 18, 2025
d2245e3
Address feedback
hishamco Jan 19, 2025
2470fb7
Fix warnings
hishamco Jan 19, 2025
7b7ba19
Apply suggestions from code review
hishamco Jan 20, 2025
b3c15a6
Fix the build
hishamco Jan 20, 2025
26177f3
Remove unnecessary local field
hishamco Jan 20, 2025
fd7e657
Remove extra cast
hishamco Jan 20, 2025
ea580d0
cleanup for approval
MikeAlhayek Jan 21, 2025
1791300
Fix tests
MikeAlhayek Jan 21, 2025
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,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();
hishamco marked this conversation as resolved.
Show resolved Hide resolved

foreach (var role in systemRoles)
{
await _roleManager.CreateAsync(new Role { RoleName = role });
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}

_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>()
hishamco marked this conversation as resolved.
Show resolved Hide resolved
.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
Copy link
Member

Choose a reason for hiding this comment

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

The SaaS recipe still has these. pages.recipe.json too but I'm not sure how that's used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Me too :) but seems used the module for test project to test Assembly attributes if I recall

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
19 changes: 0 additions & 19 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 All @@ -88,20 +83,6 @@
"Name": "Contributor",
"Description": "Grants users the ability to contribute content.",
"Permissions": []
},
{
"Name": "Authenticated",
"Description": "A system role representing all authenticated users.",
"Permissions": [
"ViewContent",
hishamco marked this conversation as resolved.
Show resolved Hide resolved
"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
Loading