@modnes/access-controller
v1.0.0
Published
Module to Control Access using roles and functions
Downloads
10
Readme
Access Controller Modules
Access Controller module provides an utility class to check if an user has access to some resource based in constraints as roles or functions set.
Installing
npm i @modnes/access-controller
Usage
To check if an user has access to something the AccessController.hasAccess()
method must be called passing an user object and a constraints object.
import AccessController from 'path/to/@modnes/access-controller/access-controller.js'
const USER = {
id: 1,
roles: ['user'],
isLogged: () => true
}
const CONSTRAINTS = {
roles: ['user', 'manager'],
functions: [
user => user.id === 1,
USER.isLogged
]
}
if AccessController.hasAccess(USER, CONSTRAINTS) console.log('User has access!')
Methods
Access Controller provides more two methods: userHasRoles() and callFunctions(). These methods can be called in isolation in certain circumstances.
userHasRoles
The userHasRoles()
method checks if an user has some of passed roles.
const USER = {
id: 1,
roles: ['user']
}
const ROLES = ['user', 'manager']
if AccessController.userHasRoles(USER, ROLES) console.log('User has some of these roles!')
callFunctions
The callFunctions()
method returns true
if all passed functions return true
.
const USER = {
id: 1,
isLogged: () => true
}
const FUNCTIONS = [
user => user.id === 1,
USER.isLogged
]
if AccessController.callFunctions(USER, FUNCTIONS) console.log('All functions returned true!')
See the API documentation