Permissions for functions
I would like to start a discussion on function permissions. Presently they are very limited
Consider the following 2 scenarios
Scenario 1
Search function based on https://docs.hasura.io/1.0/graphql/manual/queries/custom-functions.html#example-fuzzy-match-search-functions Assume 2 roles, "User" and "Anonymous" user can view all fields in property, however anonymous can only see city state zip
Results Role User - Works as intended Role Anonymous - Problematic - This function allows anonymous users to search on other address providing insight to hidden data
Scenario 2
Function that returns a list of calendar events. Again 2 roles, with "User" being able to see all events and "Anonymous" only being able to see future events ( eventtime > now() ) Related #2882
Current potential workaround
design 1 add a field 'anonymous_allowed' to the return table and query on that design 2 2 functions, one for each Role with custom logic 2 empty tables, identical, one for each Role
Possible solutions:
- Being able to enable/disable a function based on role is a simple and effective option
- Being able to pass in the role for logic internal also solves more complicated solutions
@andrewalex,
Hasura does not have an option to set permission for functions. The permissions will be inherited from the table.
Scenario 1
- In the user table permissions, for the anonymous role, allow access only to the fields you want (city, state, zip)
- Create a view in the user table, with the id and all the other fields you didn't use for the anonymous role. In the permissions for this view, for the user role, allow access to all fields.
- In the user table create a relationship between the user table and the created view. Call the relationship private
query {
user {
id
city
state
zip
private {
email
}
}
}
When running the above query as anonymous, all fields in the private object will be null, when running as user all fields will be populated.
Scenario 2
- Create a view DateFilter for example
CREATE view public.date_filter as SELECT id, created_at, NOW() as now FROM public.events
- In the Events table create a relationship with the created view and call it validation
- In the Events table permission, for the user role select the option without any checks. For the anonymous role select the option with custom checks and add the following check
{validation: {created_at: {_cgte: now}}}
Role user will have access to all events Role anonymous will have access to events greater than or equal now
Unless I'm misunderstanding, both your solutions seem to be ignoring the use of a function. For the purposes of the issue, assume the logic is sufficiently complex to require the use of function.
Scenario 1. How does this stop a function from querying fields based on a users role?
Scenario 2 This seems to just be close to what I indicate as 'workaround design 1'.
I just ran into this. https://docs.hasura.io/1.0/graphql/manual/guides/auditing-tables.html Is it possible to make it so we can do current_setting('hasura.user') inside functions as well?
@andrewalex
We'll support functions to accept session information (all X-Hasura-* variables) through an argument. Please refer to https://github.com/hasura/graphql-engine/issues/2322
@rakeshkky Awesome. the second thing that might be useful is the ability to enable/disable functions based on role. (tho we can just put a check in the function to get around that)
@marionschleifer is actions an appropriate label? My understanding is actions are mutates only and this is for queries? However #3143 could be considered at a solution for this.
@rakeshkky Unless I'm mistaken, it seems that this has been implemented.
Yes, you can access session_variables in functions. The ability to hide a function field can be tracked in a separate issue if that's allright.