-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
@hishamco I know this is a draft. But I think you are missing the ask. Not all these roles are system role. There are currently only 3 system roles. Administration (this name could be changed), Anonymous, and Authorized. You should inject ISystemRolePrpvider to get a list of the system roles. OrchardCore/src/OrchardCore/OrchardCore.Roles.Abstractions/ISystemRoleNameProvider.cs Line 5 in a2f45e5
In the roles module, create a SystemRolesMigrations file that would read the system roles from the provider and create them using the RoleManager. Then you can remove only these 3 roles from the recipes. |
Thanks it is still a draft, I added the system roles first, then added the rest accidentally Using the provider is much better, I will react to your feedback |
@MikeAlhayek I didn't see a description there, or shall we make |
I tried the PR on a fresh installation and it works fine but I don't know why it fails in Linux |
src/OrchardCore.Modules/OrchardCore.Roles/Migrations/SystemRolesMigrations.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Roles/Migrations/SystemRolesMigrations.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
src/OrchardCore.Modules/OrchardCore.Roles/Migrations/SystemRolesMigrations.cs
Outdated
Show resolved
Hide resolved
#pragma warning disable CS0618 // Type or member is obsolete | ||
#pragma warning disable IDE0060 // Remove unused parameter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why were these added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid the warning cause OC treat them as errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't inject the old interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be a breaking change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would it be a breaking change? You should stop using the old interface and use the new one instead. Why do you need the old interface injected here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would it be a breaking change?
What if someone subclass one of those classes that rely on the old interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that is the case, you are already causing a breaking change by injecting a new dependency. Anyway, I would not worry about that and remove the injection of the old interface. This is going to be part pf 3.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The no need to obsolete things :)
src/OrchardCore/OrchardCore.Roles.Abstractions/Extensions/SystemRoleProviderExtensions.cs
Outdated
Show resolved
Hide resolved
public static async ValueTask<IRole> GetAdminRoleAsync(this ISystemRoleProvider systemRoleNameProvider) | ||
=> (await systemRoleNameProvider.GetSystemRolesAsync()).First(); | ||
|
||
public static async ValueTask<bool> IsSystemRoleAsync(this ISystemRoleProvider systemRoleNameProvider, string name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this was moved to the interface as suggested originally, you can optimize this by adding a case insensitive dictionary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not addressed yet. Move it to the interface so we have more efficient lookup than having to enumerate over the collection every single time. Use a frozen Dictionary internally with a case insensitive look up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be against this approach, why do we need to use case insensitive, I remember RoleManager
uses ILookupNormalizer
src/OrchardCore/OrchardCore.Roles.Abstractions/ISystemRoleNameProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/DefaultSystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/Services/DefaultSystemRoleNameProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/ServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore.Modules/OrchardCore.Roles/Services/RoleClaimsProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/DefaultSystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Mike Alhayek <[email protected]>
@hishamco can you please finish one PR at a time? I am seeing more PRs coming in but a PR like this was put on the shelve. This is one of the reasons why many PR just sit. |
src/OrchardCore/OrchardCore.Roles.Abstractions/Extensions/SystemRoleProviderExtensions.cs
Outdated
Show resolved
Hide resolved
{ | ||
ValueTask<IEnumerable<IRole>> GetSystemRolesAsync(); | ||
|
||
ValueTask<IRole> GetAdminRoleAsync(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add bool IsSystemRole function.
Also, I don't think all th functions here should not be async
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add bool IsSystemRole function.
There is no need for it could be an extension method as I did because it could be computed from GetSystemRolesAsync()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. But it's less efficient. Use a frozen dictionary and a look up agains that dictionary is faster what we need in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was working well because you was stored strings previously
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can still use FrozenDictionary<string, IRole> in the implementation so the lookup by name is fast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check the implementation that I linked here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should I look for? Please review my original suggestion for the interface and stick to it. Only thing I would change is replace the async calls with sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To check the implementation, coz your implementation will expose FrozenSet
if you intend to ignore the casing we could use the sort of Normalize()
function. I'm still suggesting exposing IEnumerable<IRole>
like RoleManager
is a good option
In terms of PERF, let's hear @sebastienros thought about this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh... it's a singleton object and we check if a role is a system role often (maybe on every single request) so optimization would be great here. You don't need to expose a FrozenSet, you can expose IEnumerable. But, expose IsSystemRole method that would accept a string parameter and returns Boolean. Internally you'll have a frozen set to manage the system roles. A 10 minute PR is going to take 1+ month. Please only request my review when my feedback addresses so we are not wasting time with the unnecessary back and forth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please only request my review when my feedback addresses so we are not wasting time with the unnecessary back and forth.
Please don't take it in person, the idea is that I'm reacting to the feedback when it makes sense to me because each one thinks in a different way
I will try to use FrozenSet
but moving IsSystemRole()
back to the interface is not make sense
src/OrchardCore/OrchardCore.Roles.Core/DefaultSystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/SystemRoleNameProviderExtensions.cs
Outdated
Show resolved
Hide resolved
{ | ||
public static bool IsAdminRole(this ISystemRoleProvider systemRoleNameProvider, string name) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(name, nameof(name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the second argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the role that we need to check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure what you are asking. I am saying, you don't the second parameter nameof(name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception needs the argument value and argument name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception needs the argument value and argument name
The paramName
argument has the CallerArgumentExpressionAtribute
and it takes the argument parameter. So you only need to give it the first parameter (argument
):
ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
src/OrchardCore/OrchardCore.Roles.Abstractions/ISystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/DefaultSystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/DefaultSystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/DefaultSystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
src/OrchardCore/OrchardCore.Roles.Core/DefaultSystemRoleProvider.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Mike Alhayek <[email protected]>
Oops, last code suggestion break the build |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hishamco I just made minor cleanup commit to approve the PR for you.
I wanted to wait until you two have finished discussing this, but now I see Georg also approved, so nothing to add. |
Fixes #17271