How to add authorization to filter nested data
I'm running a graphql query with the following filter
but I want this query to be possible only for a specific role
11 Replies
🤔 could you tell me about why you want that query to be possible only for a specific role? That sounds like a strange requirement to me.
I have the following role [:owner, :team_lead, :member]
:owner can filter by any team_id, :team_lead only by the assigned team_ids, :member cannot filter at all
My question is why can't they filter?
If they can see the data, why would you stop them from filtering it?
this is the solution I came up with, it will make more sense reading it
Gotcha, so I would typically model this with a policy, but that works too
something like that
but this fully authorize or not an action, it does not control its filters
which is fine, probably my solution make sense, because trying to encode this pattern in some macro would create too much complexity for its outcome
this is incorrect 😄
Those policies will be applied as a filter
what do you mean, I don't understand
If you have those policies on your resource
it will not forbid the action
it will filter the underlying data when you read
Meaning it doesn't matter how someone filters data, they will just not be able to see things they aren't allowed to see
https://hexdocs.pm/ash/policies.html#read-actions-and-filtering-behavior
in the block you provided I don't understand how it prevents to filter by a specific field.
or, I don't see any reference to a specific field
Right, it doesn't prevent filtering
but neither does this:
What I'm saying is that preventing filtering is not a safe way to do authorization
You instead attach a filter that allows you to see only what you can see, and then it doesn't matter if you filter by any given field
because the query only returns what it should return