"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Permissions = void 0;
/**
* Create a Permissions class.
* @class
* @param {Array<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<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 && bits != 0) {
userPermissions.push(permission.name);
break;
}
if (rest < bits) {
userPermissions.push(permission.name);
bits = rest;
}
}
return userPermissions;
}
/**
*
* @param {Array<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<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