Skip to content

Commit e1fe88b

Browse files
committed
inheritsResource method added
1 parent 70181ad commit e1fe88b

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

dist/acl.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ angular.module('stylet.acl').service('AclService', ["AclRegistryService", functi
297297
* the existing Resource from which the newly added Resource will inherit.
298298
*
299299
* @param {string} resource
300-
* @param {string} parent
300+
* @param {string} [parent=null] parent
301301
* @return {AclService} Provides a fluent interface
302302
*/
303-
this.addResource = function (resource, parent /*null*/) {
303+
this.addResource = function (resource, parent) {
304304
parent = typeof parent === 'undefined' ? null : parent;
305305

306306
var resourceId = resource;
@@ -363,6 +363,47 @@ angular.module('stylet.acl').service('AclService', ["AclRegistryService", functi
363363
return _resources[resource] !== undefined;
364364
};
365365

366+
/**
367+
* Returns true if and only if $resource inherits from $inherit
368+
*
369+
* Both parameters may be either a Resource or a Resource identifier. If
370+
* $onlyParent is true, then $resource must inherit directly from
371+
* $inherit in order to return true. By default, this method looks
372+
* through the entire inheritance tree to determine whether $resource
373+
* inherits from $inherit through its ancestor Resources.
374+
*
375+
* @param {(AclResourceInterface|string)} resource
376+
* @param {(AclResourceInterface|string)} inherit
377+
* @param {boolean} [onlyParent=false] onlyParent
378+
* @return {boolean}
379+
*/
380+
this.inheritsResource = function (resource, inherit, onlyParent) {
381+
resource = self.getResource(resource);
382+
inherit = self.getResource(inherit);
383+
384+
var parentId = null;
385+
386+
if (_resources[resource].parent !== null) {
387+
parentId = _resources[resource].parent;
388+
if (parentId === inherit) {
389+
return true;
390+
} else if (onlyParent) {
391+
return false;
392+
}
393+
} else {
394+
return false;
395+
}
396+
397+
while (_resources[parentId].parent !== null) {
398+
parentId = _resources[parentId].parent;
399+
if (inherit === parentId) {
400+
return true;
401+
}
402+
}
403+
404+
return false;
405+
};
406+
366407
/**
367408
* Removes a Resource and all of its children
368409
*

src/acl-service.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ angular.module('stylet.acl').service('AclService', function (AclRegistryService)
296296
* the existing Resource from which the newly added Resource will inherit.
297297
*
298298
* @param {string} resource
299-
* @param {string} parent
299+
* @param {string} [parent=null] parent
300300
* @return {AclService} Provides a fluent interface
301301
*/
302-
this.addResource = function (resource, parent /*null*/) {
302+
this.addResource = function (resource, parent) {
303303
parent = typeof parent === 'undefined' ? null : parent;
304304

305305
var resourceId = resource;
@@ -362,6 +362,47 @@ angular.module('stylet.acl').service('AclService', function (AclRegistryService)
362362
return _resources[resource] !== undefined;
363363
};
364364

365+
/**
366+
* Returns true if and only if $resource inherits from $inherit
367+
*
368+
* Both parameters may be either a Resource or a Resource identifier. If
369+
* $onlyParent is true, then $resource must inherit directly from
370+
* $inherit in order to return true. By default, this method looks
371+
* through the entire inheritance tree to determine whether $resource
372+
* inherits from $inherit through its ancestor Resources.
373+
*
374+
* @param {(AclResourceInterface|string)} resource
375+
* @param {(AclResourceInterface|string)} inherit
376+
* @param {boolean} [onlyParent=false] onlyParent
377+
* @return {boolean}
378+
*/
379+
this.inheritsResource = function (resource, inherit, onlyParent) {
380+
resource = self.getResource(resource);
381+
inherit = self.getResource(inherit);
382+
383+
var parentId = null;
384+
385+
if (_resources[resource].parent !== null) {
386+
parentId = _resources[resource].parent;
387+
if (parentId === inherit) {
388+
return true;
389+
} else if (onlyParent) {
390+
return false;
391+
}
392+
} else {
393+
return false;
394+
}
395+
396+
while (_resources[parentId].parent !== null) {
397+
parentId = _resources[parentId].parent;
398+
if (inherit === parentId) {
399+
return true;
400+
}
401+
}
402+
403+
return false;
404+
};
405+
365406
/**
366407
* Removes a Resource and all of its children
367408
*

test/acl-service.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,35 @@ describe('ncAclService', function () {
143143
}).toThrow();
144144
});
145145

146+
it('ensures that an exception is thrown when a non-existent resource is specified to each parameter of inherits()', function () {
147+
AclService.addResource('Animal');
148+
149+
expect(function() {
150+
AclService.inheritsResource('nonexistent', 'Animal');
151+
}).toThrow();
152+
153+
expect(function() {
154+
AclService.inheritsResource('Animal', 'nonexistent');
155+
}).toThrow();
156+
});
157+
158+
it('tests basic resource inheritance', function () {
159+
AclService.addResource('city');
160+
AclService.addResource('building', 'city');
161+
AclService.addResource('room', 'building');
162+
163+
expect(AclService.inheritsResource('building', 'city', true)).toBeTruthy();
164+
expect(AclService.inheritsResource('room', 'building', true)).toBeTruthy();
165+
expect(AclService.inheritsResource('room', 'city')).toBeTruthy();
166+
expect(AclService.inheritsResource('room', 'city', true)).toBeFalsy();
167+
expect(AclService.inheritsResource('city', 'building')).toBeFalsy();
168+
expect(AclService.inheritsResource('building', 'room')).toBeFalsy();
169+
expect(AclService.inheritsResource('city', 'room')).toBeFalsy();
170+
171+
AclService.removeResource('building');
172+
expect(AclService.hasResource('room')).toBeFalsy();
173+
});
174+
146175
});
147176

148177
describe('can()', function () {

0 commit comments

Comments
 (0)