Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure parent permissions are evaluated only once #345

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/permission-ui/authorization/StatePermissionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ function PermStatePermissionMap(PermPermissionMap) {
*/
function areSetStatePermissions(state) {
try {
return !!state.data.permissions;
// We check for hasOwnProperty here because ui-router lets the `data` property inherit from its parent
return Object.prototype.hasOwnProperty.call(state.data, 'permissions') && !!state.data.permissions;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using only hasOwnProperty should be sufficient I think. No need to recheck by calling property itself afterwards.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I generally agree except if someone uses null as the permissions value. Edge case though, I guess.

} catch (e) {
return false;
}
Expand Down
42 changes: 38 additions & 4 deletions test/unit/permission-ui/authorization/StatePermissionMap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,44 @@ describe('permission.ui', function () {
// GIVEN
var state = jasmine.createSpyObj('state', ['$$permissionState']);
state.$$permissionState.and.callFake(function () {
var parent = {permissions: {only: ['accepted'], except: ['denied']}};
var child = Object.create(parent);
child.permissions = {only: ['acceptedChild'], except: ['deniedChild']};

return {
path: [
{data: child},
{data: parent}
]
};
});

// WHEN
var map = new PermStatePermissionMap(state);

// THEN
expect(map.only).toEqual([['acceptedChild'], ['accepted']]);
expect(map.except).toEqual([['deniedChild'], ['denied']]);
expect(map.redirectTo).not.toBeDefined();
});

it('should not duplicate parent state inheritance if childs dont have permissions', function () {
// GIVEN
var state = jasmine.createSpyObj('state', ['$$permissionState']);
state.$$permissionState.and.callFake(function () {
var grandparent = {permissions: {only: ['accepted'], except: ['denied']}};
var parent = Object.create(grandparent);
parent.permissions = {
only: ['acceptedChild'],
except: ['deniedChild']
};
var child = Object.create(parent);

return {
path: [
{data: {permissions: {only: ['accepted'], except: ['denied']}}},
{data: {permissions: {only: ['acceptedChild'], except: ['deniedChild']}}}
{data: child},
{data: parent},
{data: grandparent}
]
};
});
Expand All @@ -33,8 +67,8 @@ describe('permission.ui', function () {
var map = new PermStatePermissionMap(state);

// THEN
expect(map.only).toEqual([['accepted'], ['acceptedChild']]);
expect(map.except).toEqual([['denied'], ['deniedChild']]);
expect(map.only).toEqual([['acceptedChild'], ['accepted']]);
expect(map.except).toEqual([['deniedChild'], ['denied']]);
expect(map.redirectTo).not.toBeDefined();
});
});
Expand Down