Skip to content

andyvanee/m2acl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

M2ACL - Flexible Access Control Library

Install

yarn add m2acl

or

npm i m2acl

Basic example

import M2ACL from "m2acl"

// Create new ACL object with given actions
const acl = new M2ACL({
    actions: ["create", "read", "update", "delete"]
})

// Grant a group a permission for some resource
acl.role("public").can("read", { resource: "todo" })

const user = {
    id: "abc",
    role: "public" // role can be string or array
}

// Async wrapper since verify is promise-based
;(async () => {
    const permit1 = await acl.verify(user, {
        resource: "todo",
        action: "read",
        properties: {
            id: 1,
            name: "An example todo"
        }
    })
    permit1.granted // => true
    permit1.properties // => { id: 1, name: "An example todo" }
    console.log({ permit1 })

    const permit2 = await acl.verify(user, {
        type: "todo",
        action: "create",
        properties: {
            name: "Another example"
        }
    })
    permit2.granted // => false since only read permission was granted
    permit2.properties // => null
    console.log({ permit2 })
})()

For more detailed examples see the spec files in spec/*.spec.js

Advanced tips

Property filters and Permission arguments are only for basic schema validation and property checking. For more complex schema enforcement or filtering, you should use a library like Joi within your callback functions.

Development

# install dependencies
yarn

# run specs
yarn spec

About

Flexible Access Control Library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published