Skip to content

Commit e180493

Browse files
committed
(feat) Allow number as valid role and grant type
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 #93
1 parent 2826ffe commit e180493

28 files changed

+248
-695
lines changed

lib/AccessControl.d.ts

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Access, IAccessInfo, Query, IQueryInfo, Permission } from './core';
2-
import type { ValidRoleOrArray, ValidRole } from '.';
32
/**
43
* @classdesc
54
* AccessControl class that implements RBAC (Role-Based Access Control) basics
@@ -119,7 +118,7 @@ declare class AccessControl {
119118
* @name AccessControl#isLocked
120119
* @type {Boolean}
121120
*/
122-
get isLocked(): boolean;
121+
readonly isLocked: boolean;
123122
/**
124123
* Gets the internal grants object that stores all current grants.
125124
*
@@ -235,7 +234,7 @@ declare class AccessControl {
235234
* @throws {AccessControlError} - If a role is extended by itself or a
236235
* non-existent role. Or if called after `.lock()` is called.
237236
*/
238-
extendRole(roles: ValidRoleOrArray, extenderRoles: ValidRoleOrArray): AccessControl;
237+
extendRole(roles: string | string[], extenderRoles: string | string[]): AccessControl;
239238
/**
240239
* Removes all the given role(s) and their granted permissions, at once.
241240
* @chainable
@@ -247,7 +246,7 @@ declare class AccessControl {
247246
*
248247
* @throws {AccessControlError} - If called after `.lock()` is called.
249248
*/
250-
removeRoles(roles: ValidRoleOrArray): AccessControl;
249+
removeRoles(roles: string | string[]): AccessControl;
251250
/**
252251
* Removes all the given resources for all roles, at once.
253252
* Pass the `roles` argument to remove access to resources for those
@@ -264,7 +263,7 @@ declare class AccessControl {
264263
*
265264
* @throws {AccessControlError} - If called after `.lock()` is called.
266265
*/
267-
removeResources(resources: ValidRoleOrArray, roles?: ValidRoleOrArray): AccessControl;
266+
removeResources(resources: string | string[], roles?: string | string[]): AccessControl;
268267
/**
269268
* Gets all the unique roles that have at least one access information.
270269
*
@@ -285,12 +284,12 @@ declare class AccessControl {
285284
*
286285
* @returns {Array<String>}
287286
*/
288-
getInheritedRolesOf(role: ValidRole): ValidRole[];
287+
getInheritedRolesOf(role: string): string[];
289288
/**
290289
* Alias of `getInheritedRolesOf`
291290
* @private
292291
*/
293-
getExtendedRolesOf(role: ValidRole): ValidRole[];
292+
getExtendedRolesOf(role: string): string[];
294293
/**
295294
* Gets all the unique resources that are granted access for at
296295
* least one role.
@@ -306,7 +305,7 @@ declare class AccessControl {
306305
*
307306
* @returns {Boolean}
308307
*/
309-
hasRole(role: ValidRoleOrArray): boolean;
308+
hasRole(role: string | string[]): boolean;
310309
/**
311310
* Checks whether grants include the given resource or resources.
312311
*
@@ -315,7 +314,7 @@ declare class AccessControl {
315314
*
316315
* @returns {Boolean}
317316
*/
318-
hasResource(resource: ValidRoleOrArray): boolean;
317+
hasResource(resource: string | string[]): boolean;
319318
/**
320319
* Gets an instance of `Query` object. This is used to check whether the
321320
* defined access is allowed for the given role(s) and resource. This
@@ -348,12 +347,12 @@ declare class AccessControl {
348347
* ac.can(['admin', 'user']).createOwn('profile');
349348
* // Note: when multiple roles checked, acquired attributes are unioned (merged).
350349
*/
351-
can(role: ValidRoleOrArray | IQueryInfo): Query;
350+
can(role: string | string[] | IQueryInfo): Query;
352351
/**
353352
* Alias of `can()`.
354353
* @private
355354
*/
356-
query(role: ValidRoleOrArray | IQueryInfo): Query;
355+
query(role: string | string[] | IQueryInfo): Query;
357356
/**
358357
* Gets an instance of `Permission` object that checks and defines the
359358
* granted access permissions for the target resource and role. Normally
@@ -438,12 +437,12 @@ declare class AccessControl {
438437
* // Note: when attributes is omitted, it will default to `['*']`
439438
* // which means all attributes (of the resource) are allowed.
440439
*/
441-
grant(role?: ValidRoleOrArray | IAccessInfo): Access;
440+
grant(role?: string | string[] | IAccessInfo): Access;
442441
/**
443442
* Alias of `grant()`.
444443
* @private
445444
*/
446-
allow(role?: ValidRoleOrArray | IAccessInfo): Access;
445+
allow(role?: string | string[] | IAccessInfo): Access;
447446
/**
448447
* Gets an instance of `Access` object. This is used to deny access to
449448
* specified resource(s) for the given role(s). Denying will only remove a
@@ -496,31 +495,31 @@ declare class AccessControl {
496495
* // To deny same resource for multiple roles:
497496
* ac.deny(['admin', 'user']).createOwn('profile');
498497
*/
499-
deny(role?: ValidRoleOrArray | IAccessInfo): Access;
498+
deny(role?: string | string[] | IAccessInfo): Access;
500499
/**
501500
* Alias of `deny()`.
502501
* @private
503502
*/
504-
reject(role?: ValidRoleOrArray | IAccessInfo): Access;
503+
reject(role?: string | string[] | IAccessInfo): Access;
505504
/**
506505
* @private
507506
*/
508-
_removePermission(resources: ValidRoleOrArray, roles?: ValidRoleOrArray, actionPossession?: string): void;
507+
_removePermission(resources: string | string[], roles?: string | string[], actionPossession?: string): void;
509508
/**
510509
* Documented separately in enums/Action
511510
* @private
512511
*/
513-
static get Action(): any;
512+
static readonly Action: any;
514513
/**
515514
* Documented separately in enums/Possession
516515
* @private
517516
*/
518-
static get Possession(): any;
517+
static readonly Possession: any;
519518
/**
520519
* Documented separately in AccessControlError
521520
* @private
522521
*/
523-
static get Error(): any;
522+
static readonly Error: any;
524523
/**
525524
* A utility method for deep cloning the given data object(s) while
526525
* filtering its properties by the given attribute (glob) notations.

lib/AccessControl.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.AccessControl = void 0;
43
var core_1 = require("./core");
54
var enums_1 = require("./enums");
65
var utils_1 = require("./utils");
@@ -131,7 +130,7 @@ var AccessControl = /** @class */ (function () {
131130
get: function () {
132131
return this._isLocked && Object.isFrozen(this._grants);
133132
},
134-
enumerable: false,
133+
enumerable: true,
135134
configurable: true
136135
});
137136
// -------------------------------
@@ -288,8 +287,8 @@ var AccessControl = /** @class */ (function () {
288287
var _this = this;
289288
if (this.isLocked)
290289
throw new core_1.AccessControlError(utils_1.ERR_LOCK);
291-
var rolesToRemove = utils_1.utils.toValidRoleArray(roles);
292-
if (rolesToRemove.length === 0 || !utils_1.utils.isFilledValidRoleArray(rolesToRemove)) {
290+
var rolesToRemove = utils_1.utils.toStringArray(roles);
291+
if (rolesToRemove.length === 0 || !utils_1.utils.isFilledStringArray(rolesToRemove)) {
293292
throw new core_1.AccessControlError("Invalid role(s): " + JSON.stringify(roles));
294293
}
295294
rolesToRemove.forEach(function (roleName) {
@@ -632,15 +631,15 @@ var AccessControl = /** @class */ (function () {
632631
*/
633632
AccessControl.prototype._removePermission = function (resources, roles, actionPossession) {
634633
var _this = this;
635-
resources = utils_1.utils.toValidRoleArray(resources);
634+
resources = utils_1.utils.toStringArray(resources);
636635
// resources is set but returns empty array.
637-
if (resources.length === 0 || !utils_1.utils.isFilledValidRoleArray(resources)) {
636+
if (resources.length === 0 || !utils_1.utils.isFilledStringArray(resources)) {
638637
throw new core_1.AccessControlError("Invalid resource(s): " + JSON.stringify(resources));
639638
}
640639
if (roles !== undefined) {
641-
roles = utils_1.utils.toValidRoleArray(roles);
640+
roles = utils_1.utils.toStringArray(roles);
642641
// roles is set but returns empty array.
643-
if (roles.length === 0 || !utils_1.utils.isFilledValidRoleArray(roles)) {
642+
if (roles.length === 0 || !utils_1.utils.isFilledStringArray(roles)) {
644643
throw new core_1.AccessControlError("Invalid role(s): " + JSON.stringify(roles));
645644
}
646645
}
@@ -674,7 +673,7 @@ var AccessControl = /** @class */ (function () {
674673
get: function () {
675674
return enums_1.Action;
676675
},
677-
enumerable: false,
676+
enumerable: true,
678677
configurable: true
679678
});
680679
Object.defineProperty(AccessControl, "Possession", {
@@ -685,7 +684,7 @@ var AccessControl = /** @class */ (function () {
685684
get: function () {
686685
return enums_1.Possession;
687686
},
688-
enumerable: false,
687+
enumerable: true,
689688
configurable: true
690689
});
691690
Object.defineProperty(AccessControl, "Error", {
@@ -696,7 +695,7 @@ var AccessControl = /** @class */ (function () {
696695
get: function () {
697696
return core_1.AccessControlError;
698697
},
699-
enumerable: false,
698+
enumerable: true,
700699
configurable: true
701700
});
702701
// -------------------------------

0 commit comments

Comments
 (0)