Skip to content

Commit

Permalink
(feat) Allow number as valid role and grant type
Browse files Browse the repository at this point in the history
In some cases it makes sense to have `number`s instead of `string`s for
role types.  For example, when using `enum`s in TypeScript

Fixes onury#93
  • Loading branch information
vegerot committed Nov 20, 2020
1 parent 2826ffe commit e180493
Show file tree
Hide file tree
Showing 28 changed files with 248 additions and 695 deletions.
37 changes: 18 additions & 19 deletions lib/AccessControl.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Access, IAccessInfo, Query, IQueryInfo, Permission } from './core';
import type { ValidRoleOrArray, ValidRole } from '.';
/**
* @classdesc
* AccessControl class that implements RBAC (Role-Based Access Control) basics
Expand Down Expand Up @@ -119,7 +118,7 @@ declare class AccessControl {
* @name AccessControl#isLocked
* @type {Boolean}
*/
get isLocked(): boolean;
readonly isLocked: boolean;
/**
* Gets the internal grants object that stores all current grants.
*
Expand Down Expand Up @@ -235,7 +234,7 @@ declare class AccessControl {
* @throws {AccessControlError} - If a role is extended by itself or a
* non-existent role. Or if called after `.lock()` is called.
*/
extendRole(roles: ValidRoleOrArray, extenderRoles: ValidRoleOrArray): AccessControl;
extendRole(roles: string | string[], extenderRoles: string | string[]): AccessControl;
/**
* Removes all the given role(s) and their granted permissions, at once.
* @chainable
Expand All @@ -247,7 +246,7 @@ declare class AccessControl {
*
* @throws {AccessControlError} - If called after `.lock()` is called.
*/
removeRoles(roles: ValidRoleOrArray): AccessControl;
removeRoles(roles: string | string[]): AccessControl;
/**
* Removes all the given resources for all roles, at once.
* Pass the `roles` argument to remove access to resources for those
Expand All @@ -264,7 +263,7 @@ declare class AccessControl {
*
* @throws {AccessControlError} - If called after `.lock()` is called.
*/
removeResources(resources: ValidRoleOrArray, roles?: ValidRoleOrArray): AccessControl;
removeResources(resources: string | string[], roles?: string | string[]): AccessControl;
/**
* Gets all the unique roles that have at least one access information.
*
Expand All @@ -285,12 +284,12 @@ declare class AccessControl {
*
* @returns {Array<String>}
*/
getInheritedRolesOf(role: ValidRole): ValidRole[];
getInheritedRolesOf(role: string): string[];
/**
* Alias of `getInheritedRolesOf`
* @private
*/
getExtendedRolesOf(role: ValidRole): ValidRole[];
getExtendedRolesOf(role: string): string[];
/**
* Gets all the unique resources that are granted access for at
* least one role.
Expand All @@ -306,7 +305,7 @@ declare class AccessControl {
*
* @returns {Boolean}
*/
hasRole(role: ValidRoleOrArray): boolean;
hasRole(role: string | string[]): boolean;
/**
* Checks whether grants include the given resource or resources.
*
Expand All @@ -315,7 +314,7 @@ declare class AccessControl {
*
* @returns {Boolean}
*/
hasResource(resource: ValidRoleOrArray): boolean;
hasResource(resource: string | string[]): boolean;
/**
* Gets an instance of `Query` object. This is used to check whether the
* defined access is allowed for the given role(s) and resource. This
Expand Down Expand Up @@ -348,12 +347,12 @@ declare class AccessControl {
* ac.can(['admin', 'user']).createOwn('profile');
* // Note: when multiple roles checked, acquired attributes are unioned (merged).
*/
can(role: ValidRoleOrArray | IQueryInfo): Query;
can(role: string | string[] | IQueryInfo): Query;
/**
* Alias of `can()`.
* @private
*/
query(role: ValidRoleOrArray | IQueryInfo): Query;
query(role: string | string[] | IQueryInfo): Query;
/**
* Gets an instance of `Permission` object that checks and defines the
* granted access permissions for the target resource and role. Normally
Expand Down Expand Up @@ -438,12 +437,12 @@ declare class AccessControl {
* // Note: when attributes is omitted, it will default to `['*']`
* // which means all attributes (of the resource) are allowed.
*/
grant(role?: ValidRoleOrArray | IAccessInfo): Access;
grant(role?: string | string[] | IAccessInfo): Access;
/**
* Alias of `grant()`.
* @private
*/
allow(role?: ValidRoleOrArray | IAccessInfo): Access;
allow(role?: string | string[] | IAccessInfo): Access;
/**
* Gets an instance of `Access` object. This is used to deny access to
* specified resource(s) for the given role(s). Denying will only remove a
Expand Down Expand Up @@ -496,31 +495,31 @@ declare class AccessControl {
* // To deny same resource for multiple roles:
* ac.deny(['admin', 'user']).createOwn('profile');
*/
deny(role?: ValidRoleOrArray | IAccessInfo): Access;
deny(role?: string | string[] | IAccessInfo): Access;
/**
* Alias of `deny()`.
* @private
*/
reject(role?: ValidRoleOrArray | IAccessInfo): Access;
reject(role?: string | string[] | IAccessInfo): Access;
/**
* @private
*/
_removePermission(resources: ValidRoleOrArray, roles?: ValidRoleOrArray, actionPossession?: string): void;
_removePermission(resources: string | string[], roles?: string | string[], actionPossession?: string): void;
/**
* Documented separately in enums/Action
* @private
*/
static get Action(): any;
static readonly Action: any;
/**
* Documented separately in enums/Possession
* @private
*/
static get Possession(): any;
static readonly Possession: any;
/**
* Documented separately in AccessControlError
* @private
*/
static get Error(): any;
static readonly Error: any;
/**
* A utility method for deep cloning the given data object(s) while
* filtering its properties by the given attribute (glob) notations.
Expand Down
21 changes: 10 additions & 11 deletions lib/AccessControl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessControl = void 0;
var core_1 = require("./core");
var enums_1 = require("./enums");
var utils_1 = require("./utils");
Expand Down Expand Up @@ -131,7 +130,7 @@ var AccessControl = /** @class */ (function () {
get: function () {
return this._isLocked && Object.isFrozen(this._grants);
},
enumerable: false,
enumerable: true,
configurable: true
});
// -------------------------------
Expand Down Expand Up @@ -288,8 +287,8 @@ var AccessControl = /** @class */ (function () {
var _this = this;
if (this.isLocked)
throw new core_1.AccessControlError(utils_1.ERR_LOCK);
var rolesToRemove = utils_1.utils.toValidRoleArray(roles);
if (rolesToRemove.length === 0 || !utils_1.utils.isFilledValidRoleArray(rolesToRemove)) {
var rolesToRemove = utils_1.utils.toStringArray(roles);
if (rolesToRemove.length === 0 || !utils_1.utils.isFilledStringArray(rolesToRemove)) {
throw new core_1.AccessControlError("Invalid role(s): " + JSON.stringify(roles));
}
rolesToRemove.forEach(function (roleName) {
Expand Down Expand Up @@ -632,15 +631,15 @@ var AccessControl = /** @class */ (function () {
*/
AccessControl.prototype._removePermission = function (resources, roles, actionPossession) {
var _this = this;
resources = utils_1.utils.toValidRoleArray(resources);
resources = utils_1.utils.toStringArray(resources);
// resources is set but returns empty array.
if (resources.length === 0 || !utils_1.utils.isFilledValidRoleArray(resources)) {
if (resources.length === 0 || !utils_1.utils.isFilledStringArray(resources)) {
throw new core_1.AccessControlError("Invalid resource(s): " + JSON.stringify(resources));
}
if (roles !== undefined) {
roles = utils_1.utils.toValidRoleArray(roles);
roles = utils_1.utils.toStringArray(roles);
// roles is set but returns empty array.
if (roles.length === 0 || !utils_1.utils.isFilledValidRoleArray(roles)) {
if (roles.length === 0 || !utils_1.utils.isFilledStringArray(roles)) {
throw new core_1.AccessControlError("Invalid role(s): " + JSON.stringify(roles));
}
}
Expand Down Expand Up @@ -674,7 +673,7 @@ var AccessControl = /** @class */ (function () {
get: function () {
return enums_1.Action;
},
enumerable: false,
enumerable: true,
configurable: true
});
Object.defineProperty(AccessControl, "Possession", {
Expand All @@ -685,7 +684,7 @@ var AccessControl = /** @class */ (function () {
get: function () {
return enums_1.Possession;
},
enumerable: false,
enumerable: true,
configurable: true
});
Object.defineProperty(AccessControl, "Error", {
Expand All @@ -696,7 +695,7 @@ var AccessControl = /** @class */ (function () {
get: function () {
return core_1.AccessControlError;
},
enumerable: false,
enumerable: true,
configurable: true
});
// -------------------------------
Expand Down
Loading

0 comments on commit e180493

Please sign in to comment.