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

access-control-tree

v0.0.2

Published

Authorizing access to your app with ease.

Downloads

7

Readme

Access Control Tree

Authorizing access to your app with ease.

Check demo on: https://ariomoklo.github.io/access-control-tree/

Get Started

npm install access-control-tree

Define your access control

The concept of this library is structuring your application as a tree. Defining access from page to section to content to button or function as deep as you want.

// this is the base usage
const access = createAccessControl(
    {
        todo: {
            view: 'Enable user to view todo list page',
            action: {
                toggle: 'Enable user to toggle todo value',
                edit: 'Enable user to edit todo item',
                create: 'Enable user to add todo item',
                delete: 'Enable user to delete todo item'
            }
        },
        extra: {
            sayHi: 'Enable user to say hi'
        }
    }
);

createAccessControl

interface Access = { [K: string]: Access | string }
type AccessControlOptions = {
    /** 
     * add prefix to scope. default to '' (none) 
     * example prefix: 'com.example'
     * scope 'todo.view' => 'com.example.todo.view'
     * */
    prefix: string
    
    /** string to use for spacer for scope. default to '.' */
    spacer: string
    
    /** default string scopes to be enabled. default to [] */
    default: string[]
}

function createAccessControl<T extends Access> (access: T, Partial<AccessControlOptions>): {
    grant: (...scopes: string[]) => void
    reset: () => void
    export: (enabledOnly: boolean) => ({ scope: string, desc: string })[]
    has: (scope: string) => boolean
    get: (scope: string) => AccessNode | undefined
    search: (substring: string) => AccessNode[]
    getChild: (scope: string) => AccessNode[]
    readonly enabled: accessNode[]
    readonly disabled: accessNode[]
    readonly nodes: AccessNodes<T, AccessNode>
    readonly can: AccessNodes<T, boolean>
}

Asserting and Granting access

To assert granted access you can use can property. can property will be typed as the type of access object from createAccessControl function with the leaf node will be typed boolean. With the example from before, variable access will have property of can.todo.view with typeof boolean.

For getting access node data you can use nodes property. Like can, nodes will be structured as given access object but with node leaf with type of AccessNode.

type AccessNode = {
    // string scope
    scope: string
    // enabled value
    enabled: boolean
    // scope description
    desc: string
    // enabled toggle function
    toggle: () => void 
}

Here is example how to assert and granting scope

// asserting granted access
const isViewEnabled = access.can.todo.view
//    ^ type is boolean

// grant access by string scope
access.grant('todo.view', 'todo.toggle', 'todo.crud.edit')

// grant access by toggle function
access.nodes.todo.view.toggle()

// reseting access to default
access.reset()