feat: 🎸 roles
This commit is contained in:
1 parent
b652f759c8
commit
fc4dab606e
10 files changed
+183
-23
No files matched your search
@@ -29,7 +29,8 @@ export default class CollectionAbility extends Ability {
|
||||
* @return {boolean}
|
||||
*/
|
||||
hasAuthorizedCollectionAction(action) {
|
||||
const { authorized_collection_actions } = this.model;
|
||||
const authorized_collection_actions =
|
||||
(this.model || {}).authorized_collection_actions || {};
|
||||
const collection = this.collection || {};
|
||||
return authorized_collection_actions[collection]?.includes(action);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,11 @@ export default class BaseModel extends Model {
|
||||
})
|
||||
authorized_actions;
|
||||
|
||||
@attr({
|
||||
readOnly: true,
|
||||
})
|
||||
authorized_collection_actions;
|
||||
|
||||
set scopeModel(model) {
|
||||
if (model) {
|
||||
const json = model.serialize();
|
||||
|
||||
@@ -4,6 +4,12 @@ import { trait } from 'ember-cli-mirage';
|
||||
export default factory.extend({
|
||||
type: 'global',
|
||||
|
||||
authorized_collection_actions: () => {
|
||||
return {
|
||||
roles: ['create', 'list'],
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Generates realistic-ish IDs while still being deterministic.
|
||||
*/
|
||||
|
||||
@@ -32,6 +32,8 @@ titles:
|
||||
descriptions:
|
||||
empty-set: There are no items to display yet. You may be able to add items or try back later.
|
||||
origin-initialization: To get started, please enter your origin URL
|
||||
create-but-not-list: You do not have permission to list {resource}, but you can create new instances.
|
||||
neither-list-nor-create: You do not have permission to list or create {resource}.
|
||||
questions:
|
||||
delete-confirm: Are you sure you want to delete this resource?
|
||||
remove-confirm: Are you sure you want to remove this association?
|
||||
|
||||
@@ -11,6 +11,7 @@ export default class ScopesScopeRolesRoute extends Route {
|
||||
@service intl;
|
||||
@service notify;
|
||||
@service session;
|
||||
@service can;
|
||||
|
||||
// =methods
|
||||
|
||||
@@ -26,8 +27,11 @@ export default class ScopesScopeRolesRoute extends Route {
|
||||
* @return {Promise{[RoleModel]}}
|
||||
*/
|
||||
async model() {
|
||||
const { id: scope_id } = this.modelFor('scopes.scope');
|
||||
return this.store.query('role', { scope_id });
|
||||
const scope = this.modelFor('scopes.scope');
|
||||
const { id: scope_id } = scope;
|
||||
if (this.can.can('list collection', scope, { collection: 'roles' })) {
|
||||
return this.store.query('role', { scope_id });
|
||||
}
|
||||
}
|
||||
|
||||
// =actions
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
import Route from '@ember/routing/route';
|
||||
export default class ScopesScopeUsersIndexRoute extends Route {
|
||||
// =methods
|
||||
|
||||
export default class ScopesScopeRolesIndexRoute extends Route {}
|
||||
setupController(controller) {
|
||||
const scope = this.modelFor('scopes.scope');
|
||||
super.setupController(...arguments);
|
||||
controller.setProperties({ scope });
|
||||
}
|
||||
}
|
||||
@@ -102,10 +102,12 @@
|
||||
<Rose::Icon @name='app-icons/group' @size='medium' />
|
||||
{{t 'resources.group.title_plural'}}
|
||||
</nav.link>
|
||||
<nav.link @route='scopes.scope.roles'>
|
||||
<Rose::Icon @name='app-icons/roles' @size='medium' />
|
||||
{{t 'resources.role.title_plural'}}
|
||||
</nav.link>
|
||||
{{#if (can 'navigate collection' @model collection='roles')}}
|
||||
<nav.link @route='scopes.scope.roles'>
|
||||
<Rose::Icon @name='app-icons/roles' @size='medium' />
|
||||
{{t 'resources.role.title_plural'}}
|
||||
</nav.link>
|
||||
{{/if}}
|
||||
<nav.link @route='scopes.scope.auth-methods'>
|
||||
<Rose::Icon @name='app-icons/auth-method' @size='medium' />
|
||||
{{t 'resources.auth-method.title_plural'}}
|
||||
|
||||
@@ -15,10 +15,12 @@
|
||||
</page.header>
|
||||
|
||||
<page.actions>
|
||||
{{#if @model}}
|
||||
<Rose::LinkButton @route='scopes.scope.roles.new' @style='primary'>{{t
|
||||
'titles.new'
|
||||
}}</Rose::LinkButton>
|
||||
{{#if (can 'create collection' this.scope collection='users')}}
|
||||
{{#if @model}}
|
||||
<Rose::LinkButton @route='scopes.scope.roles.new' @style='primary'>{{t
|
||||
'titles.new'
|
||||
}}</Rose::LinkButton>
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
</page.actions>
|
||||
|
||||
@@ -68,12 +70,29 @@
|
||||
as |message|
|
||||
>
|
||||
<message.description>
|
||||
{{t 'resources.role.description'}}
|
||||
{{#if (can 'list collection' this.scope collection='roles')}}
|
||||
{{! can list (at least): show default welcome message}}
|
||||
{{t 'resources.roles.description'}}
|
||||
{{else if (can 'create collection' this.scope collection='roles')}}
|
||||
{{! can create (only): show create-but-not-list welcome message}}
|
||||
{{t
|
||||
'descriptions.create-but-not-list'
|
||||
resource=(t 'resources.role.title_plural')
|
||||
}}
|
||||
{{else}}
|
||||
{{! can neither list nor create }}
|
||||
{{t
|
||||
'descriptions.neither-list-nor-create'
|
||||
resource=(t 'resources.role.title_plural')
|
||||
}}
|
||||
{{/if}}
|
||||
</message.description>
|
||||
<message.link @route='scopes.scope.roles.new'>
|
||||
<Rose::Icon @name='plus-circle-outline' />
|
||||
{{t 'titles.new'}}
|
||||
</message.link>
|
||||
{{#if (can 'create collection' this.scope collection='roles')}}
|
||||
<message.link @route='scopes.scope.roles.new'>
|
||||
<Rose::Icon @name='plus-circle-outline' />
|
||||
{{t 'titles.new'}}
|
||||
</message.link>
|
||||
{{/if}}
|
||||
</Rose::Message>
|
||||
</Rose::Layout::Centered>
|
||||
|
||||
|
||||
+36
-6
@@ -11,10 +11,13 @@ import {
|
||||
//invalidateSession,
|
||||
} from 'ember-simple-auth/test-support';
|
||||
|
||||
module('Acceptance | roles', function (hooks) {
|
||||
module('Acceptance | roles | create', function (hooks) {
|
||||
setupApplicationTest(hooks);
|
||||
setupMirage(hooks);
|
||||
|
||||
let orgURL;
|
||||
let orgScope;
|
||||
|
||||
const instances = {
|
||||
scopes: {
|
||||
global: null,
|
||||
@@ -27,10 +30,20 @@ module('Acceptance | roles', function (hooks) {
|
||||
orgScope: null,
|
||||
roles: null,
|
||||
role: null,
|
||||
newRole: null,
|
||||
newRoleURL: null,
|
||||
};
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
orgScope = this.server.create(
|
||||
'scope',
|
||||
{
|
||||
type: 'org',
|
||||
scope: { id: 'global', type: 'global' },
|
||||
},
|
||||
'withChildren'
|
||||
);
|
||||
|
||||
orgURL = `/scopes/${orgScope.id}`;
|
||||
authenticateSession({});
|
||||
instances.scopes.global = this.server.create('scope', { id: 'global' });
|
||||
instances.scopes.org = this.server.create('scope', {
|
||||
@@ -53,22 +66,39 @@ module('Acceptance | roles', function (hooks) {
|
||||
);
|
||||
urls.roles = `/scopes/${instances.scopes.org.id}/roles`;
|
||||
urls.role = `${urls.roles}/${instances.role.id}`;
|
||||
urls.newRole = `${urls.roles}/new`;
|
||||
urls.newRoleURL = `${urls.roles}/new`;
|
||||
});
|
||||
|
||||
test('can create new role', async function (assert) {
|
||||
assert.expect(1);
|
||||
const rolesCount = this.server.db.roles.length;
|
||||
await visit(urls.newRole);
|
||||
await visit(urls.newRoleURL);
|
||||
await fillIn('[name="name"]', 'role name');
|
||||
await click('[type="submit"]');
|
||||
assert.equal(this.server.db.roles.length, rolesCount + 1);
|
||||
});
|
||||
|
||||
test('can navigate to new roles route with proper authorization', async function (assert) {
|
||||
assert.expect(2);
|
||||
await visit(orgURL);
|
||||
assert.ok(orgScope.authorized_collection_actions.roles.includes('create'));
|
||||
assert.ok(find(`[href="${orgURL}/roles"]`));
|
||||
});
|
||||
|
||||
test('cannot navigate to new roles route without proper authorization', async function (assert) {
|
||||
assert.expect(2);
|
||||
orgScope.authorized_collection_actions.roles = [];
|
||||
await visit(orgURL);
|
||||
assert.notOk(
|
||||
orgScope.authorized_collection_actions.roles.includes('create')
|
||||
);
|
||||
assert.notOk(find(`[href="${orgURL}/roles"]`));
|
||||
});
|
||||
|
||||
test('can cancel new role creation', async function (assert) {
|
||||
assert.expect(2);
|
||||
const rolesCount = this.server.db.roles.length;
|
||||
await visit(urls.newRole);
|
||||
await visit(urls.newRoleURL);
|
||||
await fillIn('[name="name"]', 'role name');
|
||||
await click('.rose-form-actions [type="button"]');
|
||||
assert.equal(currentURL(), urls.roles);
|
||||
@@ -96,7 +126,7 @@ module('Acceptance | roles', function (hooks) {
|
||||
}
|
||||
);
|
||||
});
|
||||
await visit(urls.newRole);
|
||||
await visit(urls.newRoleURL);
|
||||
await fillIn('[name="name"]', 'new target');
|
||||
await click('form [type="submit"]');
|
||||
await a11yAudit();
|
||||
@@ -0,0 +1,84 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { visit, find } from '@ember/test-helpers';
|
||||
import { setupApplicationTest } from 'ember-qunit';
|
||||
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
||||
import {
|
||||
authenticateSession,
|
||||
// These are left here intentionally for future reference.
|
||||
//currentSession,
|
||||
//invalidateSession,
|
||||
} from 'ember-simple-auth/test-support';
|
||||
|
||||
module('Acceptance | roles | list', function (hooks) {
|
||||
setupApplicationTest(hooks);
|
||||
setupMirage(hooks);
|
||||
let orgURL;
|
||||
|
||||
const instances = {
|
||||
scopes: {
|
||||
global: null,
|
||||
org: null,
|
||||
},
|
||||
};
|
||||
|
||||
const urls = {
|
||||
scopes: {
|
||||
org: null,
|
||||
},
|
||||
roles: null,
|
||||
orgScope: null,
|
||||
};
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
instances.scopes.global = this.server.create('scope', { id: 'global' });
|
||||
urls.orgScope = this.server.create(
|
||||
'scope',
|
||||
{
|
||||
type: 'org',
|
||||
scope: { id: 'global', type: 'global' },
|
||||
},
|
||||
'withChildren'
|
||||
);
|
||||
instances.org = this.server.create(
|
||||
'scope',
|
||||
{
|
||||
type: 'org',
|
||||
scope: { id: 'global', type: 'global' },
|
||||
},
|
||||
'withChildren'
|
||||
);
|
||||
instances.role = this.server.create('role', {
|
||||
scope: instances.org,
|
||||
});
|
||||
orgURL = `/scopes/${urls.orgScope.id}`;
|
||||
|
||||
urls.roles = `/scopes/${urls.orgScope.id}/roles`;
|
||||
authenticateSession({});
|
||||
});
|
||||
|
||||
test('can navigate to roles with proper authorization', async function (assert) {
|
||||
assert.expect(2);
|
||||
await visit(orgURL);
|
||||
assert.ok(
|
||||
urls.orgScope.authorized_collection_actions.roles.includes('list')
|
||||
);
|
||||
assert.ok(find(`[href="${urls.roles}"]`));
|
||||
});
|
||||
|
||||
test('User cannot navigate to index without either list or create actions', async function (assert) {
|
||||
assert.expect(2);
|
||||
urls.orgScope.authorized_collection_actions.roles = [];
|
||||
await visit(orgURL);
|
||||
assert.notOk(
|
||||
urls.orgScope.authorized_collection_actions.roles.includes('list')
|
||||
);
|
||||
assert.notOk(find(`[href="${urls.roles}"]`));
|
||||
});
|
||||
|
||||
test('User can navigate to index with only create action', async function (assert) {
|
||||
assert.expect(1);
|
||||
urls.orgScope.authorized_collection_actions.roles = ['create'];
|
||||
await visit(orgURL);
|
||||
assert.ok(find(`[href="${urls.roles}"]`));
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user