Inherited roles¶
Table of contents
Introduction¶
An inherited role is a way to create a new role which infers permissions from two or more non-inherited roles. Once an inherited role is created, it can be treated as any other role i.e. can be given in X-Hasura-Role session variable.
Inherited roles are useful when you need to define multiple permission rules (may be overlapping) on schema objects and also for greater modularity in role management.
Note
This feature is currently accessible as an experimental feature and must be enabled.
This can be done either by setting the env var HASURA_GRAPHQL_EXPERIMENTAL_FEATURES
to inherited_roles or by providing the server flag --experimental-features
to inherited_roles.
See server config reference for info on setting the flag/env var.
Supported from
Inherited roles are supported for versions v2.0.0-alpha.4 and above.
Creating inherited roles¶
To add a new inherited role, edit the metadata/inherited_roles.yaml file adding the inherited role definition
like this:
- role_name: sample_inherited_role
role_set:
- user
- editor
Apply the metadata by running:
hasura metadata apply
You can add a inherited role using the add_inherited_role metadata API:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "add_inherited_role",
"args": {
"role_name":"sample_inherited_role",
"role_set":[
"user",
"editor"
]
}
}
How is the permission of the inherited role inferred?¶
A select permission is comprised of the following things:
- Columns accessible to the role
- Row selection filter
- Limit
- Allow aggregation
- Scalar computed fields accessible to the role
Note
Inherited roles can only combine SELECT permissions currently
Suppose there are two roles, role1 gives access to column C1 with row filter P1 and role2 gives access to columns C1 and C2 with row filter P2. Consider the following GraphQL query executed with an inherited role comprised of role1 and role2:
query {
T {
C1
C2
}
}
The above GraphQL query will be translated to the following SQL query.
select (case when (P1 or P2) then C1 else null end) as C1,
(case when P2 then C2 else null end) as C2
from T
where (P1 or P2)
The other parameters of the select permission will be combined in the following manner:
- Limit - Maximum of the limits will be the limit of the inherited role
- Allow aggregations - If any of the role allows aggregation, then the inherited role will allow aggregation
- Scalar computed fields - same as table column fields, as in the above example
Accessibility of a field for an inherited role¶
Accessibility of a field for an inherited role is defined as follows:
- When all the roles give access to a column
C, thenCwill always be accessible. - When not all, but some of the roles give access to the column
Cthen the value of the columnCwill be outputed when the OR ofP1,P2....P(n)istrueand when it evaluates tofalse, the value of the columnCwill benull, wherePis the row filter of the select permissions in which columnCis given access to. - When none of the roles give access to column
C, it won’t be accessible to the inherited role.
Examples¶
Let’s take the example of an users table with the following columns:
id- Int - Primary keyname- Textemail- Text
There are two roles defined namely employee and manager.
- User role - The user role will be able to access all columns of their row when the session variable
X-Hasura-User-Idis equal to theid. - Anonymous role - The anonymous role will be able to access only the
idandnamecolumns of all the users.
Let’s create a new inherited role called user_anonymous_inherited_role which inherits from the user and the anonymous roles.
- Executing the query as
userrole
POST /v1/graphql HTTP/1.1
Content-Type: application/json
X-Hasura-Role: user
X-Hasura-User-Id: 1
query {
users {
id
name
email
}
}
- Executing the query as
anonymousrole
POST /v1/graphql HTTP/1.1
Content-Type: application/json
X-Hasura-Role: anonymous
query {
users {
id
name
}
}
- Executing the query as
user_anonymous_inherited_rolerole
POST /v1/graphql HTTP/1.1
Content-Type: application/json
X-Hasura-Role: user_anonymous_inherited_role
X-Hasura-User-Id: 1
query {
users {
id
name
email
}
}
In the response of the query being executed with the user_anonymous_inherited_role role, there are 3 rows returned and if
we compare that to the queries executed as the user and anonymous roles, the results are unioned in the inherited
role. But some of the fields have null values despite the value in the database not being null. This can only happen
with inherited roles when a column doesn’t have permission in the particular row. In the above example, we see that the
email of “Bob” and “Sam” is null but a non null value for “Alice”, this is because the “Alice” row is executed as the
user role and the other rows are executed as the anonymous role which is why is why the value is null.
- Suppose we have two tables
usersandauthorsand similarly two rolesuserandauthorare defined. Theuserrole doesn’t have permission to query theauthorstable and theauthorrole doesn’t have permission to query theuserstable. With only theuserand theauthorrole, we won’t be able to construct a query which fetches data from both the tables. This can be solved by creating an inherited role out ofuserandauthorwhich can query both the tables in a single query.
POST /v1/graphql HTTP/1.1
Content-Type: application/json
X-Hasura-Role: user_authors_inherited_role
X-Hasura-User-Id: 1
query {
users {
id
name
email
}
authors {
id
name
followers
}
}
Present limitations¶
Currently, inherited roles are supported only for Postgres read queries and subscriptions. The following features are not supported for inherited roles yet:
- Mutations
- Actions
- Remote schemas