can-it.github.io

@can-it/operators-nested

This package provides a simple way to define and work with hierarchical relationships between values, typically resources. It is used in conjunction with @can-it/core or other can-it utilization packages. npm version

Examples

👉 Source code

Concepts

The Nested operator is designed to handle nested values. Here are some use cases that illustrate how this concept works:

RI definition:

Use cases and corresponding permissions:

Usage

NestedPattern {
  separator: string; // default is `'::'`
  wildcard: string; // default is `'*'`
  resourceRegex: string; // default is `'[\\w-]*'`
}

import { NestedGenerator } from '@can-it/operators-nested';

const generator = new NestedGenerator();
// The input will be passed with the `requestCode` and `permissionCode` in order.
generator.transform('orgs'); // `'orgs'`
generator.transform('orgs', '', 'users'); // `'orgs::*::users'` (an empty string will be processed as a wildcard signal).
generator.transform('orgs', 'any-org-id-9585'); // `'orgs::any-org-id-9585'`
generator.transform('orgs', 'any-org-id-9585', 'users', 'user-id-73jg4k'); // `'orgs::any-org-id-9585::users::user-id-73jg4k'`
import { NestedComparator } from '@can-it/operators-nested';

const comparator = new NestedComparator();
// The input will be passed with the `requestCode` and `permissionCode` in order.
comparator.isAllowed('orgs', 'orgs'); // true
comparator.isAllowed('orgs::any-org-id-9585', 'orgs'); // true
comparator.isAllowed('orgs::any-org-id-9585::users::user-id-73jg4k', 'orgs'); // true
comparator.isAllowed('orgs', 'orgs::*'); // true
comparator.isAllowed('orgs::any-org-id-93485', 'orgs::*'); // true
comparator.isAllowed('orgs::any-org-id-93485::users', 'orgs::*'); // false
comparator.isAllowed('orgs::any-org-id-93485::users:any-user-id-js934n', 'orgs::*'); // false
comparator.isAllowed('orgs::any-org-id::users', 'orgs::*::users'); // true
comparator.isAllowed('orgs::any-org-id::users::any-user-id-39485', 'orgs::*::users'); // true
comparator.isAllowed('orgs::any-org-id::products::any-product-id-39485', 'orgs::*::users'); // false
comparator.isAllowed('orgs::any-org-id::users::any-user-id-39485:products', 'orgs::*::users'); // true