diff --git a/addons/api/mirage/factories/scope.js b/addons/api/mirage/factories/scope.js index e9c5ac09f..e945b9cab 100644 --- a/addons/api/mirage/factories/scope.js +++ b/addons/api/mirage/factories/scope.js @@ -7,7 +7,7 @@ export default factory.extend({ authorized_collection_actions: () => { return { users: ['create', 'list'], - // groups: ['create', 'list'], + groups: ['create', 'list'], // roles: ['create', 'list'], // 'auth-methods': ['create', 'list'], // sessopms: ['list'], diff --git a/ui/admin/app/routes/scopes/scope/groups.js b/ui/admin/app/routes/scopes/scope/groups.js index 7fa556182..1784799cc 100644 --- a/ui/admin/app/routes/scopes/scope/groups.js +++ b/ui/admin/app/routes/scopes/scope/groups.js @@ -11,7 +11,7 @@ export default class ScopesScopeGroupsRoute extends Route { @service intl; @service notify; @service session; - + @service can; // =methods /** @@ -26,8 +26,11 @@ export default class ScopesScopeGroupsRoute extends Route { * @return {Promise[GroupModel]} */ async model() { - const { id: scope_id } = this.modelFor('scopes.scope'); - return this.store.query('group', { scope_id }); + const scope = this.modelFor('scopes.scope'); + const { id: scope_id } = scope; + if (this.can.can('list collection', scope, { collection: 'groups' })) { + return this.store.query('group', { scope_id }); + } } // =actions diff --git a/ui/admin/app/routes/scopes/scope/groups/index.js b/ui/admin/app/routes/scopes/scope/groups/index.js index 7d2a20fb0..22e3d3593 100644 --- a/ui/admin/app/routes/scopes/scope/groups/index.js +++ b/ui/admin/app/routes/scopes/scope/groups/index.js @@ -1,3 +1,10 @@ import Route from '@ember/routing/route'; +export default class ScopesScopeGroupsIndexRoute extends Route { + // =methods -export default class ScopesScopeGroupsIndexRoute extends Route {} + setupController(controller) { + const scope = this.modelFor('scopes.scope'); + super.setupController(...arguments); + controller.setProperties({ scope }); + } +} diff --git a/ui/admin/app/templates/scopes/scope.hbs b/ui/admin/app/templates/scopes/scope.hbs index decc3194d..726d82ab5 100644 --- a/ui/admin/app/templates/scopes/scope.hbs +++ b/ui/admin/app/templates/scopes/scope.hbs @@ -100,10 +100,12 @@ {{t 'resources.user.title_plural'}} {{/if}} - - - {{t 'resources.group.title_plural'}} - + {{#if (can 'navigate collection' @model collection='groups')}} + + + {{t 'resources.group.title_plural'}} + + {{/if}} {{t 'resources.role.title_plural'}} diff --git a/ui/admin/app/templates/scopes/scope/groups/index.hbs b/ui/admin/app/templates/scopes/scope/groups/index.hbs index 7c25937c0..1e5f038db 100644 --- a/ui/admin/app/templates/scopes/scope/groups/index.hbs +++ b/ui/admin/app/templates/scopes/scope/groups/index.hbs @@ -13,10 +13,12 @@ - {{#if @model}} - {{t - 'titles.new' - }} + {{#if (can 'create collection' this.scope collection='groups')}} + {{#if @model}} + {{t + 'titles.new' + }} + {{/if}} {{/if}} @@ -70,11 +72,29 @@ > {{t 'resources.group.description'}} + {{#if (can 'list collection' this.scope collection='groups')}} + {{! can list (at least): show default welcome message}} + {{t 'resources.group.description'}} + {{else if (can 'create collection' this.scope collection='groups')}} + {{! can create (only): show create-but-not-list welcome message}} + {{t + 'descriptions.create-but-not-list' + resource=(t 'resources.group.title_plural') + }} + {{else}} + {{! can neither list nor create }} + {{t + 'descriptions.neither-list-nor-create' + resource=(t 'resources.group.title_plural') + }} + {{/if}} - - - {{t 'titles.new'}} - + {{#if (can 'create collection' this.scope collection='groups')}} + + + {{t 'titles.new'}} + + {{/if}} diff --git a/ui/admin/tests/acceptance/groups-test.js b/ui/admin/tests/acceptance/groups/create-test.js similarity index 71% rename from ui/admin/tests/acceptance/groups-test.js rename to ui/admin/tests/acceptance/groups/create-test.js index 38b95d60d..815e4a760 100644 --- a/ui/admin/tests/acceptance/groups-test.js +++ b/ui/admin/tests/acceptance/groups/create-test.js @@ -10,9 +10,10 @@ import { //invalidateSession, } from 'ember-simple-auth/test-support'; -module('Acceptance | groups', function (hooks) { +module('Acceptance | groups | create', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); + let orgURL; const instances = { scopes: { @@ -20,9 +21,9 @@ module('Acceptance | groups', function (hooks) { org: null, }, group: null, + orgScope: null, }; const urls = { - orgScope: null, groups: null, group: null, newGroup: null, @@ -30,17 +31,21 @@ module('Acceptance | groups', function (hooks) { hooks.beforeEach(function () { authenticateSession({}); - instances.scopes.global = this.server.create('scope', { id: 'global' }); - instances.scopes.org = this.server.create('scope', { - type: 'org', - scope: { id: 'global', type: 'global' }, - }); + instances.orgScope = this.server.create( + 'scope', + { + type: 'org', + scope: { id: 'global', type: 'global' }, + }, + 'withChildren' + ); instances.group = this.server.create('group', { - scope: instances.scopes.org, + scope: instances.orgScope, }); - urls.groups = `/scopes/${instances.scopes.org.id}/groups`; + urls.groups = `/scopes/${instances.orgScope.id}/groups`; urls.group = `${urls.groups}/${instances.group.id}`; urls.newGroup = `${urls.groups}/new`; + orgURL = `/scopes/${instances.orgScope.id}`; }); test('can create new group', async function (assert) { @@ -52,6 +57,24 @@ module('Acceptance | groups', function (hooks) { assert.equal(this.server.db.groups.length, groupsCount + 1); }); + test('can navigate to new groups route with proper authorization', async function (assert) { + assert.expect(2); + await visit(orgURL); + assert.ok( + instances.orgScope.authorized_collection_actions.groups.includes('create') + ); + assert.ok(find(`[href="${urls.groups}"]`)); + }); + + test('cannot navigate to new groups route without proper authorization', async function (assert) { + assert.expect(2); + instances.orgScope.authorized_collection_actions.groups = []; + await visit(orgURL); + assert.notOk( + instances.orgScope.authorized_collection_actions.groups.includes('create') + ); + assert.notOk(find(`[href="${urls.groups}"]`)); + }); test('can cancel new group creation', async function (assert) { assert.expect(2); const groupsCount = this.server.db.groups.length; diff --git a/ui/admin/tests/acceptance/groups/list-test.js b/ui/admin/tests/acceptance/groups/list-test.js new file mode 100644 index 000000000..f9229c19d --- /dev/null +++ b/ui/admin/tests/acceptance/groups/list-test.js @@ -0,0 +1,76 @@ +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 | groups | list', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + let orgURL; + + const instances = { + scopes: { + global: null, + org: null, + }, + orgScope: null, + }; + + const urls = { + scopes: { + org: null, + }, + groups: null, + }; + + hooks.beforeEach(function () { + instances.scopes.global = this.server.create('scope', { id: 'global' }); + instances.orgScope = this.server.create( + 'scope', + { + type: 'org', + scope: { id: 'global', type: 'global' }, + }, + 'withChildren' + ); + instances.group = this.server.create('group', { + scope: instances.orgScope, + }); + orgURL = `/scopes/${instances.orgScope.id}`; + + urls.groups = `/scopes/${instances.orgScope.id}/groups`; + authenticateSession({}); + }); + + test('can navigate to groups with proper authorization', async function (assert) { + assert.expect(2); + await visit(orgURL); + assert.ok( + instances.orgScope.authorized_collection_actions.groups.includes('list') + ); + assert.ok(find(`[href="${urls.groups}"]`)); + }); + + test('User cannot navigate to index without either list or create actions', async function (assert) { + assert.expect(2); + instances.orgScope.authorized_collection_actions.groups = []; + await visit(orgURL); + assert.notOk( + instances.orgScope.authorized_collection_actions.groups.includes('list') + ); + assert.notOk(find(`[href="${urls.groups}"]`)); + }); + + test('User can navigate to index with only create action', async function (assert) { + assert.expect(1); + instances.orgScope.authorized_collection_actions.groups = ['create']; + await visit(orgURL); + assert.ok(find(`[href="${urls.groups}"]`)); + }); +});