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.
The Nested operator is designed to handle nested values. Here are some use cases that illustrate how this concept works:
RI definition:
orgs::Y::users::X
orgs::Y::users::X::products::Z
orgs::Y::payments::payment-id-94kd03
orgs::Y::settings::setting-id-j8e84
Use cases and corresponding permissions:
orgs::*::settings
orgs::*::users
orgs::Y::users
orgs::Y::users::X
orgs::*::users::*
orgs::Y::users::*
orgs::*::users::*::products
orgs::*::users::X::products
orgs::Y::users::X::products::Z
NestedGenerator
is an operator used to generate code based on the initialized NestedPattern, which has the following options: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'`
NestedComparator
is an operator used to check whether a request can match the given permission.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