Skip to content

setPermissions

The setPermissions method sets multiple permissions for the Guantr instance in one go. This method replaces the existing permissions with a new array of permissions.

Usage

You can use the setPermissions method to initialize or update the permissions for the Guantr instance with a predefined set of permissions.

Example

ts
import { createGuantr } from 'guantr';

// Create a new Guantr instance
const guantr = createGuantr();

// Define permissions
const permissions = [
  { action: 'read', resource: 'posts', condition: null, inverted: false },
  { action: 'write', resource: 'posts', condition: { title: { equals: 'Special Title' } }, inverted: false },
  { action: 'delete', resource: 'posts', condition: null, inverted: true }
];

// Set permissions
guantr.setPermissions(permissions);

// Check permissions
const canReadPosts = guantr.can('read', 'posts'); // true
const canWriteSpecialPosts = guantr.can('write', ['posts', { title: { equals: 'Special Title' } }]); // true
const canDeletePosts = guantr.cannot('delete', 'posts'); // true (because 'delete' is denied)

In this example:

  • permissions: An array of permission objects where each object defines an action, resource, condition, and whether the permission is inverted (denied).
    • { action: 'read', resource: 'posts', condition: null, inverted: false }: Allows read access to the posts resource.
    • { action: 'write', resource: 'posts', condition: { title: { equals: 'Special Title' } }, inverted: false }: Allows write access to the posts resource if the title is 'Special Title'.
    • { action: 'delete', resource: 'posts', condition: null, inverted: true }: Denies delete access to the posts resource.

References

Signature

ts
setPermissions(permissions: GuantrPermission<Meta, Context>[]): void;

Parameters

  • permissions (GuantrPermission<Meta, Context>[]): An array of permissions to set. Each permission object contains:
    • action (string): The action to be allowed or denied.
    • resource (string): The resource associated with the action.
    • condition (GuantrCondition | null): The condition that must be met for the permission to apply. If null, the permission applies without conditions.
    • inverted (boolean): Whether the permission is denied (true) or allowed (false).

Returns

  • void: This method does not return any value. It updates the permissions internally within the Guantr instance.