From 94ecfbee6a294808aa054cd50ea45cab0e90b62a Mon Sep 17 00:00:00 2001 From: mabashian Date: Wed, 11 Apr 2018 12:53:49 -0400 Subject: [PATCH] Check for org credentials and present the count to the user before org deletion --- awx/ui/client/lib/models/Organization.js | 19 ++++- .../lib/services/base-string.service.js | 1 + .../list/organizations-list.controller.js | 84 ++++++++++++------- .../projects/list/projects-list.controller.js | 2 +- 4 files changed, 72 insertions(+), 34 deletions(-) diff --git a/awx/ui/client/lib/models/Organization.js b/awx/ui/client/lib/models/Organization.js index 2e29f72473..6209889fba 100644 --- a/awx/ui/client/lib/models/Organization.js +++ b/awx/ui/client/lib/models/Organization.js @@ -1,21 +1,36 @@ let Base; +let Credential; + +function setDependentResources (id) { + this.dependentResources = [ + { + model: new Credential(), + params: { + organization: id + } + } + ]; +} function OrganizationModel (method, resource, config) { Base.call(this, 'organizations'); this.Constructor = OrganizationModel; + this.setDependentResources = setDependentResources.bind(this); return this.create(method, resource, config); } -function OrganizationModelLoader (BaseModel) { +function OrganizationModelLoader (BaseModel, CredentialModel) { Base = BaseModel; + Credential = CredentialModel; return OrganizationModel; } OrganizationModelLoader.$inject = [ - 'BaseModel' + 'BaseModel', + 'CredentialModel' ]; export default OrganizationModelLoader; diff --git a/awx/ui/client/lib/services/base-string.service.js b/awx/ui/client/lib/services/base-string.service.js index 11192aba29..8afb0071ef 100644 --- a/awx/ui/client/lib/services/base-string.service.js +++ b/awx/ui/client/lib/services/base-string.service.js @@ -73,6 +73,7 @@ function BaseStringService (namespace) { this.deleteResource = { HEADER: t.s('Delete'), USED_BY: resourceType => t.s('The {{ resourceType }} is currently being used by other resources.', { resourceType }), + UNAVAILABLE: resourceType => t.s('Deleting this {{ resourceType }} will make the following resources unavailable.', { resourceType }), CONFIRM: resourceType => t.s('Are you sure you want to delete this {{ resourceType }}?', { resourceType }) }; diff --git a/awx/ui/client/src/organizations/list/organizations-list.controller.js b/awx/ui/client/src/organizations/list/organizations-list.controller.js index 7b3ff2488e..48a61b0397 100644 --- a/awx/ui/client/src/organizations/list/organizations-list.controller.js +++ b/awx/ui/client/src/organizations/list/organizations-list.controller.js @@ -6,39 +6,40 @@ export default ['$stateParams', '$scope', '$rootScope', - 'Rest', 'OrganizationList', 'Prompt', - 'ProcessErrors', 'GetBasePath', 'Wait', '$state', 'rbacUiControlService', '$filter', 'Dataset', 'i18n', + 'Rest', 'OrganizationList', 'Prompt', 'OrganizationModel', + 'ProcessErrors', 'GetBasePath', 'Wait', '$state', + 'rbacUiControlService', '$filter', 'Dataset', 'i18n', + 'AppStrings', function($stateParams, $scope, $rootScope, - Rest, OrganizationList, Prompt, - ProcessErrors, GetBasePath, Wait, $state, rbacUiControlService, $filter, Dataset, i18n) { + Rest, OrganizationList, Prompt, Organization, + ProcessErrors, GetBasePath, Wait, $state, + rbacUiControlService, $filter, Dataset, i18n, + AppStrings + ) { var defaultUrl = GetBasePath('organizations'), list = OrganizationList; - init(); + $scope.canAdd = false; - function init() { - $scope.canAdd = false; + rbacUiControlService.canAdd("organizations") + .then(function(params) { + $scope.canAdd = params.canAdd; + }); + $scope.orgCount = Dataset.data.count; - rbacUiControlService.canAdd("organizations") - .then(function(params) { - $scope.canAdd = params.canAdd; - }); - $scope.orgCount = Dataset.data.count; + // search init + $scope.list = list; + $scope[`${list.iterator}_dataset`] = Dataset.data; + $scope[list.name] = $scope[`${list.iterator}_dataset`].results; - // search init - $scope.list = list; - $scope[`${list.iterator}_dataset`] = Dataset.data; - $scope[list.name] = $scope[`${list.iterator}_dataset`].results; + $scope.orgCards = parseCardData($scope[list.name]); + $rootScope.flashMessage = null; - $scope.orgCards = parseCardData($scope[list.name]); - $rootScope.flashMessage = null; - - // grab the pagination elements, move, destroy list generator elements - $('#organization-pagination').appendTo('#OrgCards'); - $('#organizations tag-search').appendTo('.OrgCards-search'); - $('#organizations-list').remove(); - } + // grab the pagination elements, move, destroy list generator elements + $('#organization-pagination').appendTo('#OrgCards'); + $('#organizations tag-search').appendTo('.OrgCards-search'); + $('#organizations-list').remove(); function parseCardData(cards) { return cards.map(function(card) { @@ -167,13 +168,34 @@ export default ['$stateParams', '$scope', '$rootScope', }); }; - Prompt({ - hdr: i18n._('Delete'), - resourceName: $filter('sanitize')(name), - body: '
' + i18n._('Are you sure you want to delete this organization? This makes everything in this organization unavailable.') + '
', - action: action, - actionText: i18n._('DELETE') - }); + const organization = new Organization(); + + organization.getDependentResourceCounts(id) + .then((counts) => { + const invalidateRelatedLines = []; + let deleteModalBody = `
${AppStrings.get('deleteResource.CONFIRM', 'organization')}
`; + + counts.forEach(countObj => { + if(countObj.count && countObj.count > 0) { + invalidateRelatedLines.push(`
${countObj.label}${countObj.count}
`); + } + }); + + if (invalidateRelatedLines && invalidateRelatedLines.length > 0) { + deleteModalBody = `
${AppStrings.get('deleteResource.UNAVAILABLE', 'organization')} ${AppStrings.get('deleteResource.CONFIRM', 'organization')}
`; + invalidateRelatedLines.forEach(invalidateRelatedLine => { + deleteModalBody += invalidateRelatedLine; + }); + } + + Prompt({ + hdr: i18n._('Delete'), + resourceName: $filter('sanitize')(name), + body: deleteModalBody, + action: action, + actionText: i18n._('DELETE') + }); + }); }; } ]; diff --git a/awx/ui/client/src/projects/list/projects-list.controller.js b/awx/ui/client/src/projects/list/projects-list.controller.js index 1bcd7db2d6..2c9525de26 100644 --- a/awx/ui/client/src/projects/list/projects-list.controller.js +++ b/awx/ui/client/src/projects/list/projects-list.controller.js @@ -241,7 +241,7 @@ export default ['$scope', '$rootScope', '$log', 'Rest', 'Alert', resourceName: $filter('sanitize')(name), body: deleteModalBody, action: action, - actionText: 'DELETE' + actionText: i18n._('DELETE') }); }); };