Skip to content

Commit

Permalink
make hasPermissions accept an array of capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
andaley committed Jan 25, 2019
1 parent 4bcfb98 commit daa16c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
14 changes: 6 additions & 8 deletions ui/app/components/wizard/features-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,33 @@ export default Component.extend({
},

hasSecretsPermission: computed(function() {
return this.permissions.hasPermission('sys/mounts/example', 'update');
return this.permissions.hasPermission('sys/mounts/example', ['update']);
}),

hasAuthenticationPermission: computed(function() {
const canRead = this.permissions.hasPermission('sys/auth', 'read');
const canRead = this.permissions.hasPermission('sys/auth', ['read']);

const capabilities = ['update', 'sudo'];
const canUpdateOrCreate = capabilities.every(capability => {
return this.permissions.hasPermission('sys/auth/example', capability);
});
const canUpdateOrCreate = this.permissions.hasPermission('sys/auth/example', capabilities);

return canRead && canUpdateOrCreate;
}),

hasPoliciesPermission: computed(function() {
return this.permissions.hasPermission('sys/policies/acl', 'list');
return this.permissions.hasPermission('sys/policies/acl', ['list']);
}),

hasReplicationPermission: computed(function() {
const PATHS = ['sys/replication/performance/primary/enable', 'sys/replication/dr/primary/enable'];
return PATHS.every(path => {
return this.permissions.hasPermission(path, 'update');
return this.permissions.hasPermission(path, ['update']);
});
}),

hasToolsPermission: computed(function() {
const PATHS = ['sys/wrapping/wrap', 'sys/wrapping/lookup', 'sys/wrapping/unwrap', 'sys/wrapping/rewrap'];
return PATHS.every(path => {
return this.permissions.hasPermission(path, 'update');
return this.permissions.hasPermission(path, ['update']);
});
}),

Expand Down
20 changes: 13 additions & 7 deletions ui/app/services/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,23 @@ export default Service.extend({
}
},

hasPermission(pathName, capability) {
hasPermission(pathName, capabilities = []) {
const path = this.pathNameWithNamespace(pathName);

if (
this.canViewAll ||
this.hasMatchingExactPath(path, capability) ||
this.hasMatchingGlobPath(path, capability)
) {
if (this.canViewAll) {
return true;
}
return false;

if (capabilities.length) {
return capabilities.every(capability => {
if (this.hasMatchingExactPath(path, capability) || this.hasMatchingGlobPath(path, capability)) {
return true;
}
return false;
});
}

return this.hasMatchingExactPath(path) || this.hasMatchingGlobPath(path);
},

hasMatchingExactPath(pathName, capability) {
Expand Down
8 changes: 4 additions & 4 deletions ui/tests/unit/services/permissions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ module('Unit | Service | permissions', function(hooks) {
let service = this.owner.lookup('service:permissions');
service.set('exactPaths', PERMISSIONS_RESPONSE.data.exact_paths);
service.set('globPaths', PERMISSIONS_RESPONSE.data.glob_paths);
assert.equal(service.hasPermission('bar/bee', 'create'), true);
assert.equal(service.hasPermission('baz/biz', 'read'), true);
assert.equal(service.hasPermission('bar/bee', ['create']), true);
assert.equal(service.hasPermission('baz/biz', ['read']), true);
});

test('it returns false if a policy does not have the specified capabilities on a path', function(assert) {
let service = this.owner.lookup('service:permissions');
service.set('exactPaths', PERMISSIONS_RESPONSE.data.exact_paths);
service.set('globPaths', PERMISSIONS_RESPONSE.data.glob_paths);
assert.equal(service.hasPermission('bar/bee', 'delete'), false);
assert.equal(service.hasPermission('foo', 'create'), false);
assert.equal(service.hasPermission('bar/bee', ['delete']), false);
assert.equal(service.hasPermission('foo', ['create']), false);
});

test('defaults to show all items when policy cannot be found', async function(assert) {
Expand Down

0 comments on commit daa16c8

Please sign in to comment.