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

vanilla-rbac

v1.0.3

Published

A framework agnostic type-safe role-base access control

Downloads

7

Readme

GitHub contributors badge PRs Welcome badge Number of hits badge

NPM version badge NPM downloads badge

GitHub watchers badge GitHub stars badge

vanilla-rbac

A framework agnostic type-safe role-base access control library.

Getting Started

Vanilla RBAC is a < 1kb type-safe library that provides role-based access control to any frontend framework. It comprises of a setupRBAC function and a webcomponent named rback-comp.

Installation

  • npm: npm i vanilla-rbac
  • cdn: https://cdn.jsdelivr.net/gh/kiranmantha/vanilla-rbac/dist/index.js
  • importmap:
<script type="importmap">
  {
    "imports": {
      "vanilla-rbac": "https://cdn.jsdelivr.net/gh/kiranmantha/vanilla-rbac/dist/index.js"
    }
  }
</script>
<script type="module">
  import { setupRBAC } from 'vanilla-rbac';
</script>

Usage

  • In your project entry file, import setupRBAC and use it as below:
import { setupRBAC } from 'vanilla-rbac';

/**
 * This setup function will register the user details, user role and all possible roles with associated permissions
 * This will also register a webcomponent named `rbac-comp` which is used to control user interactions based on permission and role
 * Call this function after the system fetched user details and all possible roles with permissions
 */
setupRBAC({
  /**
   * logged in user details. type object.
   */
  user: {},
  /**
   * role of loggedin user. type string
   */
  userRole: 'lead',
  /**
   * roles is an object with role as key and permissions as value
   * permissions can be a string or a list of strings or an object
   * any role with '*' as permission defines that provided role is entitled to do any operation
   */
  roles: {
    lead: '*',
    seniormember: ['add:post', 'edit:post', 'view:posts'],
    juniormember: {
      /**
       * others is a predefined key that segregates permissions that do not depend on any logic
       */
      others: ['add:post'],
      /**
       * if provided permission not found in others, then it will be treated as key of object and execute it
       * @param {object} user is the loggedin user details
       * @param {DOMStringMap} data is the HTML Dataset which is the list of data-* that passed to rbac-comp
       */
      'edit:post': (user, data) => {
        /**
         * remember: data is the HTMLDataset that gives all the data-* attributes passed to the `rbac-comp`
         * more details: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
         */
        return user.id === data.postOwnerId;
      }
    }
  }
});
  • consume rbac-comp by passing data-perform. rbac-comp accepts 2 slots. allowed and notallowed.

Example 1: controlling specific operations

<rbac-comp data-perform="edit:post" data-post-owner-id="123">
  <button slot="allowed" onclick='alert("i can edit")'>Edit</button>
  <span slot="notallowed">access denied</span>
</rbac-comp>

Example 2: controlling specific pages

<!-- page: localhost/posts -->
<rbac-comp data-perform="view:posts">
  <div slot="allowed">
    <!-- display all posts within the page -->
    <ul>
      <li>Post 1</li>
      <li>Post 2</li>
    </ul>
    <!-- or -->
    <your-posts-component></your-posts-component>
  </div>
  <div slot="notallowed">
    <access-denied-component></access-denied-component>
  </div>
</rbac-comp>

Live Examples: