UNPKG

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.

165 lines (146 loc) 4.47 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: index.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: index.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Permissions = void 0; /** * Create a Permissions class. * @class * @param {Array&lt;Object>} [permissions] - The list of permissions. * @param {Bits} [bits = 0] - Actual permissions of user. */ class Permissions { constructor(permissions, bits) { this.bits = bits; this.permissions = permissions; } /** * Return all permissions as a integer. * @type {number} */ get totalBits() { let bit = 0; for (let flag of this.permissions) { bit += flag.value; } return bit; } /** * Return the permissions of member as a number. * @type {number} */ get permissionCalc() { return this.bits; } /** * Return the permissions of user as an array * @returns {Array&lt;string>} */ toArray() { let bits = this.bits; const flags = [...this.permissions].reverse(); const userPermissions = []; for (let permission of flags) { const rest = bits % permission.value; if (rest == 0 &amp;&amp; bits != 0) { userPermissions.push(permission.name); break; } if (rest &lt; bits) { userPermissions.push(permission.name); bits = rest; } } return userPermissions; } /** * * @param {Array&lt;string>|String|Number} [permission] The * @returns {boolean} */ hasPermissions(permission) { if (Array.isArray(permission)) return permission.every(p => this.hasPermissions(p)); const permissionsArray = this.toArray(); if (permissionsArray.includes('ADMINISTRATOR')) return true; if (typeof permission === 'string') { if (permissionsArray.includes(permission)) return true; else return false; } if (typeof permission === 'number') { let hasPermissions = false; this.permissions.map(p => { if (p.value === permission) hasPermissions = true; }); return hasPermissions; } return false; } /** * * @param {string|Array&lt;string>} permissions * @returns {number} */ calculate(permissions) { if (typeof permissions === 'string') { const permission = this.permissions.find(f => f.name === permissions.toUpperCase()); return permission ? permission : undefined; } if (Array.isArray(permissions)) { let some = 0; permissions.map(p => { const permission = this.permissions.find(perm => perm.name === p.toUpperCase()); if (permission) some += permission.value; }); } } } exports.Permissions = Permissions; // hasAnyRoles // Add role // Remove role // Add all permissions // Remove all permissions // To string // To array // Missing // Change // Equals // Value of // default </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Permissions.html">Permissions</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Fri Jun 11 2021 20:07:15 GMT+0200 (heure d’été d’Europe centrale) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>