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

@cellajs/permission-manager

v0.1.0

Published

Permission manager to formulate abac and rbac policies in typescript projects that have a hierarchical structure.

Downloads

1

Readme

permission-manager

Description

The permission-manager is a tool designed to facilitate Role-Based Access Control (RBAC) and Attribute-Based Access Control (ABAC) permissions within a hierarchical application structure.

Workflow

  1. Define the hierarchical structure of your application with modules: Context and Product.
  2. Configure access policies based on a many-to-many relation (context - subject).
  3. Optionally configure your Adapters.
  4. Integrate the permission manager into Middlewares or application logic.

Permission Manager Configuration

It's recommended to configure the permission manager during app setup. This can be broken down into three main topics:

1. Define App Structure

We differentiate between a context and a product:

  • A context contains roles that an actor can claim.
  • A product doesn't have roles and represents entities that can be created by actors.

Example Usage

import { Context, Product } from './src/PermissionManager';

const community = new Context(
    'community',  // Name of context
    ['admin', 'member'], // Array of role names
);

const group = new Context(
    'group', // Name of context
    ['leader', 'member'],// Array of role names
    new Set([community]), // A set of parents
);

const item = new Product(
    'item', // Name of product
    new Set([group]), // A set of parents
);

2. Build Access Policies

To configure access policies within the permission-manager, follow these steps:

  1. Create a New Instance: Instantiate a new PermissionManager instance.

  2. Configure Access Policies: Utilize the configureAccessPolicies method to set up access policies. This function will be injected with an object containing the subject and the contexts.

Example Usage

import { PermissionManager, AccessPolicyConfiguration } from './src/PermissionManager';

// Create a new instance of PermissionManager
const permissionManager = new PermissionManager('guard');

// Configure access policies using the configureAccessPolicies method
permissionManager.accessPolicies.configureAccessPolicies(({ subject, contexts }: AccessPolicyConfiguration) => {

    // Destructure the contexts object
    const { community, group } = contexts;

    // Switch statement to define access policies based on the subject
    switch (subject.name) {
        case 'community':
            // Define access policies for community context
            community.admin({ create: 0, read: 1, update: 1, delete: 0, invite: 1 });
            community.member({ create: 0, read: 1, update: 0, delete: 0, invite: 1 });
            break;

        case 'group':
            // Define access policies for group context
            community.admin({ create: 1, read: 1, update: 1, delete: 1, invite: 1 });
            group.leader({ create: 0, read: 1, update: 1, delete: 0, invite: 1 });
            group.member({ create: 0, read: 1, update: 0, delete: 0, invite: 1 });
            break;

        case 'item':
            // Define access policies for item context
            community.admin({ create: 1, read: 1, update: 1, delete: 1 });
            group.leader({ create: 1, read: 1, update: 1, delete: 1 });
            group.member({ create: 1, read: 1, update: 0, delete: 0 });
            break;
    }
});

3. Optional: Configure Adapters

The permission manager expects a specific format for memberships and subjects to check allowances. To facilitate this conversion process, you can configure adapters that automatically transform your memberships and subjects into the required format.

Example Usage

import { Membership, MembershipAdapter, Subject, SubjectAdapter } from './src/PermissionManager';

// Custom adapter for transforming memberships to the expected format
class AppMembershipAdapter extends MembershipAdapter {
    adapt(memberships: any[]): Membership[] {
        return memberships.map((m) => ({
            contextName: m.type,
            contextKey: m.key,
            roleName: m.role,
            ancestors: m.ancestors || {}
        }));
    }
}

// Instantiate and use the custom membership adapter
const appMembershipAdapter = new AppMembershipAdapter();

// Custom adapter for transforming subjects to the expected format
class AppSubjectAdapter extends SubjectAdapter {
    adapt(s: any): Subject {
        return {
            name: s.type,
            key: s.key,
            ancestors: s.ancestors || {}
        };
    }
}

// Instantiate and use the custom subject adapter
const appSubjectAdapter = new AppSubjectAdapter();

Permission Manager Usage

There are currently two ways to use permission-manager within app logic:

  1. isPermissionAllowed: Checks if a permission is allowed. It returns a simple boolean. Additionally, it checks the ancestor roles of a subject.

Example Usage

const isAllowed = permissionManager.isPermissionAllowed(memberships, 'read', subject);
  1. getActorPolicies: Returns an object with all action allowances and action allowances of child subjects. This object can be used to extend a subject with, for example, a 'canDo' property. This can be utilized on the client-side, so clients don't need to know which role an actor has to determine their permissions

Example Usage

const canDo = permissionManager.getActorPolicies(memberships, subject);

Contributors

(Add contributors if applicable)

License

(Include license details)