npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vsky/accesscontrol

v3.0.23

Published

This package is the core of AccessControl. JavaScript Package that encapsulates functionality related to access control permissions. This package is designed to manage and manipulate a set of permissions represented in a data structure.

Downloads

208

Readme

This package is the core of AccessControl. JavaScript Package that encapsulates functionality related to access control permissions. This package is designed to manage and manipulate a set of permissions represented in a data structure.

Installation

npm install @vsky/accesscontrol
# or
pnpm install @vsky/accesscontrol
# or
yarn add @vsky/accesscontrol
const data = [
   {
      role: "superadmin",
      resource: "supplier, product, order, employee, user, report, company, branch, profile, payment, dashboard, inventory, customer, settings",
      action: "create",
   },
   {
      role: "superadmin",
      resource: "supplier, product, order, employee, user, report, company, branch, profile, payment, dashboard, inventory, customer, settings",
      action: "read",
   },
   {
      role: "admin",
      resource: "supplier, product, order, employee, user, report, company, branch, profile, payment, dashboard, inventory, customer, settings",
      action: "read",
   },
   {
      role: "user",
      resource: "product, order, profile, payment, dashboard, inventory, customer, settings",
      action: "read",
   },
];

let ac = new AccessControl(data);

getData():

returns the data stored in the object.

Returns the data passed to the constructor

const data = ac.getData()

getResourcesForActionAndRole(action, role):

returns a list of resources that match the specified action and role.

Returns an array of resources for the specified action and role

let resources = ac.getResourcesForActionAndRole("read", "superadmin");


Returns an array of resources for the specified action

let resources = ac.getResourcesForActionAndRole("read");

canPerformAction(role, resource, action):

returns a boolean indicating whether the specified role is allowed to perform the specified action on the specified resource.

Returns true if the specified role has permission to perform the specified action on the specified resource

const isAllowed = ac.canPerformAction("superadmin", "supplier", "create")

const isAllowed = ac.canPerformAction("superadmin", "product", "read")


Returns false if the specified role does not have permission to perform the specified action on the specified resource

const isAllowed = ac.canPerformAction("admin", "books", "create")

const isAllowed = ac.canPerformAction("admin", "cars", "update")

findActions(role, resource):

returns a list of actions that are allowed for the specified role and resource.

Returns an array of actions that the specified role is allowed to perform on the specified resource

let actions = ac.findActions("superadmin", "supplier");


Returns an array of actions that the specified role is allowed to perform on all resources

let actions = ac.findActions("superadmin");

addPermission(options):

adds a new permission to the data, with the specified role, action, and resource(s).


// Add a permission to the data
ac.addPermission({
  role: 'admin',
  action: 'read',
  resource: 'documents'
});

ac.addPermission({
   role: "student",
   action: ["create", "update"],
   resource: ["customer", "payment"],
});

updateResource(options):

updates the resources for the specified role and action, adding the specified resource(s) to the list of allowed resources.

// Update resources for a specific role and action

ac.updateResource({
  role: 'admin',
  action: 'read',
  resources: ['reports', 'invoices']
});

removeResource(options):

removes the specified resource(s) from the list of allowed resources for the specified role and action.

Remove a resource from an entry with the specified role, action, and resource

const data = ac.removeResource({
   role: "superadmin",
   action: "create",
   resource: "supplier",
});


Remove multiple resources from an entry with the specified role, action, and resource if the resource is provided as an array

ac.addPermission({
   role: "superadmin",
   action: "create",
   resource: ["supplier", "product"],
});

const data = ac.removeResource({
   role: "superadmin",
   action: "create",
   resource: ["supplier", "product"],
});


Remove a resource from all entries with the specified action if no role is specified

ac.addPermission({
   role: "superadmin",
   action: "create",
   resource: ["supplier", "product"],
});

ac.addPermission({
   role: "admin",
   action: "create",
   resource: ["supplier", "product"],
});
      
const data = ac.removeResource({ action: "create", resource: "supplier" });


Remove a resource from all entries with the specified role if no action is specified

ac.addPermission({
   role: "superadmin",
   action: "create",
   resource: ["supplier", "product"],
});

ac.addPermission({
   role: "superadmin",
   action: "read",
   resource: ["supplier", "product"],
});

const data = ac.removeResource({ role: "superadmin", resource: "supplier" });

removePermissions(options):

removes all permissions for the specified role, action, and resource.

const data = ac.removePermission({ role: "superadmin", action: "create" });

Remove all entries from the data array if no role or action is specified

ac.addPermission({
   role: "superadmin",
   action: "create",
   resource: ["supplier", "product"],
});

ac.addPermission({
   role: "admin",
   action: "create",
   resource: ["supplier", "product"],
});

ac.addPermission({
   role: "superadmin",
   action: "read",
   resource: ["supplier", "product"],
});

const data = ac.removePermission({});


Remove all entries with the specified role if no action is specified

ac.addPermission({
   role: "admin",
   action: "create",
   resource: ["supplier", "product"],
});

ac.addPermission({
   role: "superadmin",
   action: "read",
   resource: ["supplier", "product"],
});

const data = ac.removePermission({ role: "superadmin" });

Remove all entries with the specified action if no role is specified

ac.addPermission({
   role: "superadmin",
   action: "create",
   resource: ["supplier", "product", "sidebar"],
});

ac.addPermission({
   role: "admin",
   action: "create",
   resource: ["supplier", "product"],
});

ac.addPermission({
   role: "superadmin",
   action: "read",
   resource: ["supplier", "product", "sidebar", "sidebar"],
});

const data = ac.removePermission({ action: "create" });