Extending a Role instance

Extend a role instance by adding permissions for a new resource.

Extending a Role instance

You can extend a role instance by using the add method.

const extended = role.add({
    resource: 'page',
    permissions: {
      base: 'crudl',
      custom: {
        suspend: false
      }
    }
}) // returns a new role instance

extended.can('page', 'create') // true
extended.can('page', 'suspend') // false

The add Method: Extending Role Permissions

The add method extends a role instance by adding permissions for a new resource. It takes an object with these properties:

  • resource (string): Name of the new resource.
  • permissions (object): Permissions for the resource.
    • base (string): CRUD permissions (e.g., 'crudl').
    • custom (object): Additional custom permissions.

Optional properties:

  • mutate (boolean, default: false): If true, modifies the existing role instead of returning a new instance.
  • noOverride (boolean, default: false): If true, merges new permissions with existing ones instead of overriding.

The method returns a new role instance by default, allowing immediate use of can method to check newly added permissions. This approach enables dynamic extension of role capabilities without altering the original role definition, supporting flexible and granular access control.