1.0.3 • Published 6 years ago

rbacc v1.0.3

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

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);
	}
})
1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago