From f344d73f32d9e269e85522482141521f98a2d80d Mon Sep 17 00:00:00 2001 From: dharini Date: Tue, 31 Aug 2021 15:17:57 -0700 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20host=20catalogs=20cr?= =?UTF-8?q?eate/list=20capability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/abilities/collection.js | 3 +- addons/api/addon/models/base.js | 7 +- addons/api/mirage/factories/scope.js | 5 + addons/core/translations/en-us.yaml | 2 + .../app/routes/scopes/scope/host-catalogs.js | 11 ++- .../scopes/scope/host-catalogs/index.js | 8 +- ui/admin/app/templates/scopes/scope.hbs | 10 +- .../scopes/scope/host-catalogs/index.hbs | 47 ++++++--- .../create-test.js} | 41 +++++++- .../acceptance/host-catalogs/list-test.js | 98 +++++++++++++++++++ 10 files changed, 207 insertions(+), 25 deletions(-) rename ui/admin/tests/acceptance/{host-catalogs-test.js => host-catalogs/create-test.js} (74%) create mode 100644 ui/admin/tests/acceptance/host-catalogs/list-test.js diff --git a/addons/api/addon/abilities/collection.js b/addons/api/addon/abilities/collection.js index ad52afd18..92f0e4342 100644 --- a/addons/api/addon/abilities/collection.js +++ b/addons/api/addon/abilities/collection.js @@ -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); } diff --git a/addons/api/addon/models/base.js b/addons/api/addon/models/base.js index f99e0351a..5ce5d3a8c 100644 --- a/addons/api/addon/models/base.js +++ b/addons/api/addon/models/base.js @@ -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(); @@ -52,7 +57,7 @@ export default class BaseModel extends Model { /** * Convenience for getting and setting the parent scope ID. - * @type {string + * @type {string} */ @computed('scope.scope_id') get scopeID() { diff --git a/addons/api/mirage/factories/scope.js b/addons/api/mirage/factories/scope.js index fe41f3866..cfed04b66 100644 --- a/addons/api/mirage/factories/scope.js +++ b/addons/api/mirage/factories/scope.js @@ -3,6 +3,11 @@ import { trait } from 'ember-cli-mirage'; export default factory.extend({ type: 'global', + authorized_collection_actions: () => { + return { + 'host-catalogs': ['create', 'list'], + }; + }, /** * Generates realistic-ish IDs while still being deterministic. diff --git a/addons/core/translations/en-us.yaml b/addons/core/translations/en-us.yaml index 5370c7e75..8f98f633c 100644 --- a/addons/core/translations/en-us.yaml +++ b/addons/core/translations/en-us.yaml @@ -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? diff --git a/ui/admin/app/routes/scopes/scope/host-catalogs.js b/ui/admin/app/routes/scopes/scope/host-catalogs.js index 53fd92f7a..65da4edc1 100644 --- a/ui/admin/app/routes/scopes/scope/host-catalogs.js +++ b/ui/admin/app/routes/scopes/scope/host-catalogs.js @@ -11,7 +11,7 @@ export default class ScopesScopeHostCatalogsRoute extends Route { @service intl; @service notify; @service session; - + @service can; // =methods /** @@ -26,8 +26,13 @@ export default class ScopesScopeHostCatalogsRoute extends Route { * @return {Promise{[HostCatalogModel]}} */ async model() { - const { id: scope_id } = this.modelFor('scopes.scope'); - return this.store.query('host-catalog', { scope_id }); + const scope = this.modelFor('scopes.scope'); + const { id: scope_id } = scope; + if ( + this.can.can('list collection', scope, { collection: 'host-catalogs' }) + ) { + return this.store.query('host-catalog', { scope_id }); + } } // =actions diff --git a/ui/admin/app/routes/scopes/scope/host-catalogs/index.js b/ui/admin/app/routes/scopes/scope/host-catalogs/index.js index 90fba54e3..9261d003a 100644 --- a/ui/admin/app/routes/scopes/scope/host-catalogs/index.js +++ b/ui/admin/app/routes/scopes/scope/host-catalogs/index.js @@ -1,3 +1,9 @@ import Route from '@ember/routing/route'; -export default class ScopesScopeHostCatalogsIndexRoute extends Route {} +export default class ScopesScopeHostCatalogsIndexRoute 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 65e456c51..da31bfa24 100644 --- a/ui/admin/app/templates/scopes/scope.hbs +++ b/ui/admin/app/templates/scopes/scope.hbs @@ -79,10 +79,12 @@ {{t 'resources.target.title_plural'}} - - - {{t 'resources.host-catalog.title_plural'}} - + {{#if (can 'navigate collection' @model collection='host-catalogs')}} + + + {{t 'resources.host-catalog.title_plural'}} + + {{/if}} {{#if (feature-flag 'credential-store')}} diff --git a/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs b/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs index 734825303..db06323c7 100644 --- a/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs +++ b/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs @@ -15,13 +15,13 @@ - {{#if @model}} - - {{t 'titles.new'}} - + {{#if (can 'create collection' this.scope collection='host-catalogs')}} + {{#if @model}} + {{t 'titles.new'}} + {{/if}} {{/if}} @@ -82,12 +82,35 @@ as |message| > - {{t 'resources.host-catalog.description'}} + {{#if + (can 'list collection' this.scope collection='host-catalogs') + }} + {{! can list (at least): show default welcome message}} + {{t 'resources.host-catalog.description'}} + {{else if (can + 'create collection' this.scope collection='host-catalogs' + )}} + {{! can create (only): show create-but-not-list welcome message}} + {{t + 'descriptions.create-but-not-list' + resource=(t 'resources.host-catalog.title_plural') + }} + {{else}} + {{! can neither list nor create }} + {{t + 'descriptions.neither-list-nor-create' + resource=(t 'resources.host-catalog.title_plural') + }} + {{/if}} - - - {{t 'titles.new'}} - + {{#if + (can 'create collection' this.scope collection='host-catalogs') + }} + + + {{t 'titles.new'}} + + {{/if}} diff --git a/ui/admin/tests/acceptance/host-catalogs-test.js b/ui/admin/tests/acceptance/host-catalogs/create-test.js similarity index 74% rename from ui/admin/tests/acceptance/host-catalogs-test.js rename to ui/admin/tests/acceptance/host-catalogs/create-test.js index fea885978..8661e1eff 100644 --- a/ui/admin/tests/acceptance/host-catalogs-test.js +++ b/ui/admin/tests/acceptance/host-catalogs/create-test.js @@ -10,7 +10,7 @@ import { //invalidateSession, } from 'ember-simple-auth/test-support'; -module('Acceptance | host-catalogs', function (hooks) { +module('Acceptance | host-catalogs | create', function (hooks) { setupApplicationTest(hooks); setupMirage(hooks); @@ -22,6 +22,7 @@ module('Acceptance | host-catalogs', function (hooks) { org: null, project: null, }, + orgScope: null, }; const urls = { globalScope: null, @@ -35,6 +36,14 @@ module('Acceptance | host-catalogs', function (hooks) { hooks.beforeEach(function () { // Generate resources instances.scopes.global = this.server.create('scope', { id: 'global' }); + instances.orgScope = this.server.create( + 'scope', + { + type: 'org', + scope: { id: 'global', type: 'global' }, + }, + 'withChildren' + ); instances.scopes.org = this.server.create('scope', { type: 'org', scope: { id: 'global', type: 'global' }, @@ -46,17 +55,19 @@ module('Acceptance | host-catalogs', function (hooks) { instances.hostCatalog = this.server.create('host-catalog', { scope: instances.scopes.project, }); + // Generate route URLs for resources urls.globalScope = `/scopes/global/scopes`; - urls.orgScope = `/scopes/${instances.scopes.org.id}/scopes`; + urls.orgScope = `/scopes/${instances.orgScope.id}/scopes`; urls.projectScope = `/scopes/${instances.scopes.project.id}`; urls.hostCatalogs = `${urls.projectScope}/host-catalogs`; + urls.hostCatalog = `${urls.hostCatalogs}/${instances.hostCatalog.id}`; - urls.unknownHostCatalog = `${urls.hostCatalogs}/foo`; urls.newHostCatalog = `${urls.hostCatalogs}/new`; // Generate resource couner gethostCatalogCount = () => this.server.schema.hostCatalogs.all().models.length; + authenticateSession({}); }); @@ -79,6 +90,30 @@ module('Acceptance | host-catalogs', function (hooks) { assert.equal(gethostCatalogCount(), count); }); + test('can navigate to new host catalogs route with proper authorization', async function (assert) { + assert.expect(2); + await visit(urls.projectScope); + assert.ok( + instances.scopes.project.authorized_collection_actions[ + 'host-catalogs' + ].includes('create') + ); + assert.ok(find(`[href="${urls.hostCatalogs}"]`)); + }); + + test('cannot navigate to new host catalogs route without proper authorization', async function (assert) { + assert.expect(2); + instances.scopes.project.authorized_collection_actions['host-catalogs'] = + []; + await visit(urls.projectScope); + assert.notOk( + instances.scopes.project.authorized_collection_actions[ + 'host-catalogs' + ].includes('create') + ); + assert.notOk(find(`[href="${urls.hostCatalogs}"]`)); + }); + test('saving a new host catalog with invalid fields displays error messages', async function (assert) { assert.expect(2); this.server.post('/host-catalogs', () => { diff --git a/ui/admin/tests/acceptance/host-catalogs/list-test.js b/ui/admin/tests/acceptance/host-catalogs/list-test.js new file mode 100644 index 000000000..127784f4a --- /dev/null +++ b/ui/admin/tests/acceptance/host-catalogs/list-test.js @@ -0,0 +1,98 @@ +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 | host-catalogs | list', function (hooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + const instances = { + scopes: { + global: null, + org: null, + project: null, + }, + orgScope: null, + }; + + const urls = { + scopes: { + org: null, + }, + projectScope: null, + hostCatalogs: 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.scopes.org = this.server.create('scope', { + type: 'org', + scope: { id: 'global', type: 'global' }, + }); + instances.scopes.project = this.server.create('scope', { + type: 'project', + scope: { id: instances.scopes.org.id, type: 'org' }, + }); + instances.hostCatalog = this.server.create('host-catalog', { + scope: instances.scopes.project, + }); + + // orgURL = `/scopes/${instances.orgScope.id}`; + urls.hostCatalogs = `/scopes/${instances.orgScope.id}/host-catalogs`; + urls.globalScope = `/scopes/global/scopes`; + urls.orgScope = `/scopes/${instances.orgScope.id}/scopes`; + urls.projectScope = `/scopes/${instances.scopes.project.id}`; + urls.hostCatalogs = `${urls.projectScope}/host-catalogs`; + + urls.hostCatalog = `${urls.hostCatalogs}/${instances.hostCatalog.id}`; + authenticateSession({}); + }); + + test('can navigate to host catalogs with proper authorization', async function (assert) { + assert.expect(2); + await visit(urls.projectScope); + assert.ok( + instances.scopes.project.authorized_collection_actions[ + 'host-catalogs' + ].includes('list') + ); + assert.ok(find(`[href="${urls.hostCatalogs}"]`)); + }); + + test('User cannot navigate to index without either list or create actions', async function (assert) { + assert.expect(2); + instances.scopes.project.authorized_collection_actions['host-catalogs'] = + []; + await visit(urls.projectScope); + assert.notOk( + instances.scopes.project.authorized_collection_actions[ + 'host-catalogs' + ].includes('list') + ); + assert.notOk(find(`[href="${urls.hostCatalogs}"]`)); + }); + + test('User can navigate to index with only create action', async function (assert) { + assert.expect(1); + instances.scopes.project.authorized_collection_actions['host-catalogs'] = [ + 'create', + ]; + await visit(urls.projectScope); + assert.ok(find(`[href="${urls.hostCatalogs}"]`)); + }); +}); From 166b35860ace66e79233ab62b8dbebfab4b0686e Mon Sep 17 00:00:00 2001 From: dharini Date: Wed, 1 Sep 2021 15:29:09 -0700 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20=F0=9F=A4=96=20merge=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/templates/scopes/scope/host-catalogs/index.hbs | 4 +++- ui/admin/tests/acceptance/host-catalogs/create-test.js | 8 ++++---- ui/admin/tests/acceptance/host-catalogs/list-test.js | 3 +-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs b/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs index db06323c7..d5c7ab53a 100644 --- a/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs +++ b/ui/admin/app/templates/scopes/scope/host-catalogs/index.hbs @@ -20,7 +20,9 @@ {{t 'titles.new'}} + > + {{t 'titles.new'}} + {{/if}} {{/if}} diff --git a/ui/admin/tests/acceptance/host-catalogs/create-test.js b/ui/admin/tests/acceptance/host-catalogs/create-test.js index 8661e1eff..e95ecbde0 100644 --- a/ui/admin/tests/acceptance/host-catalogs/create-test.js +++ b/ui/admin/tests/acceptance/host-catalogs/create-test.js @@ -71,7 +71,7 @@ module('Acceptance | host-catalogs | create', function (hooks) { authenticateSession({}); }); - test('can create new host catalogs', async function (assert) { + test('Users can create new host catalogs', async function (assert) { assert.expect(1); const count = gethostCatalogCount(); await visit(urls.newHostCatalog); @@ -80,7 +80,7 @@ module('Acceptance | host-catalogs | create', function (hooks) { assert.equal(gethostCatalogCount(), count + 1); }); - test('can cancel create new host catalogs', async function (assert) { + test('Users can cancel create new host catalogs', async function (assert) { assert.expect(2); const count = gethostCatalogCount(); await visit(urls.newHostCatalog); @@ -90,7 +90,7 @@ module('Acceptance | host-catalogs | create', function (hooks) { assert.equal(gethostCatalogCount(), count); }); - test('can navigate to new host catalogs route with proper authorization', async function (assert) { + test('Users can navigate to new host catalogs route with proper authorization', async function (assert) { assert.expect(2); await visit(urls.projectScope); assert.ok( @@ -101,7 +101,7 @@ module('Acceptance | host-catalogs | create', function (hooks) { assert.ok(find(`[href="${urls.hostCatalogs}"]`)); }); - test('cannot navigate to new host catalogs route without proper authorization', async function (assert) { + test('Users cannot navigate to new host catalogs route without proper authorization', async function (assert) { assert.expect(2); instances.scopes.project.authorized_collection_actions['host-catalogs'] = []; diff --git a/ui/admin/tests/acceptance/host-catalogs/list-test.js b/ui/admin/tests/acceptance/host-catalogs/list-test.js index 127784f4a..6cf508e20 100644 --- a/ui/admin/tests/acceptance/host-catalogs/list-test.js +++ b/ui/admin/tests/acceptance/host-catalogs/list-test.js @@ -52,7 +52,6 @@ module('Acceptance | host-catalogs | list', function (hooks) { scope: instances.scopes.project, }); - // orgURL = `/scopes/${instances.orgScope.id}`; urls.hostCatalogs = `/scopes/${instances.orgScope.id}/host-catalogs`; urls.globalScope = `/scopes/global/scopes`; urls.orgScope = `/scopes/${instances.orgScope.id}/scopes`; @@ -63,7 +62,7 @@ module('Acceptance | host-catalogs | list', function (hooks) { authenticateSession({}); }); - test('can navigate to host catalogs with proper authorization', async function (assert) { + test('Users can navigate to host catalogs with proper authorization', async function (assert) { assert.expect(2); await visit(urls.projectScope); assert.ok( From 0ed506a111e5abfc5cf1d95d23eb9af701b88874 Mon Sep 17 00:00:00 2001 From: dharini Date: Thu, 2 Sep 2021 10:22:47 -0700 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=F0=9F=A4=96=20merge=20main=20+=20?= =?UTF-8?q?resolve=20conflicts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/mirage/factories/scope.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/addons/api/mirage/factories/scope.js b/addons/api/mirage/factories/scope.js index 523693a4e..f4c7073f7 100644 --- a/addons/api/mirage/factories/scope.js +++ b/addons/api/mirage/factories/scope.js @@ -3,11 +3,6 @@ import { trait } from 'ember-cli-mirage'; export default factory.extend({ type: 'global', - authorized_collection_actions: () => { - return { - 'host-catalogs': ['create', 'list'], - }; - }, authorized_collection_actions: () => { return { @@ -17,7 +12,7 @@ export default factory.extend({ // 'auth-methods': ['create', 'list'], // sessopms: ['list'], // targets: ['create', 'list'], - // 'host-catalogs': ['create', 'list'], + 'host-catalogs': ['create', 'list'], // 'credential-stores': ['create', 'list'], }; },