Comparing version
@@ -1,2 +0,2 @@ | ||
import { Access, IAccessInfo, ICondition, Query, IQueryInfo, Permission } from './core'; | ||
import { Access, IAccessInfo, ICondition, Query, IQueryInfo, Permission } from "./core"; | ||
/** | ||
@@ -121,3 +121,3 @@ * @classdesc | ||
*/ | ||
extendRole(roles: string | string[], extenderRoles: string | string[], condition?: ICondition): Promise<AccessControl>; | ||
extendRole(roles: string | string[], extenderRoles: string | string[], condition?: ICondition): AccessControl; | ||
extendRoleSync(roles: string | string[], extenderRoles: string | string[], condition?: ICondition): AccessControl; | ||
@@ -153,7 +153,7 @@ /** | ||
/** | ||
* Get allowed grants when conditions are skipped | ||
return CommonUtil.getUnionGrantsOfRoles(this._grants, query); | ||
* @returns {IAccessInfo[]} - grants | ||
*/ | ||
* Get allowed grants when conditions are skipped | ||
return CommonUtil.getUnionGrantsOfRoles(this._grants, query); | ||
* @returns {IAccessInfo[]} - grants | ||
*/ | ||
allowedGrants(query: IQueryInfo): Promise<any[]>; | ||
@@ -160,0 +160,0 @@ allowedGrantsSync(query: IQueryInfo): any[]; |
@@ -139,7 +139,9 @@ "use strict"; | ||
var type = utils_1.CommonUtil.type(grantsObject); | ||
if (type === 'object') { | ||
if (type === "object") { | ||
this._grants = utils_1.CommonUtil.normalizeGrantsObject(grantsObject); | ||
} | ||
else if (type === 'array') { | ||
grantsObject.forEach(function (item) { return utils_1.CommonUtil.commitToGrants(_this._grants, item); }); | ||
else if (type === "array") { | ||
grantsObject.forEach(function (item) { | ||
return utils_1.CommonUtil.commitToGrants(_this._grants, item); | ||
}); | ||
} | ||
@@ -179,12 +181,4 @@ return this; | ||
AccessControl.prototype.extendRole = function (roles, extenderRoles, condition) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, utils_1.CommonUtil.extendRole(this._grants, roles, extenderRoles, condition)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, this]; | ||
} | ||
}); | ||
}); | ||
// When extending role we are not checking for conditions so we can use sync method | ||
return this.extendRoleSync(roles, extenderRoles, condition); | ||
}; | ||
@@ -247,7 +241,7 @@ AccessControl.prototype.extendRoleSync = function (roles, extenderRoles, condition) { | ||
/** | ||
* Get allowed grants when conditions are skipped | ||
return CommonUtil.getUnionGrantsOfRoles(this._grants, query); | ||
* @returns {IAccessInfo[]} - grants | ||
*/ | ||
* Get allowed grants when conditions are skipped | ||
return CommonUtil.getUnionGrantsOfRoles(this._grants, query); | ||
* @returns {IAccessInfo[]} - grants | ||
*/ | ||
AccessControl.prototype.allowedGrants = function (query) { | ||
@@ -471,3 +465,5 @@ return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
utils_1.CommonUtil.eachKey(this._grants, function (role) { return callback(role, _this._grants[role]); }); | ||
utils_1.CommonUtil.eachKey(this._grants, function (role) { | ||
return callback(role, _this._grants[role]); | ||
}); | ||
}; | ||
@@ -474,0 +470,0 @@ /** |
@@ -108,3 +108,3 @@ import { IAccessInfo } from '../core'; | ||
*/ | ||
extend(roles: string | string[]): Promise<Access>; | ||
extend(roles: string | string[]): Access; | ||
/** | ||
@@ -111,0 +111,0 @@ * Sets the roles to be extended for this `Access` instance. |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -172,12 +137,4 @@ var utils_1 = require("./../utils/"); | ||
Access.prototype.extend = function (roles) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, utils_1.CommonUtil.extendRole(this._grants, this._.role, roles)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, this]; | ||
} | ||
}); | ||
}); | ||
// When extending role we are not checking for conditions so we can use sync method | ||
return this.extendSync(roles); | ||
}; | ||
@@ -184,0 +141,0 @@ /** |
@@ -103,3 +103,3 @@ import { IQueryInfo, IAccessInfo, ICondition } from '../core'; | ||
*/ | ||
static extendRole(grants: any, roles: string | string[], extenderRoles: string | string[], condition?: ICondition): Promise<void>; | ||
static extendRole(grants: any, roles: string | string[], extenderRoles: string | string[], condition?: ICondition): void; | ||
static extendRoleSync(grants: any, roles: string | string[], extenderRoles: string | string[], condition?: ICondition): void; | ||
@@ -106,0 +106,0 @@ static matchesAllElement(values: any, predicateFn: (elm: any) => boolean): boolean; |
@@ -708,39 +708,3 @@ "use strict"; | ||
CommonUtil.extendRole = function (grants, roles, extenderRoles, condition) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var arrExtRoles, nonExistentExtRoles, allExtendingRoles, extensionScore; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
arrExtRoles = array_1.ArrayUtil.toStringArray(extenderRoles); | ||
if (!arrExtRoles) | ||
throw new core_1.AccessControlError("Invalid extender role(s): " + JSON.stringify(extenderRoles)); | ||
nonExistentExtRoles = this.getNonExistentRoles(grants, arrExtRoles); | ||
if (nonExistentExtRoles.length > 0) { | ||
throw new core_1.AccessControlError("Cannot extend with non-existent role(s): \"" + nonExistentExtRoles.join(', ') + "\""); | ||
} | ||
roles = array_1.ArrayUtil.toStringArray(roles); | ||
if (!roles) | ||
throw new core_1.AccessControlError("Invalid role(s): " + JSON.stringify(roles)); | ||
return [4 /*yield*/, this.getFlatRoles(grants, arrExtRoles, null, true)]; | ||
case 1: | ||
allExtendingRoles = _a.sent(); | ||
extensionScore = allExtendingRoles.reduce(function (total, role) { | ||
return total + grants[role].score; | ||
}, 0); | ||
roles.forEach(function (role) { | ||
if (allExtendingRoles.indexOf(role) >= 0) { | ||
throw new core_1.AccessControlError("Attempted to extend role \"" + role + "\" by itself."); | ||
} | ||
grants[role] = grants[role] || { score: 1 }; | ||
grants[role].score += extensionScore; | ||
grants[role].$extend = grants[role].$extend || {}; | ||
arrExtRoles.forEach(function (extRole) { | ||
grants[role].$extend[extRole] = grants[role].$extend[extRole] || {}; | ||
grants[role].$extend[extRole].condition = condition; | ||
}); | ||
}); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
CommonUtil.extendRoleSync(grants, roles, extenderRoles, condition); | ||
}; | ||
@@ -747,0 +711,0 @@ CommonUtil.extendRoleSync = function (grants, roles, extenderRoles, condition) { |
{ | ||
"name": "role-acl", | ||
"version": "4.3.2", | ||
"version": "4.3.3", | ||
"description": "Role, Attribute and Condition based Access Control for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -385,6 +385,4 @@ [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=73QY55FZWSPRJ) [](https://travis-ci.org/tensult/role-acl) [](https://codeclimate.com/github/tensult/role-acl/test_coverage) [](https://npmjs.org/package/role-acl) [](https://github.com/tensult/role-acl/issues) | ||
// first level of extension (extending with condition) | ||
ac.extendRoleSync('sports/editor', 'editor', {Fn: 'EQUALS', args: {category: 'sports'}}) // <-- Sync Example | ||
await ac.extendRole('sports/editor', 'editor', {Fn: 'EQUALS', args: {category: 'sports'}}); // <-- Async Example | ||
ac.extendRoleSync('politics/editor', 'editor', {Fn: 'EQUALS', args: {category: 'politics'}}); // <-- Sync Example | ||
await ac.extendRole('politics/editor', 'editor', {Fn: 'EQUALS', args: {category: 'politics'}}); // <-- Async Example | ||
ac.extendRole('sports/editor', 'editor', {Fn: 'EQUALS', args: {category: 'sports'}}); | ||
ac.extendRole('politics/editor', 'editor', {Fn: 'EQUALS', args: {category: 'politics'}}); | ||
@@ -403,4 +401,3 @@ | ||
// second level of extension (extending without condition) | ||
ac.extendRoleSync('sports-and-politics/editor', ['sports/editor', 'politics/editor']); // <-- Sync Example | ||
await ac.extendRole('sports-and-politics/editor', ['sports/editor', 'politics/editor']); // <-- Async Example | ||
ac.extendRole('sports-and-politics/editor', ['sports/editor', 'politics/editor']); | ||
permission = ac.can('sports-and-politics/editor').context({category: 'politics'}).execute('create').sync().on('post'); // <-- Sync Example | ||
@@ -412,9 +409,5 @@ permission = await ac.can('sports-and-politics/editor').context({category: 'politics'}).execute('create').on('post'); // <-- Async Example | ||
// third level of extension (extending with condition) | ||
ac.extendRoleSync('conditional/sports-and-politics/editor', 'sports-and-politics/editor', { | ||
ac.extendRole('conditional/sports-and-politics/editor', 'sports-and-politics/editor', { | ||
Fn: 'EQUALS', | ||
args: { status: 'draft' } | ||
}); // <-- Sync Example | ||
await ac.extendRole('conditional/sports-and-politics/editor', 'sports-and-politics/editor', { | ||
Fn: 'EQUALS', | ||
args: { status: 'draft' } | ||
}); // <-- Async Example | ||
@@ -438,9 +431,7 @@ | ||
ac.grant('user').execute('*').on('image'); | ||
ac.extendRoleSync('admin', 'user'); // <-- Sync Example | ||
await ac.extendRole('admin', 'user'); // <-- Async Example | ||
ac.extendRole('admin', 'user'); | ||
ac.grant('admin').execute('delete').on('article'); | ||
ac.grant('admin').execute('*').on('category'); | ||
ac.extendRoleSync('owner', 'admin'); // <-- Sync Example | ||
await ac.extendRole('owner', 'admin'); // <-- Async Example | ||
ac.extendRole('owner', 'admin'); // <-- Sync Example | ||
@@ -447,0 +438,0 @@ ac.grant('owner').execute('*').on('video'); |
Sorry, the diff of this file is too big to display
317219
-2.58%6366
-2.17%489
-1.81%