hasura / hasura/graphql-engine
feature request: Conditional Permissions Dependent on Session Variables
- Dominant language
- TypeScript
- Stars
- 32.1k
- Forks
- 3k
- PR merge metrics
- PR metrics pending
Description
Let's assume I have a table defined as follows:
```sql
CREATE TABLE cars(id TEXT, make TEXT, model TEXT, color TEXT);
```
Where column `color` can take the values `red`, `green`, or `blue`.
And let's assume we have two users with the following session variables/auth claims:
```json
{
"X-Hasura-User-Id": "user1",
"X-Hasura-Role": "user",
"X-Hasura-Color": "red",
"X-Hasura-Allow-Select-All-Colors": true
}
```
```json
{
"X-Hasura-User-Id": "user2",
"X-Hasura-Role": "user",
"X-Hasura-Color": "red",
"X-Hasura-Allow-Select-All-Colors": false
}
```
I want to allow `user1` to retrieve all rows from table `cars` regardless of color, but I want to ensure that `user2` can only select cars whose color is `red`.
Currently, there is no way for me to do this without creating an entirely distinct role for `user1`.
Ideally, I could create a custom permissions check for role `user` like so:
```json
{
"_or": [
{
"X-Hasura-Allow-Select-All-Colors": {
"_eq": true
},
},
{
"color": {
"_eq": "X-Hasura-Color"
}
}
]
}
```
I imagine this exact syntax may not work, as it would not be compatible with tables that have one or more columns whose names are prefixed by `X-Hasura-`.
Perhaps instead, there could be some `_`-prefixed operator that can extract a value from session variables, similar to how the `_exists` operator works. For example:
```json
{
"_or": [
{
"_sesh": {
"_name": "X-Hasura-Allow-Select-All-Colors",
"_eq": true
}
},
{
"color": {
"_eq": "X-Hasura-Color"
}
}
]
}
```
Contributor guide
Assessment
This issue has not been assessed yet.