feat: 🎸 add ui-level navigation ability

This commit is contained in:
Randall Morey committed 2021-08-19 17:52:27 -04:00
1 parent 7fd7613c30
commit 4659b54e9e
4 files changed
+118

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
import CollectionAbility from 'api/abilities/collection';
import { inject as service } from '@ember/service';
export default class OverrideModelAbility extends CollectionAbility {
// =services
@service features;
// =permissions
/**
* Navigating to a resource is allowed if either list or create grants
* are present.
* @type {boolean}
*/
get canNavigate() {
return this.canList || this.canCreate;
}
// =methods
/**
* If the capabilities feature flag is disabled, actions are always
* authorized. This mimics pre-capabilities behavior.
*/
hasAuthorizedCollectionAction() {
if (!this.features.isEnabled('capabilities')) return true;
return super.hasAuthorizedCollectionAction(...arguments);
}
}
@@ -0,0 +1,29 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Abilities | Collection', function (hooks) {
setupTest(hooks);
test('it reflects when a resource may be navigated to based on list and create actions', function (assert) {
assert.expect(4);
const service = this.owner.lookup('service:can');
const model = {
authorized_collection_actions: { foobars: [] },
};
assert.notOk(
service.can('navigate collection', model, { collection: 'foobars' })
);
model.authorized_collection_actions.foobars = ['list'];
assert.ok(
service.can('navigate collection', model, { collection: 'foobars' })
);
model.authorized_collection_actions.foobars = ['create'];
assert.ok(
service.can('navigate collection', model, { collection: 'foobars' })
);
model.authorized_collection_actions.foobars = ['list', 'create'];
assert.ok(
service.can('navigate collection', model, { collection: 'foobars' })
);
});
});
+30
View File
@@ -0,0 +1,30 @@
import CollectionAbility from 'api/abilities/collection';
import { inject as service } from '@ember/service';
export default class OverrideModelAbility extends CollectionAbility {
// =services
@service features;
// =permissions
/**
* Navigating to a resource is allowed if either list or create grants
* are present.
* @type {boolean}
*/
get canNavigate() {
return this.canList || this.canCreate;
}
// =methods
/**
* If the capabilities feature flag is disabled, actions are always
* authorized. This mimics pre-capabilities behavior.
*/
hasAuthorizedCollectionAction() {
if (!this.features.isEnabled('capabilities')) return true;
return super.hasAuthorizedCollectionAction(...arguments);
}
}
@@ -0,0 +1,29 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Abilities | Collection', function (hooks) {
setupTest(hooks);
test('it reflects when a resource may be navigated to based on list and create actions', function (assert) {
assert.expect(4);
const service = this.owner.lookup('service:can');
const model = {
authorized_collection_actions: { foobars: [] },
};
assert.notOk(
service.can('navigate collection', model, { collection: 'foobars' })
);
model.authorized_collection_actions.foobars = ['list'];
assert.ok(
service.can('navigate collection', model, { collection: 'foobars' })
);
model.authorized_collection_actions.foobars = ['create'];
assert.ok(
service.can('navigate collection', model, { collection: 'foobars' })
);
model.authorized_collection_actions.foobars = ['list', 'create'];
assert.ok(
service.can('navigate collection', model, { collection: 'foobars' })
);
});
});