Options
All
  • Public
  • Public/Protected
  • All
Menu

ts-can

Index

Type aliases

Abilities

Abilities: {}

Represents a set of abilities, where each key is a string representing the ability name, and the corresponding value is a boolean indicating whether the ability is granted or not.

Type declaration

  • [key: string]: boolean

Checks

Checks: {}

Represents a set of checks, where each key is a string representing the check name, and the corresponding value is a function that takes a target and returns a boolean. The boolean indicates whether the check passes or not.

param

The target object to perform the check on.

returns
  • Returns true if the check passes, otherwise false.

Type declaration

  • [key: string]: (target: any) => boolean
      • (target: any): boolean
      • Parameters

        • target: any

        Returns boolean

PermissionModule

PermissionModule: { abilities: Abilities; checks: Checks }

Represents a module of permissions with associated abilities and checks.

property

{Abilities} abilities - The set of abilities associated with the module.

property

{Checks} checks - The set of checks associated with the module.

Type declaration

Permissions

Permissions: {}

Represents a collection of permissions, where each key is a string representing a module name, and the corresponding value is a PermissionModule.

Type declaration

Rules

Rules: { abilities?: readonly string[] | undefined; checks?: readonly string[] | undefined; module?: string; target?: unknown }

Represents a set of rules defining permissions, typically used in the context of checking user permissions.

property

{string} module - The optional module name to which the rules apply.

property

{readonly string[] | undefined} abilities - The optional list of abilities required by the rules.

property

{readonly string[] | undefined} checks - The optional list of checks required by the rules.

property

{unknown} target - The optional target object to perform checks against.

Type declaration

  • Optional Readonly abilities?: readonly string[] | undefined
  • Optional Readonly checks?: readonly string[] | undefined
  • Optional Readonly module?: string
  • Optional Readonly target?: unknown

TestRules

TestRules: {}

Represents a collection of test rules, where each key is a string representing a rule name, and the corresponding value is a Rules object.

Type declaration

Functions

Const canAllow

  • Checks whether a set of permissions allows all specified test rules.

    example
    const userPermissions = {
      'moduleA': {
        abilities: { read: true, write: false },
        checks: { isAdmin: (target) => target.isAdmin }
      },
      'moduleB': {
        abilities: { read: true, write: true },
        checks: { hasAccess: (target) => target.isValidUser }
      },
      // ... other modules
    };
    
    const testRules = {
      'moduleA': { abilities: ['read'] },
      'moduleB': { checks: ['hasAccess'], target: { isValidUser: true } },
      // ... other test rules
    };
    
    const result = canAllow(userPermissions, testRules);
    console.log(result); // Output: true
    

    Parameters

    • permissions: Permissions

      The object containing user permissions.

    • test: TestRules

      The set of test rules to check against the user permissions.

    Returns boolean

    • Returns true if the user has the required permissions for all test rules, otherwise false.

Const checkPermissions

  • Checks if a user has the required permissions based on specified rules.

    example

    Basic usage

    const userPermissions = {
      'moduleA': {
        abilities: { read: true, write: false },
        checks: { isAdmin: (target) => target.isAdmin }
      },
      'moduleB': {
        abilities: { read: true, write: true },
        checks: { hasAccess: (target) => target.isValidUser }
      },
      // ... other modules
    };
    
    const rules1 = { module: 'moduleA', abilities: ['read'] };
    const result1 = checkPermissions(userPermissions, rules1);
    console.log(result1); // Output: true
    
    example

    Checking with custom checks

    const rules2 = { module: 'moduleB', checks: ['hasAccess'], target: { isValidUser: true } };
    const result2 = checkPermissions(userPermissions, rules2);
    console.log(result2); // Output: true
    
    example

    No specified rules (always returns true)

    const rules3 = {};
    const result3 = checkPermissions(userPermissions, rules3);
    console.log(result3); // Output: true
    

    Parameters

    • permissions: Permissions

      The object containing user permissions.

    • rules: Rules

      The rules specifying the required permissions and checks.

    Returns boolean

    • Returns true if the user has the required permissions, otherwise false.

Generated using TypeDoc