group-manager-js v0.2.0
group-manager-js
A simple tool to handle users' permission like a plugin of server of Minecraft named 'GroupManager'.
Installation
$ npm install -S group-manager-js
Example
const manager = require('./dist/index');
// Permission example
console.log(manager.hasPermission('comment.send', 'comment.send')) // true
// You can also use '-' as a negative permission
console.log(manager.hasPermission('-comment.send', 'comment.send')) // false
console.log(manager.hasPermission('comment.*', 'comment.send')) // true
console.log(manager.hasPermission('*', 'comment.send')) // true
console.log(manager.hasPermission('article.send', 'comment.send')) // false
// Group example
const defaultGroup = new manager.Group('default', ['article.read', 'register'], []);
/**
* The user group will inherit all permissions from default group.
* All permission in user group will overwrite permissions in parent group(s).
**/
const userGroup = new manager.Group('user', ['comment.send', '-register'], [defaultGroup]);
console.log(userGroup.hasPermission('article.read')) // true
console.log(userGroup.hasPermission('comment.send')) // true
console.log(userGroup.hasPermission('register')) // false
API Reference
const manager = require('group-manager-js');
Format of permission string
Valid examples:
comment.send
comment.* // Will match comment.send, comment.modify, comment.modify.self etc.
*
P.S. character "*" will match any of permission
Invalid examples:
comment.*.send
Valid but not suggested examples:
中文.chinese
manager.hasPermission()
Args
perm {string} The permission string you want to be compared.
targetPerm {string} The permission string you want to compare with.
manager.isValid()
Args
perm {string} The permission string you want to be compared.
manager.getRelation()
Get the relation between perm and targetPerm.
Args
perm {string} The permission string you want to be compared.
targetPerm {string} The permission string you want to compare with.
Return
If return 0, perm is the brother of targetPerm or perm is equal to targetPerm. e.g: 'a.b.c' and 'a.b.d'
If return 1, perm is the parent of targetPerm. e.g: 'a.b' and 'a.b.c'.
If return -1, perm is the child of targetPerm. e.g: 'a.b.c' and 'a.b'
If return -2, perm has no relation with targetPerm. e.g: 'a.b.c' and 'a.d.c'
class manager.Group
constructor
name {string} The name of this group.
permissions? {string[]} Permission that this group have.
inherit? {Group[]} Group that will be inherited by this group.
P.S. Attributes are same as args of constructor function.