-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
Can I authorize per multiple policies ? #6
Comments
Not at present, no. You can have a policy like “TeacherOrAdmin”. |
I see that might be a good solution. |
If I were to add multiple policy support, it would probably function like the .NET Core one does, in an |
mmm ... I believe it is more like |
I understand. If I were to implement it I would just prefer it to behave like ASP.NET Core does to avoid confusion. |
@jalchr I wrote my own extension and validation rule to make this happen. Based on the documentation here https://graphql-dotnet.github.io/docs/getting-started/authorization The usage ends up like this:
The RequireRole can be written as an extension method like this. Which adds the roles comma separated as metadata on the field.
Then we can add our own validation rule like this
And use dependency injection to add it as an IValidationRule (I'm, using Autofac here).
Now we can create another extension method that validates the roles against the users claims
|
If you need differing policies based on role - then MetaData as above is the way to go. If you just need to give multiple users with different roles access to the same stuff, then |
Another workaround is to implement custom
And an extension for finding errors:
Then register it in the IOC:
|
@Mousavi310 Why do you use |
Just looks like a stylistic choice - break is going to end the loop and return the initialized value of |
Taking a second look - in that sample it should really be:
The way that's written a case like this would not be correct:
|
Then I don't understand the meaning of the method at all - SatisfiesAtLeastOnePolicyAsync. Why does it break on the first false result? |
You read more closely than I did :) - I failed to read the method name "SatisfiesAtLeastOnePolicyAsync". |
Exactly. This example is misleading. |
Initial problem can be solved by role-based auth - graphql-dotnet/graphql-dotnet#3067 . "Admin" and "Teacher" from initial post look more like roles, not policies. ping @Shane32 |
Agree @sungam3r . As of GraphQL v5, roles can be applied to the GraphQL schema rather than only policies, which would work in the method requested. (Requires implementation by the authorization rule within this repository, which has not been done here yet.) I can also explain how authorization works in ASP.Net Core, but I am not sure how it applies to this repository. Typical ASP.Net Core authorization rules would either apply a single policy ** or ** one or more roles directly. A policy typically contains one or more requirements, one of which could be "is a member of at least one role in the supplied list". However, custom authorization requirements can be written for any desired behavior. Links:
|
Using the GraphQLAuthorize attribute, can I apply multiple policies at once ...
like ["Admin", "Teacher"] .
Then any user that has any of those claims get authorized.
The text was updated successfully, but these errors were encountered: