Class Policy

A base class for creating Pundit.js policies. Policy classes can extend from this class (recommended) or set up actions at runtime for an instance of this class.

Constructors

Properties

Methods

Constructors

  • The constructor of the base policy class. This can be called with super(user, record) and this.setup.apply(this) to initialise the policy with the specified actions (methods) that are defined by the class that extends Policy.

    Parameters

    • user: unknown

      The user being authorised by a policy action.

    • record: unknown

      The record/object being authorised by a policy action.

    Returns Policy

Properties

actions: Map<string, ActionFunction | ActionMethod>

A map of policy action names and corresponding functions/methods that can be used to permit or forbid actions of the user for the record.

record: unknown

A kind of "model" object, whose authorisation you want to check.

user: unknown

The user you want to authorise (such the current user of the app).

Methods

  • An optional helper method that can be used to manually add a function as an action to the policy at runtime. Not needed if this.setup.apply(this) is used in the class constructor.

    Parameters

    • actionName: string

      The intended name of the action to add to the policy.

    • actionFn: ActionFunction

      The intended function to authorise the action.

    Returns void

  • Returns a boolean depending on whether the user is authorised to perform the specified action.

    Parameters

    • actionName: string

      The name of the policy action being authorised.

    Returns boolean

    • True if the user is permitted to perform the action. False if the user is forbidden from performing the action.
  • An optional helper method to make a copy of the policy with the same user, record, and actions.

    Parameters

    • user: unknown

      Override the existing user property of the policy

    • record: unknown

      Override the existing record property of the policy.

    Returns Policy

    • A copy of the policy.
  • A helper function that can be called as a part of the policy constructor using this.setup.apply(this). It automatically sets up all policy actions based on the method names of the class (with the exception of the class constructor).

    Returns void