Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

rbacc

1.0.3 • Public • Published

RBACC

Simple RBAC with custom user resources and Redis backend .

Install

npm install rbacc -save

Prerequisites

Examples

//Create rbacc object
//Client option on https://www.npmjs.com/package/redis
let rbacc=new  Rbacc({
    cacheTTL:3600000, //user object with all merged resources are cached in memory-cache, no in redis 
    debug:false,
    client:{
        host:"localhost",
        port:6379,
        prefix:"rbacc:"
    }
})
//Add initial roles. 
rbacc.addRoles({
    roles:{
        admin:{
            "*":"*" // asterisk on resource and premission allows all
        },
        user:{
            "/files/public":"read",
            "/blog/post":["read","write"], // use array on premission
            "/files/userDir/*":["read","write"],// asterisk on resource check subpath
        },
        guest:{
            "/files/public":"read",
            "/blog/post":"read"
        }
    },
    cb:(err,res)=>{
        if  (err)  {
            return  console.log(err);
        }
        console.log("added roles:",res);
    }
})
//Add user resources
rbacc.addUserResources({
    userId:"userId", 
    roles:["user"],
    resources:{
        "/files/myFiles":["read","write"],
        "/files/otherFiles":"read",
    },
    cb:(err,res)=>{
        if  (err)  {
            return  console.log(err)
        }
        console.log("added resources to user:",res)
    }
})
//Check if allowed
rbacc.isAllowed({
    userId:"userId",
    resource:"/files/userDir/584ds215fe812/data",
    premision:"read",
    cb:(err,res)=>{
        if  (err)  {
            return  console.log(err);
        }
        console.log("/files/userDir/584ds215fe812/data:read - ",res);
    }
})
 
rbacc.isAllowed({
    userId:"userId",
    resource:"/files/otherFiles",
    premision:"read",
    cb:(err,res)=>{
        if  (err)  {
            return  console.log(err);
        }
        console.log("/files/otherFiles:read - ",res);
    }
})

Install

npm i rbacc

DownloadsWeekly Downloads

2

Version

1.0.3

License

ISC

Unpacked Size

16 kB

Total Files

6

Last publish

Collaborators

  • avatar