For every role that you create, Hasura automatically publishes a different GraphQL schema that represents the
right queries, fields, and mutations that are available to that role.
Dynamic session variables
-------------------------
When you create a permission, or an access control rule, the permission rule itself needs access to some variables
that are derived from the request itself. Let's refer to these as *session variables*.
For example: If a user makes a request, the session token maps to a ``user-id``. This ``user-id`` can be used in
a permission to show that inserts into a table are only allowed if the ``user_id`` column has a value equal to that
of ``user-id``, the session variable.
When you are constructing permission rules, however, there might be several variables that represent the business logic
of having access to data. For example, if you have a SaaS application, you might restrict access based on a ``client_id``
variable. If you want to provide different levels of access on different devices you might restrict access based on a
``device_type`` variable.
Hasura allows you to create permission rules that can use any dynamic variable that is a property of the request.
All your dynamic variables must follow the naming convention ``X-Hasura-*``.
Examples:
..list-table::
:header-rows:1
:widths:20 10 20 50
* - Example
- Role
- Condition
- Permission expression
* - Allow access to user's own row
-``user``
-``user_id`` column is equal to ``session-user-id`` from a request
-
..code-block:: json
{
"user_id": {
"_eq": "X-Hasura-User-Id"
}
}
* - Allow project admins access to anything that belongs to the project
-``project-admin``
-``project_id`` column is equal to ``project-id`` of the "session user"
-
..code-block:: json
{
"project_id": {
"_eq": "X-Hasura-Project-Id"
}
}
Modelling Roles in Hasura
-------------------------
General guidelines for modelling roles in Hasura.
Roles are typically be modelled in two ways:
1.**Hierarchical roles**: Access scopes are nested depending on available roles. `Roles in Github for organisations <https://help.github.com/en/articles/managing-peoples-access-to-your-organization-with-roles>`_
is a great example of such modelling where access scopes are inherited by deeper roles: