permissions.js
Version:
This module is a manager for permissions. With this module you can defined and host the permissions of user as a integer which makes it possible to limit the use of memory.
263 lines (160 loc) • 5.19 kB
Markdown
This module is a manager for permissions. With this module you can defined and host the permissions of user as a integer which makes it possible to limit the use of memory.
It work with a bitwise operations using the bigint library.
Prerequisites
Node.js 12.0.0 or newer is required.
With npm :
```sh-session
npm install permissions.js
```
With yarn :
```sh-session
yarn add permissions.js
```
Import the module from node_modules :
With CommonJS syntax :
```js
const { Permissions } = require("permissions.js");
```
With module syntax :
```js
import { Permissions } = from 'permissions.js';
```
Create a new Permissions with all available permissions as a parameter (an array).
| PARAMETER | TYPE | OPTIONAL | DEFAULT | DESCRIPTION |
| ---------------- | ------------------- | -------- | ------- | ------------------------- |
| permissions | Array\<string> | ✖️ | none | All available permissions |
| user permissions | int, bigint, string | ✓ | none | The user permissions |
Note : The order of the permissions is verry important, after choosing it you can add new permissions but at the end of array.
Example : `new Permissions(['ADMINISTRATOR', 'VIEW_ALL_MEMBERS', 'UPDATE_MEMBERS', 'DELETE_MEMBERS', 'BAN_MEMBERS'])`.
## Properties
---
### Permissions.default
Return the default permissions as a integer.
Type : Number
### Permissions.MAX
Return the max permission as a string.
Type : String
### Permissions.permissionCalc
The permissions of user (has a string).
Type : String
## Methods
---
## Permissions#find(name)
Find the permission value bay name
Params :
- name : The name of permission (string or Array\<string>)
Return :
The permission (Array\<Object> or empty array)
```js
console.log(userPermissions.find(4));
```
Return an array of permissions
Return :
Array\<permission>
```js
console.log(userPermissions.toArray());
```
Return the permissions names of user (separate by \`, `)
Return :
String\<permission>
```js
console.log(userPermissions.toString());
```
Return true if the user has at least one permission.
Return :
Boolean
```js
console.log(userPermissions.hasAnyPermissions());
```
Return true if user have permission(s).
Params :
permission : The permission to check (Array, String, Number)
Return :
Object
```js
console.log(userPermissions.hasPermission([3]));
```
Return the missings permissions of user in the parameter.
Params :
permissions : The permissions to test (Array, String, Number)
Return :
Object
```js
console.log(userPermissions(["3"]));
```
Return true if user permissions are equal to parameter.
Params :
- other : The compared (Array, String, Number)
Return :
Boolean
```js
console.log(userPermissions.equals([100, 100]));
```
Add all permissions from user.
Return :
String (the new bits permissions)
```js
console.log(userPermissions.addAllPermissions());
```
Add a permission to a user.
Params :
- permission : The permission to add (string | Array\<string> | number)
Return :
String (the new bits of permissions.)
```js
console.log(userPermissions.addPermission("ADMINISTRATOR"));
```
Remove all permissions of the user.
Return :
String (the new bits of permissions).
```js
console.log(userPermissions.removeAllPermissions());
```
Remove a permission to a user.
Params :
- permission : The permission to remove (string | Array\<string>) | number
Return :
String (the new bits of permissions.)
```js
console.log(userPermissions.removePermission("ADMINISTRATOR"));
```
```js
const { Permissions } = require("permissions");
const permissionsList = [
"ADMINISTRATOR",
"VIEW_ALL_MEMBERS",
"UPDATE_MEMBERS",
"DELETE_MEMBERS",
"BAN_MEMBERS",
];
const userPermissions = new Permissions(permissionsList, 100);
console.log(`Default : ${userPermissions.default}`);
console.log(`Max : ${userPermissions.MAX}`);
console.log("User bits : " + userPermissions.permissionCalc);
console.log(userPermissions.find(4));
console.log(userPermissions.toArray());
console.log(userPermissions.toString());
console.log(userPermissions.hasAnyPermissions());
console.log(userPermissions.missing([3]));
console.log(userPermissions.equals([100, 100]));
console.log(userPermissions.addPermission("ADMINISTRATOR"));
console.log(userPermissions.removePermission("ADMINISTRATOR"));
console.log(userPermissions.addAllPermissions());
console.log(userPermissions.removeAllPermissions());
```