How to implement readonly access to fields depending on the users Role #4341
-
|
Hi, I am evaluatating Graphql as we plan to take over an existing Rest based node app and rewrite it in .NET. The one thing I can not find a solution or examples for is this: Given a simple class (e.g. from ef core) how can I limit the update for any single property (so not the object as a whole) to certain roles. Or to put it the other way round, how can I differentiate "seeing" a field from "changing" it depending on a role? Any link or explanatioon would be much appreciated ... or even a search term, maybe I just ask Google the wrong question :) Warmly, Christian |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
If you take a look at the Authorization documentation, you can annotate your properties or mutation methods using the type User {
name: String
ssn: SocialSecurityNumber @authorize(roles: "Administrator)
}
type Query {
user(id: ID): User
}Now only an administrator can query for the For modification you would have a mutation: type Mutation {
setUserSsn(userId: ID, ssn: SocialSecurityNumber): Payload @authorize(policy: "IsOwner")
}Now only users that fulfill the Hope this helps :) |
Beta Was this translation helpful? Give feedback.
If you take a look at the Authorization documentation, you can annotate your properties or mutation methods using the
[Authorize]attribute.There is no such thing as "hiding" fields or getters/setters.
You could for example have the following setup:
Now only an administrator can query for the
ssnfield on the user type.For modification you would have a mutation:
Now only users that fulfill the
IsOwnerpolicy can change thessnof a user.Hope this helps :)