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

Create system roles by default #17341

merged 33 commits into from
Jan 21, 2025

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Jan 12, 2025

Fixes #17271

@hishamco hishamco marked this pull request as draft January 12, 2025 04:40
@MikeAlhayek
Copy link
Member

@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.

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.

@hishamco
Copy link
Member Author

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

@hishamco
Copy link
Member Author

@MikeAlhayek I didn't see a description there, or shall we make ISystemRoleProvider instead of ISystemRoleNameProvider?

@hishamco hishamco marked this pull request as ready for review January 13, 2025 00:08
@hishamco hishamco requested a review from MikeAlhayek January 13, 2025 00:13
@hishamco
Copy link
Member Author

I tried the PR on a fresh installation and it works fine but I don't know why it fails in Linux

@hishamco hishamco requested a review from MikeAlhayek January 13, 2025 05:29
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

@hishamco hishamco mentioned this pull request Jan 15, 2025
10 tasks
Comment on lines 18 to 19
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable IDE0060 // Remove unused parameter
Copy link
Member

Choose a reason for hiding this comment

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

Why were these added?

Copy link
Member Author

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

Copy link
Member

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.

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 be a breaking change

Copy link
Member

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?

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Member Author

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 :)

public static async ValueTask<IRole> GetAdminRoleAsync(this ISystemRoleProvider systemRoleNameProvider)
=> (await systemRoleNameProvider.GetSystemRolesAsync()).First();

public static async ValueTask<bool> IsSystemRoleAsync(this ISystemRoleProvider systemRoleNameProvider, string name)
Copy link
Member

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.

Copy link
Member

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

Copy link
Member Author

@hishamco hishamco Jan 18, 2025

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

https://github.com/dotnet/aspnetcore/blob/2bf439cdf50ed98a218e00c32a444f113a8d1b4a/src/Identity/Extensions.Core/src/RoleManager.cs

@MikeAlhayek
Copy link
Member

@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.

{
ValueTask<IEnumerable<IRole>> GetSystemRolesAsync();

ValueTask<IRole> GetAdminRoleAsync();
Copy link
Member

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

Copy link
Member Author

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()

Copy link
Member

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.

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 was working well because you was stored strings previously

Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

@hishamco hishamco Jan 18, 2025

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

Copy link
Member

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.

Copy link
Member Author

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

@hishamco hishamco requested a review from MikeAlhayek January 19, 2025 22:00
{
public static bool IsAdminRole(this ISystemRoleProvider systemRoleNameProvider, string name)
{
ArgumentException.ThrowIfNullOrEmpty(name, nameof(name));
Copy link
Member

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

Copy link
Member Author

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?

Copy link
Member

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)

Copy link
Member Author

@hishamco hishamco Jan 20, 2025

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

Copy link
Contributor

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)

@hishamco
Copy link
Member Author

Oops, last code suggestion break the build

Copy link
Member

@MikeAlhayek MikeAlhayek left a 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.

@Piedone
Copy link
Member

Piedone commented Jan 21, 2025

I wanted to wait until you two have finished discussing this, but now I see Georg also approved, so nothing to add.

@Piedone Piedone merged commit 0c5fb0e into main Jan 21, 2025
7 checks passed
@Piedone Piedone deleted the hishamco/system-roles branch January 21, 2025 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create System roles by default
4 participants