Skip to content

Commit 0b40633

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 090a338 commit 0b40633

28 files changed

+695
-248
lines changed

lib/AccessControl.d.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Access, IAccessInfo, Query, IQueryInfo, Permission } from './core';
2+
import type { ValidRoleOrArray, ValidRole } from '.';
23
/**
34
* @classdesc
45
* AccessControl class that implements RBAC (Role-Based Access Control) basics
@@ -118,7 +119,7 @@ declare class AccessControl {
118119
* @name AccessControl#isLocked
119120
* @type {Boolean}
120121
*/
121-
readonly isLocked: boolean;
122+
get isLocked(): boolean;
122123
/**
123124
* Gets the internal grants object that stores all current grants.
124125
*
@@ -234,7 +235,7 @@ declare class AccessControl {
234235
* @throws {AccessControlError} - If a role is extended by itself or a
235236
* non-existent role. Or if called after `.lock()` is called.
236237
*/
237-
extendRole(roles: string | string[], extenderRoles: string | string[]): AccessControl;
238+
extendRole(roles: ValidRoleOrArray, extenderRoles: ValidRoleOrArray): AccessControl;
238239
/**
239240
* Removes all the given role(s) and their granted permissions, at once.
240241
* @chainable
@@ -246,7 +247,7 @@ declare class AccessControl {
246247
*
247248
* @throws {AccessControlError} - If called after `.lock()` is called.
248249
*/
249-
removeRoles(roles: string | string[]): AccessControl;
250+
removeRoles(roles: ValidRoleOrArray): AccessControl;
250251
/**
251252
* Removes all the given resources for all roles, at once.
252253
* Pass the `roles` argument to remove access to resources for those
@@ -263,7 +264,7 @@ declare class AccessControl {
263264
*
264265
* @throws {AccessControlError} - If called after `.lock()` is called.
265266
*/
266-
removeResources(resources: string | string[], roles?: string | string[]): AccessControl;
267+
removeResources(resources: ValidRoleOrArray, roles?: ValidRoleOrArray): AccessControl;
267268
/**
268269
* Gets all the unique roles that have at least one access information.
269270
*
@@ -284,12 +285,12 @@ declare class AccessControl {
284285
*
285286
* @returns {Array<String>}
286287
*/
287-
getInheritedRolesOf(role: string): string[];
288+
getInheritedRolesOf(role: ValidRole): ValidRole[];
288289
/**
289290
* Alias of `getInheritedRolesOf`
290291
* @private
291292
*/
292-
getExtendedRolesOf(role: string): string[];
293+
getExtendedRolesOf(role: ValidRole): ValidRole[];
293294
/**
294295
* Gets all the unique resources that are granted access for at
295296
* least one role.
@@ -305,7 +306,7 @@ declare class AccessControl {
305306
*
306307
* @returns {Boolean}
307308
*/
308-
hasRole(role: string | string[]): boolean;
309+
hasRole(role: ValidRoleOrArray): boolean;
309310
/**
310311
* Checks whether grants include the given resource or resources.
311312
*
@@ -314,7 +315,7 @@ declare class AccessControl {
314315
*
315316
* @returns {Boolean}
316317
*/
317-
hasResource(resource: string | string[]): boolean;
318+
hasResource(resource: ValidRoleOrArray): boolean;
318319
/**
319320
* Gets an instance of `Query` object. This is used to check whether the
320321
* defined access is allowed for the given role(s) and resource. This
@@ -347,12 +348,12 @@ declare class AccessControl {
347348
* ac.can(['admin', 'user']).createOwn('profile');
348349
* // Note: when multiple roles checked, acquired attributes are unioned (merged).
349350
*/
350-
can(role: string | string[] | IQueryInfo): Query;
351+
can(role: ValidRoleOrArray | IQueryInfo): Query;
351352
/**
352353
* Alias of `can()`.
353354
* @private
354355
*/
355-
query(role: string | string[] | IQueryInfo): Query;
356+
query(role: ValidRoleOrArray | IQueryInfo): Query;
356357
/**
357358
* Gets an instance of `Permission` object that checks and defines the
358359
* granted access permissions for the target resource and role. Normally
@@ -437,12 +438,12 @@ declare class AccessControl {
437438
* // Note: when attributes is omitted, it will default to `['*']`
438439
* // which means all attributes (of the resource) are allowed.
439440
*/
440-
grant(role?: string | string[] | IAccessInfo): Access;
441+
grant(role?: ValidRoleOrArray | IAccessInfo): Access;
441442
/**
442443
* Alias of `grant()`.
443444
* @private
444445
*/
445-
allow(role?: string | string[] | IAccessInfo): Access;
446+
allow(role?: ValidRoleOrArray | IAccessInfo): Access;
446447
/**
447448
* Gets an instance of `Access` object. This is used to deny access to
448449
* specified resource(s) for the given role(s). Denying will only remove a
@@ -495,31 +496,31 @@ declare class AccessControl {
495496
* // To deny same resource for multiple roles:
496497
* ac.deny(['admin', 'user']).createOwn('profile');
497498
*/
498-
deny(role?: string | string[] | IAccessInfo): Access;
499+
deny(role?: ValidRoleOrArray | IAccessInfo): Access;
499500
/**
500501
* Alias of `deny()`.
501502
* @private
502503
*/
503-
reject(role?: string | string[] | IAccessInfo): Access;
504+
reject(role?: ValidRoleOrArray | IAccessInfo): Access;
504505
/**
505506
* @private
506507
*/
507-
_removePermission(resources: string | string[], roles?: string | string[], actionPossession?: string): void;
508+
_removePermission(resources: ValidRoleOrArray, roles?: ValidRoleOrArray, actionPossession?: string): void;
508509
/**
509510
* Documented separately in enums/Action
510511
* @private
511512
*/
512-
static readonly Action: any;
513+
static get Action(): any;
513514
/**
514515
* Documented separately in enums/Possession
515516
* @private
516517
*/
517-
static readonly Possession: any;
518+
static get Possession(): any;
518519
/**
519520
* Documented separately in AccessControlError
520521
* @private
521522
*/
522-
static readonly Error: any;
523+
static get Error(): any;
523524
/**
524525
* A utility method for deep cloning the given data object(s) while
525526
* filtering its properties by the given attribute (glob) notations.

lib/AccessControl.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.AccessControl = void 0;
34
var core_1 = require("./core");
45
var enums_1 = require("./enums");
56
var utils_1 = require("./utils");
@@ -130,7 +131,7 @@ var AccessControl = /** @class */ (function () {
130131
get: function () {
131132
return this._isLocked && Object.isFrozen(this._grants);
132133
},
133-
enumerable: true,
134+
enumerable: false,
134135
configurable: true
135136
});
136137
// -------------------------------
@@ -287,8 +288,8 @@ var AccessControl = /** @class */ (function () {
287288
var _this = this;
288289
if (this.isLocked)
289290
throw new core_1.AccessControlError(utils_1.ERR_LOCK);
290-
var rolesToRemove = utils_1.utils.toStringArray(roles);
291-
if (rolesToRemove.length === 0 || !utils_1.utils.isFilledStringArray(rolesToRemove)) {
291+
var rolesToRemove = utils_1.utils.toValidRoleArray(roles);
292+
if (rolesToRemove.length === 0 || !utils_1.utils.isFilledValidRoleArray(rolesToRemove)) {
292293
throw new core_1.AccessControlError("Invalid role(s): " + JSON.stringify(roles));
293294
}
294295
rolesToRemove.forEach(function (roleName) {
@@ -631,15 +632,15 @@ var AccessControl = /** @class */ (function () {
631632
*/
632633
AccessControl.prototype._removePermission = function (resources, roles, actionPossession) {
633634
var _this = this;
634-
resources = utils_1.utils.toStringArray(resources);
635+
resources = utils_1.utils.toValidRoleArray(resources);
635636
// resources is set but returns empty array.
636-
if (resources.length === 0 || !utils_1.utils.isFilledStringArray(resources)) {
637+
if (resources.length === 0 || !utils_1.utils.isFilledValidRoleArray(resources)) {
637638
throw new core_1.AccessControlError("Invalid resource(s): " + JSON.stringify(resources));
638639
}
639640
if (roles !== undefined) {
640-
roles = utils_1.utils.toStringArray(roles);
641+
roles = utils_1.utils.toValidRoleArray(roles);
641642
// roles is set but returns empty array.
642-
if (roles.length === 0 || !utils_1.utils.isFilledStringArray(roles)) {
643+
if (roles.length === 0 || !utils_1.utils.isFilledValidRoleArray(roles)) {
643644
throw new core_1.AccessControlError("Invalid role(s): " + JSON.stringify(roles));
644645
}
645646
}
@@ -673,7 +674,7 @@ var AccessControl = /** @class */ (function () {
673674
get: function () {
674675
return enums_1.Action;
675676
},
676-
enumerable: true,
677+
enumerable: false,
677678
configurable: true
678679
});
679680
Object.defineProperty(AccessControl, "Possession", {
@@ -684,7 +685,7 @@ var AccessControl = /** @class */ (function () {
684685
get: function () {
685686
return enums_1.Possession;
686687
},
687-
enumerable: true,
688+
enumerable: false,
688689
configurable: true
689690
});
690691
Object.defineProperty(AccessControl, "Error", {
@@ -695,7 +696,7 @@ var AccessControl = /** @class */ (function () {
695696
get: function () {
696697
return core_1.AccessControlError;
697698
},
698-
enumerable: true,
699+
enumerable: false,
699700
configurable: true
700701
});
701702
// -------------------------------

0 commit comments

Comments
 (0)