Create a new Role instance

Create a new role instance using the Role class.

Create a new Role instance

You can create a new role instance by using the Role class.

import { Role } from '@iamjs/core';

const role = new Role({
  name: 'editor',
  description: 'Can edit and publish content',
  meta: {
    department: 'content'
  },
  config: {
    article: {
      base: 'crudl',
      custom: {
        publish: true
      }
    },
    user: {
      base: '-r---',
      custom: {
        editProfile: false
      }
    }
  }
});

Let's break down the Role constructor parameters:

  1. name (string): The unique identifier for the role (e.g., 'editor').
  2. description (string): A brief explanation of the role's purpose.
  3. meta (object): Optional metadata for additional role information.
  4. config (object): Defines permissions for different resources.

The config object is where you specify permissions:

  • Each key represents a resource (e.g., 'article', 'user').
  • base: A string representing basic CRUD permissions:
    • 'c': Create
    • 'r': Read
    • 'u': Update
    • 'd': Delete
    • 'l': List
    • '-': Denies the permission
  • custom: An object for additional, specific permissions.

In this example:

  • The 'editor' role has full CRUD permissions for 'article' resources, plus a custom 'publish' permission.
  • For 'user' resources, it only has 'read' permission, and explicitly cannot 'editProfile'.

This flexible structure allows you to create roles with precise, granular permissions tailored to your application's needs.