Skip to content

Commit 089ef7b

Browse files
committed
fix issue #10
1 parent 77eb89d commit 089ef7b

File tree

5 files changed

+65
-43
lines changed

5 files changed

+65
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## AccessControl - Change Log
22

3+
### **v1.5.2** (2017-07-02)
4+
5+
- Fixed an issue where the grants were not processed into the inner grants object structure; if an array is passed to `AccessControl` constructor. Fixes [issue #10](https://github.com/onury/accesscontrol/issues/10).
6+
37
### **v1.5.1** (2017-05-24)
48

59
- Fixed TS import issue. Use `import { AccessControl } from 'accesscontrol'` in TypeScript projects.

lib/AccessControl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var AccessControl = (function () {
106106
*/
107107
function AccessControl(grants) {
108108
if (grants === void 0) { grants = {}; }
109-
this._grants = grants;
109+
this.setGrants(grants);
110110
}
111111
// -------------------------------
112112
// PUBLIC METHODS

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
},
5555
"homepage": "https://github.com/onury/accesscontrol#readme",
5656
"devDependencies": {
57-
"@types/node": "^7.0.22",
57+
"@types/node": "^8.0.7",
5858
"docma": "^1.5.1",
5959
"jasmine": "^2.6.0",
6060
"jasmine-console-reporter": "^1.2.7",

src/AccessControl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class AccessControl {
110110
* definitions. See the structure of this object in the examples.
111111
*/
112112
constructor(grants:any = {}) {
113-
this._grants = grants;
113+
this.setGrants(grants);
114114
}
115115

116116
// -------------------------------

test/ac.spec.js

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
/* eslint brace-style:0, max-statements-per-line:0 */
2-
31
/**
42
* Test Suite: AccessControl (Core)
5-
* @author Onur Yıldırım (onur@cutepilot.com)
3+
* @author Onur Yıldırım <onur@cutepilot.com>
64
*/
75

8-
var AccessControl = require('../lib').AccessControl;
6+
const AccessControl = require('../lib').AccessControl;
7+
8+
function type(o) {
9+
return Object.prototype.toString.call(o).match(/\s(\w+)/i)[1].toLowerCase();
10+
}
911

1012
describe('Test Suite: Access Control', function () {
1113
'use strict';
1214

1315
// grant list fetched from DB (to be converted to a valid grants object)
14-
var grantList = [
16+
let grantList = [
1517
{ role: 'admin', resource: 'video', action: 'create:any', attributes: ['*'] },
1618
{ role: 'admin', resource: 'video', action: 'read:any', attributes: ['*'] },
1719
{ role: 'admin', resource: 'video', action: 'update:any', attributes: ['*'] },
@@ -24,7 +26,7 @@ describe('Test Suite: Access Control', function () {
2426
];
2527

2628
// valid grants object
27-
var grantsObject = {
29+
let grantsObject = {
2830
admin: {
2931
video: {
3032
'create:any': ['*'],
@@ -51,8 +53,24 @@ describe('Test Suite: Access Control', function () {
5153
// TESTS
5254
//----------------------------
5355

56+
it('should construct with grants array or object, output a grants object', function () {
57+
let ac = new AccessControl(grantList);
58+
let grants = ac.getGrants();
59+
expect(type(grants)).toEqual('object');
60+
expect(type(grants.admin)).toEqual('object');
61+
expect(grants.admin.video['create:any']).toEqual(jasmine.any(Array));
62+
// console.log(grants);
63+
64+
ac = new AccessControl(grantsObject);
65+
grants = ac.getGrants();
66+
expect(type(grants)).toEqual('object');
67+
expect(type(grants.admin)).toEqual('object');
68+
expect(grants.admin.video['create:any']).toEqual(jasmine.any(Array));
69+
});
70+
71+
5472
it('should add grants from flat list (db), check/remove roles and resources', function () {
55-
var ac = this.ac;
73+
let ac = this.ac;
5674
ac.setGrants(grantList);
5775
// console.log('grants', ac.getGrants());
5876
// console.log('resources', ac.getResources());
@@ -78,7 +96,7 @@ describe('Test Suite: Access Control', function () {
7896
});
7997

8098
it('should grant/deny access and check permissions', function () {
81-
var ac = this.ac,
99+
let ac = this.ac,
82100
attrs = ['*', '!size'];
83101

84102
ac.grant('user').createAny('photo', attrs);
@@ -121,7 +139,7 @@ describe('Test Suite: Access Control', function () {
121139
});
122140

123141
it('should chain grant methods and check permissions', function () {
124-
var ac = this.ac,
142+
let ac = this.ac,
125143
attrs = ['*'];
126144

127145
ac.grant('superadmin')
@@ -137,23 +155,23 @@ describe('Test Suite: Access Control', function () {
137155
});
138156

139157
it('should grant/deny access via object and check permissions', function () {
140-
var ac = this.ac,
158+
let ac = this.ac,
141159
attrs = ['*'];
142160

143-
var o1 = {
161+
let o1 = {
144162
role: 'moderator',
145163
resource: 'post',
146164
action: 'create:any', // action:possession
147165
attributes: ['*'] // grant only
148166
};
149-
var o2 = {
167+
let o2 = {
150168
role: 'moderator',
151169
resource: 'news',
152170
action: 'read', // separate action
153171
possession: 'own', // separate possession
154172
attributes: ['*'] // grant only
155173
};
156-
var o3 = {
174+
let o3 = {
157175
role: 'moderator',
158176
resource: 'book',
159177
// no action/possession set
@@ -187,7 +205,7 @@ describe('Test Suite: Access Control', function () {
187205
});
188206

189207
it('should grant/deny access (variation, chained)', function () {
190-
var ac = this.ac;
208+
let ac = this.ac;
191209
ac.setGrants(grantsObject);
192210

193211
expect(ac.can('admin').createAny('video').granted).toEqual(true);
@@ -254,7 +272,7 @@ describe('Test Suite: Access Control', function () {
254272
});
255273

256274
it('should switch-chain grant/deny roles', function () {
257-
var ac = this.ac;
275+
let ac = this.ac;
258276
ac.grant('r1')
259277
.createOwn('a')
260278
.grant('r2')
@@ -277,13 +295,13 @@ describe('Test Suite: Access Control', function () {
277295
});
278296

279297
it('deny should auto-set attributes to []', function () {
280-
var ac = this.ac;
298+
let ac = this.ac;
281299
ac.deny('user').createAny('book', ['*']);
282300
expect(ac.getGrants().user.book['create:any']).toEqual([]);
283301
});
284302

285303
it('should grant comma/semi-colon separated roles', function () {
286-
var ac = this.ac;
304+
let ac = this.ac;
287305
// also supporting comma/semi-colon separated roles
288306
ac.grant('role2; role3, editor; viewer, agent').createOwn('book');
289307
expect(ac.hasRole('role3')).toEqual(true);
@@ -292,7 +310,7 @@ describe('Test Suite: Access Control', function () {
292310
});
293311

294312
it('permission should also return queried role(s) and resource', function () {
295-
var ac = this.ac;
313+
let ac = this.ac;
296314
// also supporting comma/semi-colon separated roles
297315
ac.grant('foo, bar').createOwn('baz');
298316
expect(ac.can('bar').createAny('baz').granted).toEqual(false);
@@ -305,7 +323,7 @@ describe('Test Suite: Access Control', function () {
305323
});
306324

307325
it('should extend / remove roles', function () {
308-
var ac = this.ac;
326+
let ac = this.ac;
309327

310328
ac.grant('admin').createOwn('book');
311329
ac.extendRole('onur', 'admin');
@@ -333,14 +351,14 @@ describe('Test Suite: Access Control', function () {
333351
expect(ac.getGrants().admin.$extend).not.toContain('editor');
334352
expect(ac.getGrants().admin.$extend).not.toContain('agent');
335353

336-
expect(function () { ac.grant('roleX').extend('roleX'); }).toThrow();
337-
expect(function () { ac.grant(['admin2', 'roleX']).extend(['roleX', 'admin3']); }).toThrow();
354+
expect(() => ac.grant('roleX').extend('roleX')).toThrow();
355+
expect(() => ac.grant(['admin2', 'roleX']).extend(['roleX', 'admin3'])).toThrow();
338356

339357
// console.log(JSON.stringify(ac.getGrants(), null, ' '));
340358
});
341359

342360
it('should throw if grant or deny objects are invalid', function () {
343-
var o,
361+
let o,
344362
ac = this.ac;
345363

346364
o = {
@@ -349,61 +367,61 @@ describe('Test Suite: Access Control', function () {
349367
action: 'create:any',
350368
attributes: ['*'] // grant only
351369
};
352-
expect(function () { ac.grant(o); }).toThrow();
353-
expect(function () { ac.deny(o); }).toThrow();
370+
expect(() => ac.grant(o)).toThrow();
371+
expect(() => ac.deny(o)).toThrow();
354372

355373
o = {
356374
role: 'moderator',
357375
resource: null, // invalid resource, should be non-empty string
358376
action: 'create:any',
359377
attributes: ['*'] // grant only
360378
};
361-
expect(function () { ac.grant(o); }).toThrow();
362-
expect(function () { ac.deny(o); }).toThrow();
379+
expect(() => ac.grant(o)).toThrow();
380+
expect(() => ac.deny(o)).toThrow();
363381

364382
o = {
365383
role: 'admin',
366384
resource: 'post',
367385
action: 'put:any', // invalid action, should be create|read|update|delete
368386
attributes: ['*'] // grant only
369387
};
370-
expect(function () { ac.grant(o); }).toThrow();
371-
expect(function () { ac.deny(o); }).toThrow();
388+
expect(() => ac.grant(o)).toThrow();
389+
expect(() => ac.deny(o)).toThrow();
372390

373391
o = {
374392
role: 'admin',
375393
resource: 'post',
376394
action: null, // invalid action, should be create|read|update|delete
377395
attributes: ['*'] // grant only
378396
};
379-
expect(function () { ac.grant(o); }).toThrow();
380-
expect(function () { ac.deny(o); }).toThrow();
397+
expect(() => ac.grant(o)).toThrow();
398+
expect(() => ac.deny(o)).toThrow();
381399

382400
o = {
383401
role: 'admin',
384402
resource: 'post',
385403
action: 'create:all', // invalid possession, should be any|own or omitted
386404
attributes: ['*'] // grant only
387405
};
388-
expect(function () { ac.grant(o); }).toThrow();
389-
expect(function () { ac.deny(o); }).toThrow();
406+
expect(() => ac.grant(o)).toThrow();
407+
expect(() => ac.deny(o)).toThrow();
390408

391409
o = {
392410
role: 'admin2',
393411
resource: 'post',
394412
action: 'create', // possession omitted, will be set to any
395413
attributes: ['*'] // grant only
396414
};
397-
expect(function () { ac.grant(o); }).not.toThrow();
415+
expect(() => ac.grant(o)).not.toThrow();
398416
expect(ac.can('admin2').createAny('post').granted).toEqual(true);
399417
// possession "any" will also return granted=true for "own"
400418
expect(ac.can('admin2').createOwn('post').granted).toEqual(true);
401-
expect(function () { ac.deny(o); }).not.toThrow();
419+
expect(() => ac.deny(o)).not.toThrow();
402420

403421
});
404422

405423
it('should throw `AccessControlError`', function () {
406-
var ac = this.ac;
424+
let ac = this.ac;
407425
function grant() {
408426
ac.grant().createOwn();
409427
}
@@ -417,7 +435,7 @@ describe('Test Suite: Access Control', function () {
417435
});
418436

419437
it('should filter granted attributes', function () {
420-
var ac = this.ac,
438+
let ac = this.ac,
421439
attrs = ['*', '!account.balance.credit', '!account.id', '!secret'],
422440
data = {
423441
name: 'Company, LTD.',
@@ -438,9 +456,9 @@ describe('Test Suite: Access Control', function () {
438456
}
439457
};
440458
ac.grant('user').createOwn('company', attrs);
441-
var permission = ac.can('user').createOwn('company');
459+
let permission = ac.can('user').createOwn('company');
442460
expect(permission.granted).toEqual(true);
443-
var filtered = permission.filter(data);
461+
let filtered = permission.filter(data);
444462
expect(filtered.name).toEqual(jasmine.any(String));
445463
expect(filtered.address).toEqual(jasmine.any(Object));
446464
expect(filtered.address.city).toEqual('istanbul');
@@ -452,7 +470,7 @@ describe('Test Suite: Access Control', function () {
452470
});
453471

454472
it('Check with multiple roles changes grant list (issue #2)', function () {
455-
var ac = this.ac;
473+
let ac = this.ac;
456474
ac.grant('admin').updateAny('video')
457475
.grant(['user', 'admin']).updateOwn('video');
458476

@@ -472,7 +490,7 @@ describe('Test Suite: Access Control', function () {
472490
});
473491

474492
it('should grant/deny multiple roles and multiple resources', function () {
475-
var ac = this.ac;
493+
let ac = this.ac;
476494

477495
ac.grant('admin, user').createAny('profile, video');
478496
expect(ac.can('admin').createAny('profile').granted).toEqual(true);

0 commit comments

Comments
 (0)