Back to Graphql Engine

Column Presets

docs/docs/auth/authorization/permissions/column-presets.mdx

2.49.32.3 KB
Original Source

import Thumbnail from '@site/src/components/Thumbnail'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Column Presets

Introduction

While this is not strictly a permission configuration, defining role-based column presets for insert and update operations on any column automatically removes the ability to manually insert or update it for that role.

The respective fields will also be removed from the generated GraphQL schema for that role.

This setup very useful in avoiding sensitive user information being sent in the request and instead leveraging session variables or static data for that information.

<Tabs groupId="user-preference" className="api-tabs"> <TabItem value="console" label="Console">

You can define column presets for either insert or update operations in the Hasura Console in Data -> [table] -> Permissions -> insert / update as follows:

<Thumbnail src="/img/auth/set_column_preset_console_2.10.1.png" alt="Set column preset" width="1000px" /> </TabItem> <TabItem value="cli" label="CLI">

You can define column presets for table columns in the metadata > databases -> [database-name] -> tables -> [table-name].yaml, eg: public_users.yaml:

yaml
table:
  name: users
  schema: public
insert_permissions:
  - role: user
    permission:
      check: {}
      set:
        id: x-hasura-User-Id
      columns:
        - id

Apply the Metadata by running:

bash
hasura metadata apply
</TabItem> <TabItem value="api" label="API">

You can define column presets with either the insert or update permissions Metadata API. Example using a Postgres db:

http
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin

{
  "type": "pg_create_insert_permission",
  "args": {
    "table": {
      "name": "users",
      "schema": "public"
    },
    "role": "user",
    "permission": {
      "check": {},
      "columns": [
        "id"
      ],
      "set": {
        "id": "x-hasura-user-id"
    }
  },
    "source": "default"
  }
}
</TabItem> </Tabs>