# Module Entitlements

Canonical page: https://www.adaptabletools.com/docs/handbook-permissioning-modules

- Every Module in AdapTable can be given one of 3 Access Levels:
  - `ReadOnly`
  - `Hidden`
  - `Full`
- Module Entitlements can be provided in 2 ways:
  - as a 'hard-coded list'
  - via a function
- Additionally a different DefaultAccessLevel can be specified in preference to the default value of `Full`

Every Module in AdapTable has an Entitlement `AccessLevel`.

Any Module not given an **explicit** Entitlement will use that provided by the `defaultAccessLevel` property

In addition any Object-level Entitlements will take priority over the Module Entitlement

This is set by the `moduleEntitlements` property of [Entitlement Options](https://www.adaptabletools.com/docs/technical-reference-permissioning/index.md) and can provided in 2 ways:

- with a 'hard-coded' list
- via a function invoked by AdapTable as required

### `moduleEntitlements`

Provide Entitlements for each Module

  [`Entitlement[]`](https://www.adaptabletools.com/docs/reference/entitlement.md)

This property is used to set the Entitlement for any Module in AdapTable.

This will **override** the default Entitlement provided by `defaultAccessLevel`

It is defined as follows:

```ts
moduleEntitlements?:
    | Entitlement[]
    | ((entitlementContext: EntitlementContext) => AccessLevel | undefined);
```

As can be seen it offers 2 ways to provide Entitlements:

- Hard Coded Value that is set once for all use cases
- Function invoked by AdapTable each time an Entitlement is required; it receives context and returns an [`AccessLevel`](https://www.adaptabletools.com/docs/reference/accesslevel.md)

## List-based Module Entitlements

The most straightforward way of providing Module Entitlements is via a list.

The list includes every Entitlement for which the defaultAccessLevel is not suitable.

**Example: Entitlements: By List**

Managing Entitlements through a 'hard-coded' list

- In this example we have provided Entitlements using a **List** (see below) as follows:
  - The [Alerts](https://www.adaptabletools.com/docs/handbook-alerting/index.md) Module has a `Readonly` Entitlement - Alerts can be triggered (change `GitHub Stars` to under 500) but not edited, deleted or created
  - The [Format Column](https://www.adaptabletools.com/docs/handbook-column-formatting/index.md) Module has a `Readonly` Entitlement - 3 Format Columns are rendered but cannot be edited, deleted or created
  - The [Custom Sort](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md) Module has a `Hidden` Entitlement - it is invisible in all UI elements
### See how the Permissions are set as a List

```ts
entitlementOptions: {
  moduleEntitlements: [
    {
      adaptableModule: 'FormatColumn',
      accessLevel: 'ReadOnly',
    },
    {
      adaptableModule: 'Alert',
      accessLevel: 'ReadOnly',
    },
    {
      adaptableModule: 'CustomSort',
      accessLevel: 'Hidden',
    },
  ],
},
```

- Open the Settings Panel and note that Format Columns and Alert objects are disabled and Custom Sort is missing
- Fork the example and change the Entitlement of Alert to `Hidden`

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'List-Based Entitlements',

  entitlementOptions: {
    moduleEntitlements: [
      {
        adaptableModule: 'FormatColumn',
        accessLevel: 'ReadOnly',
      },
      {
        adaptableModule: 'Alert',
        accessLevel: 'ReadOnly',
      },
      {
        adaptableModule: 'CustomSort',
        accessLevel: 'Hidden',
      },
    ],
  },

  initialState: {
    Dashboard: {
      ModuleButtons: ['Alert', 'FormatColumn'],
    },
    Theme: {CurrentTheme: 'dark'},
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-github-stars',
          MessageType: 'Warning',
          Scope: {
            ColumnIds: ['github_stars'],
          },
          Rule: '[github_stars] < 1000',
          AlertProperties: {
            HighlightCell: true,
            DisplayNotification: true,
          },
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-week_issue_change',
          Style: {
            ForeColor: 'var(--ab-color-palette-19)',
          },
          Scope: {
            ColumnIds: ['week_issue_change'],
          },
          Rule: '[week_issue_change] > 0',
        },
        {
          Name: 'formatColumn-week_issue_change',
          Style: {
            ForeColor: 'var(--ab-color-palette-30)',
          },
          Scope: {
            ColumnIds: ['week_issue_change'],
          },
          Rule: '[week_issue_change] < 0',
        },
        {
          Name: 'style-all',
          Style: {
            BackColor: 'var(--ab-color-palette-26)',
            ForeColor: 'var(--ab-color-palette-17)',
          },
          Scope: {
            All: true,
          },
          Rule: '[language] = "HTML"',
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_projects',
            'has_pages',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Function-based Module Entitlements

An alternative way of providing Module Entitlements is via a JavaScript function.

This function will be invoked by AdapTable each time an Entitlement needs to be evaluated.

This is a more powerful and flexible approach than a [hard coded value](#list-based-module-entitlements) as it can return different `AccessLevel` values depending on the User, or other information.

You can use this option to connect to an external permissioning or entitlement service if required

### Providing Module Entitlements by Function

The relevant definition of the `moduleEntitlements` property is:

```js
((entitlementContext: EntitlementContext) => AccessLevel | undefined);
```

As can be seen, the function receives an object of type [`EntitlementContext`](https://www.adaptabletools.com/docs/reference/entitlementcontext.md) which contains the Module being permissioned and the `defaultAccessLevel` that applies if the function returns `undefined`. It should return an [`AccessLevel`](https://www.adaptabletools.com/docs/reference/accesslevel.md) or `undefined`.

The EntitlementContext property is defined as follows:

| Property | Type | Description |
| --- | --- | --- |
| [adaptableModule](https://www.adaptabletools.com/docs/reference/entitlementcontext.md#adaptablemodule) | [`AdaptableModule`](https://www.adaptabletools.com/docs/reference/adaptablemodule.md) | Module being entitled |
| [defaultAccessLevel](https://www.adaptabletools.com/docs/reference/entitlementcontext.md#defaultaccesslevel) | [`AccessLevel`](https://www.adaptabletools.com/docs/reference/accesslevel.md) | AccessLevel that applies if this function returns `undefined` — the module&#39;s<br/>`getDefaultAccessLevel()` (global `defaultAccessLevel`, or a module-specific override). |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/entitlementcontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

```ts {4,5}
// Call a permission service on External Server to get Entitlements
const adaptableOptions: AdaptableOptions = {
  entitlementOptions: {
    moduleEntitlements: (entitlementContext: EntitlementContext) => {
      return getPermission(
        entitlementContext.adaptableModule,
        entitlementContext.userName
      );
    },
  },
};
```

- Return `undefined` to use that Module's default Access Level (`EntitlementContext.defaultAccessLevel`)
- That default is normally [defaultAccessLevel](https://www.adaptabletools.com/docs/handbook-permissioning-default-access-level/index.md), except for Modules such as [State Management](https://www.adaptabletools.com/docs/dev-guide-adaptable-state-management/index.md) which stay `Hidden` unless entitled explicitly

**Example: Entitlements: By Function**

Managing Entitlements through a JavaScript function

- This example uses a function to define permissions for Modules
- Based on the user-access-level, they can have `Full`, `ReadOnly` or `Hidden` access
### Expand to see the moduleEntitlements function

```ts
entitlementOptions: {
  defaultAccessLevel: 'Hidden',
  moduleEntitlements: (entitlementContext: EntitlementContext) => {
    // check if the user has admin access
    if (isUserAdmin(user)) {
      return 'Full';
    }
    const fullModules: AdaptableModule[] = ['FormatColumn', 'CustomSort'];
    if (fullModules.includes(entitlementContext.adaptableModule)) {
      return 'Full';
    }
    const readOnlyModules: AdaptableModule[] = [
      'CellSummary',
      'Alert',
      'Dashboard',
      'ToolPanel',
      'SettingsPanel',
    ];
    if (readOnlyModules.includes(entitlementContext.adaptableModule)) {
      return 'ReadOnly';
    }
    // the rest are hidden for normal users
    return 'Hidden';
  },
},
```

- Fork the example and change the entitlement function to make Alert module visible.

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableModule,
  AdaptableOptions,
  EntitlementContext,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

const user = {
  id: '1',
  accessLevel: 'normal-user',
};

const isUserAdmin = (user: {id: string; accessLevel: string}) =>
  user.accessLevel === 'admin';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Function-provided Entitlements',

  entitlementOptions: {
    defaultAccessLevel: 'Hidden',
    moduleEntitlements: (entitlementContext: EntitlementContext) => {
      // check if the user has admin access
      if (isUserAdmin(user)) {
        return 'Full';
      }

      const fullModules: AdaptableModule[] = ['FormatColumn', 'CustomSort'];
      if (fullModules.includes(entitlementContext.adaptableModule)) {
        return 'Full';
      }

      const readOnlyModules: AdaptableModule[] = [
        'CellSummary',
        'Alert',
        'Dashboard',
        'ToolPanel',
        'SettingsPanel',
      ];

      if (readOnlyModules.includes(entitlementContext.adaptableModule)) {
        return 'ReadOnly';
      }

      // the rest are hidden for normal users
      return 'Hidden';
    },
  },

  initialState: {
    Dashboard: {
      ModuleButtons: ['FormatColumn'],
    },
    Theme: {CurrentTheme: 'dark'},
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-all',
          Scope: {
            All: true,
          },
          Rule: '[language] = "TypeScript"',
          Style: {
            BackColor: '#f2f2f2',
            ForeColor: 'var(--ab-color-palette-17)',
          },
        },
        {
          Name: 'formatColumn-week_issue_change',
          Style: {
            ForeColor: 'var(--ab-color-palette-19)',
          },
          Scope: {
            ColumnIds: ['week_issue_change'],
          },
          Rule: '[week_issue_change] > 0',
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_projects',
            'has_pages',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Additional Module Entitlement Rules

For some AdapTable Modules providing an Entitlement of `Full` or `ReadOnly` is insufficient to ensure availability.

Additional conditions are required - supplied typically in [Adaptable Options](https://www.adaptabletools.com/docs/technical-reference-adaptable-options/index.md):

- for [Team Sharing](https://www.adaptabletools.com/docs/handbook-team-sharing/index.md) set `enableTeamSharing` property in [Team Sharing Options](https://www.adaptabletools.com/docs/technical-reference-team-sharing/index.md) to _true_

- in [ipushpull](https://www.adaptabletools.com/docs/integrations-ipushpull/index.md) the `ippConfig` property must be provided in [ipushpull Plugin Options](https://www.adaptabletools.com/docs/integrations-ipushpull/index.md)

- in [OpenFin](https://www.adaptabletools.com/docs/integrations-openfin/index.md) the OpenFin configuration needs to be supplied

## Default Module ReadOnly Behaviour

The `ReadOnly` behaviour for an Entitlement differs by Module and use case.

As a general rule, if the Module performs an action but does not contain an object (e.g. [Smart Edit](https://www.adaptabletools.com/docs/handbook-editing-smart-edit/index.md), [Bulk Update](https://www.adaptabletools.com/docs/handbook-editing-bulk-update/index.md) or [Cell Summary](https://www.adaptabletools.com/docs/handbook-summarising/index.md)) the behaviour is the same as for `Hidden`.

If the Module contains a collection of objects (e.g. [Format Columns](https://www.adaptabletools.com/docs/handbook-column-formatting/index.md), [Shortcuts](https://www.adaptabletools.com/docs/handbook-editing-shortcut/index.md), [Custom Sort](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md)), these objects are actionable as needed (and fully viewable by the user) but cannot be edited, deleted or suspended; nor can new ones be added.

However items can be selected in Modules where Users are able to make a selection (e.g. [Layout](https://www.adaptabletools.com/docs/handbook-layouts/index.md) or [Export](https://www.adaptabletools.com/docs/handbook-exporting/index.md)).

Objects provided in [Initial Adaptable State](https://www.adaptabletools.com/docs/dev-guide-adaptable-state-initial-state/index.md) are fully **available** in `ReadOnly` mode, as is the [Adaptable API](https://www.adaptabletools.com/docs/technical-reference-adaptable-api/index.md)

There are some special cases to be aware of, here is full `ReadOnly` Entitlement behaviour for each Module:

| Module                                                                                    | Read Only Behaviour                                                                                                                                   |
| ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Alerts](https://www.adaptabletools.com/docs/handbook-alerting/index.md)                                    | Alerts trigger as normal but users cannot manage or suspend Alert Definitions; Alerts may be still cleared however.                                   |
| [Bulk Update](https://www.adaptabletools.com/docs/handbook-editing-bulk-update/index.md)                    | Same as `Hidden` Access Level                                                                                                                         |
| [Calculated Column](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md)                | Calculated Columns can be added / removed from Layouts and will be applied but Users cannot manage or suspend them                                    |
| [Cell Summary](https://www.adaptabletools.com/docs/handbook-summarising/index.md)                           | Same as `Hidden` Access Level                                                                                                                         |
| [Charts](https://www.adaptabletools.com/docs/handbook-charts/index.md)                                      | Same as `Full` Access Level (charts are managed by AG Grid)                                                                                           |
| [Column Filters](https://www.adaptabletools.com/docs/handbook-column-filter/index.md)                       | The Filter Bar cannot be made hidden / visible but Column Filters can still be created                                                                |
| [Column Info](https://www.adaptabletools.com/docs/ui-settings-panel-column-info/index.md)                   | Same as `Hidden` Access Level                                                                                                                         |
| [Comments](https://www.adaptabletools.com/docs/handbook-comments/index.md)                                  | Existing Comments - either previously created or provided by other users are visible - but new Comments cannot be added                               |
| [Custom Sort](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md)                         | Custom Sorts will be applied but Users cannot manage or suspend them                                                                                  |
| [Dashboard](https://www.adaptabletools.com/docs/ui-dashboard/index.md)                                      | The Dashboard cannot be configured (but can be expanded / hidden / floated etc.). Individual Toolbars cannot be closed or re-ordered.                 |
| [Data Change History](https://www.adaptabletools.com/docs/handbook-monitoring-data-change-history/index.md) | Data Change History will display and work (if active) but it cannot be activated, inactivated or suspended                                            |
| [Data Import](https://www.adaptabletools.com/docs/handbook-importing/index.md)                              | Same as `Full` Access Level (Data Import is an action, not a collection of objects)                                                                   |
| [Data Sets](https://www.adaptabletools.com/docs/handbook-data-sets/index.md)                                | Predefined Data Sets can be selected - and the DataSetSelected Event will fire - but Users cannot manage or suspend them                              |
| [Export](https://www.adaptabletools.com/docs/handbook-exporting/index.md)                                   | Reports can be run but Users cannot manage or suspend them                                                                                            |
| [FDC3](https://www.adaptabletools.com/docs/handbook-fdc3/index.md)                                          | Same as `Hidden` Access Level                                                                                                                         |
| [Flashing Cell](https://www.adaptabletools.com/docs/handbook-flashing-cell/index.md)                        | Flashing Cell Definitions will be applied but Users cannot manage or suspend them                                                                     |
| [Format Column](https://www.adaptabletools.com/docs/handbook-column-formatting/index.md)                    | Format Columns will be applied but Users cannot manage or suspend them                                                                                |
| [FreeText Column](https://www.adaptabletools.com/docs/handbook-freetext-column/index.md)                    | FreeText Columns can be added / removed from Layouts and visible in the Grid (with cells fully editable) but Users cannot manage or suspend them      |
| [Grid Filter](https://www.adaptabletools.com/docs/handbook-grid-filter/index.md)                            | Existing Named Queries can be run in the Grid Filter but new Named Queries cannot be saved                                                            |
| [Grid Info](https://www.adaptabletools.com/docs/ui-settings-panel-grid-info/index.md)                      | Same as `Hidden` Access Level                                                                                                                         |
| [Layout](https://www.adaptabletools.com/docs/handbook-layouts/index.md)                                     | Predefined Layouts can be selected but Users cannot actively manage or suspend them; Column changes can be made at runtime but they are not persisted |
| [Named Query](https://www.adaptabletools.com/docs/handbook-named-queries/index.md)                          | Named Queries can still be referenced in Expressions but Users cannot manage or suspend them                                                          |
| [Notes](https://www.adaptabletools.com/docs/handbook-notes/index.md)                                        | Notes in Initial State are visible, but Notes cannot be added, edited or deleted                                                                      |
| [Plus Minus](https://www.adaptabletools.com/docs/handbook-editing-plus-minus/index.md)                      | Plus Minus Nudges will be applied but Users cannot manage or suspend them                                                                             |
| [Quick Search](https://www.adaptabletools.com/docs/handbook-quick-search/index.md)                          | Same as `Hidden` Access Level                                                                                                                         |
| [Settings Panel](https://www.adaptabletools.com/docs/ui-settings-panel/index.md)                            | Same as `Full` Access Level — the Settings Panel itself is not configurable at runtime (unlike Dashboard / Tool Panel)                                |
| [Shortcuts](https://www.adaptabletools.com/docs/handbook-editing-shortcut/index.md)                         | Shortcuts will be applied but Users cannot manage or suspend them                                                                                     |
| [Smart Edit](https://www.adaptabletools.com/docs/handbook-editing-smart-edit/index.md)                      | Same as `Hidden` Access Level                                                                                                                         |
| [State Management](https://www.adaptabletools.com/docs/dev-guide-adaptable-state-management/index.md)       | The 'Retrieve State' dropdowns operate but the 'Delete State' button is disabled                                                                      |
| [Status Bar](https://www.adaptabletools.com/docs/ui-status-bar/index.md)                                    | Status Bar Panels can be used but the Status Bar cannot be configured                                                                                 |
| [Styled Column](https://www.adaptabletools.com/docs/handbook-styled-column-overview/index.md)               | Styled Columns will be rendered but Users cannot manage or suspend them                                                                               |
| [System Status](https://www.adaptabletools.com/docs/ui-tutorial-using-toast-notifications/index.md)         | System Status Messages can be viewed, but the 'Delete' buttons are disabled                                                                           |
| [Team Sharing](https://www.adaptabletools.com/docs/handbook-team-sharing/index.md)                          | Users may import existing Shared items, but are not able to create or delete them                                                                     |
| [Theme](https://www.adaptabletools.com/docs/handbook-theming/index.md)                                      | The Theme cannot be changed                                                                                                                           |
| [Tool Panel](https://www.adaptabletools.com/docs/ui-tool-panel/index.md)                                    | The Adaptable Tool Panel cannot be configured. Individual ToolPanels can be opened and closed but not hidden or re-ordered.                           |

### Plugin-based Modules

| Plugin                                                         | Read Only Behaviour           |
| -------------------------------------------------------------- | ----------------------------- |
| [ipushpull](https://www.adaptabletools.com/docs/integrations-ipushpull/index.md) | Same as `Hidden` Access Level |
| [OpenFin](https://www.adaptabletools.com/docs/integrations-openfin/index.md)     | Same as `Hidden` Access Level |
| [interop.io](https://www.adaptabletools.com/docs/integrations-interop/index.md)  | There are no Entitlements     |
