diff --git a/awx/ui/client/src/app.js b/awx/ui/client/src/app.js index 512ad43595..bde0407b20 100644 --- a/awx/ui/client/src/app.js +++ b/awx/ui/client/src/app.js @@ -841,11 +841,6 @@ var tower = angular.module('Tower', [ $rootScope.user_is_superuser = Authorization.getUserInfo('is_superuser'); $rootScope.user_is_system_auditor = Authorization.getUserInfo('is_system_auditor'); - Rest.setUrl($rootScope.current_user.related.admin_of_organizations); - Rest.get() - .success(function(data) { - $rootScope.current_user_admin_orgs = data.results.map(i => i.name); - }); // state the user refreshes we want to open the socket, except if the user is on the login page, which should happen after the user logs in (see the AuthService module for that call to OpenSocket) if (!_.contains($location.$$url, '/login')) { ConfigService.getConfig().then(function() { diff --git a/awx/ui/client/src/controllers/Credentials.js b/awx/ui/client/src/controllers/Credentials.js index 1696756ed3..aed17aee32 100644 --- a/awx/ui/client/src/controllers/Credentials.js +++ b/awx/ui/client/src/controllers/Credentials.js @@ -17,16 +17,6 @@ export function CredentialsList($scope, $rootScope, $location, $log, SelectionInit, GetChoices, Wait, $state, $filter) { ClearScope(); - $scope.canAdd = false; - - Rest.setUrl(GetBasePath('credentials')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } - }); - Wait('start'); var list = CredentialList, @@ -148,16 +138,6 @@ export function CredentialsAdd($scope, $rootScope, $compile, $location, $log, ReturnToCaller, ClearScope, GenerateList, SearchInit, PaginateInit, LookUpInit, OrganizationList, GetBasePath, GetChoices, Empty, KindChange, OwnerChange, FormSave, $state, CreateSelect2) { - - Rest.setUrl(GetBasePath('credentials')); - Rest.options() - .success(function(data) { - if (!data.actions.POST) { - $state.go("^"); - Alert('Permission Error', 'You do not have permission to add a credential.', 'alert-info'); - } - }); - ClearScope(); // Inject dynamic view diff --git a/awx/ui/client/src/controllers/Projects.js b/awx/ui/client/src/controllers/Projects.js index 2aa6d1ab9a..6c882cdd13 100644 --- a/awx/ui/client/src/controllers/Projects.js +++ b/awx/ui/client/src/controllers/Projects.js @@ -15,17 +15,14 @@ export function ProjectsList ($scope, $rootScope, $location, $log, $stateParams, Rest, Alert, ProjectList, GenerateList, Prompt, SearchInit, PaginateInit, ReturnToCaller, ClearScope, ProcessErrors, GetBasePath, SelectionInit, ProjectUpdate, Refresh, Wait, GetChoices, Empty, - Find, GetProjectIcon, GetProjectToolTip, $filter, $state) { + Find, GetProjectIcon, GetProjectToolTip, $filter, $state, rbacUiControlService) { ClearScope(); $scope.canAdd = false; - Rest.setUrl(GetBasePath('projects')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd('projects') + .then(function(canAdd) { + $scope.canAdd = canAdd; }); Wait('start'); @@ -378,7 +375,7 @@ ProjectsList.$inject = ['$scope', '$rootScope', '$location', '$log', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'ProcessErrors', 'GetBasePath', 'SelectionInit', 'ProjectUpdate', 'Refresh', 'Wait', 'GetChoices', 'Empty', 'Find', - 'GetProjectIcon', 'GetProjectToolTip', '$filter', '$state' + 'GetProjectIcon', 'GetProjectToolTip', '$filter', '$state', 'rbacUiControlService' ]; diff --git a/awx/ui/client/src/controllers/Teams.js b/awx/ui/client/src/controllers/Teams.js index 08ed3c5f8f..ca30f84699 100644 --- a/awx/ui/client/src/controllers/Teams.js +++ b/awx/ui/client/src/controllers/Teams.js @@ -14,17 +14,14 @@ export function TeamsList($scope, $rootScope, $location, $log, $stateParams, Rest, Alert, TeamList, GenerateList, Prompt, SearchInit, PaginateInit, ReturnToCaller, ClearScope, ProcessErrors, SetTeamListeners, GetBasePath, - SelectionInit, Wait, $state, Refresh, $filter) { + SelectionInit, Wait, $state, Refresh, $filter, rbacUiControlService) { ClearScope(); $scope.canAdd = false; - Rest.setUrl(GetBasePath('teams')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd('teams') + .then(function(canAdd) { + $scope.canAdd = canAdd; }); var list = TeamList, @@ -135,7 +132,7 @@ TeamsList.$inject = ['$scope', '$rootScope', '$location', '$log', '$stateParams', 'Rest', 'Alert', 'TeamList', 'generateList', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'ProcessErrors', 'SetTeamListeners', 'GetBasePath', 'SelectionInit', 'Wait', - '$state', 'Refresh', '$filter' + '$state', 'Refresh', '$filter', 'rbacUiControlService' ]; diff --git a/awx/ui/client/src/controllers/Users.js b/awx/ui/client/src/controllers/Users.js index 7033e6333d..0a00ed97c3 100644 --- a/awx/ui/client/src/controllers/Users.js +++ b/awx/ui/client/src/controllers/Users.js @@ -34,17 +34,14 @@ function user_type_sync($scope) { export function UsersList($scope, $rootScope, $location, $log, $stateParams, Rest, Alert, UserList, GenerateList, Prompt, SearchInit, PaginateInit, ReturnToCaller, ClearScope, ProcessErrors, GetBasePath, SelectionInit, - Wait, $state, Refresh, $filter) { + Wait, $state, Refresh, $filter, rbacUiControlService) { ClearScope(); $scope.canAdd = false; - Rest.setUrl(GetBasePath('users')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd('users') + .then(function(canAdd) { + $scope.canAdd = canAdd; }); var list = UserList, @@ -145,7 +142,7 @@ UsersList.$inject = ['$scope', '$rootScope', '$location', '$log', '$stateParams', 'Rest', 'Alert', 'UserList', 'generateList', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'ProcessErrors', 'GetBasePath', 'SelectionInit', 'Wait', '$state', - 'Refresh', '$filter' + 'Refresh', '$filter', 'rbacUiControlService' ]; diff --git a/awx/ui/client/src/inventories/list/inventory-list.controller.js b/awx/ui/client/src/inventories/list/inventory-list.controller.js index d70217e6c5..a8d5fc73e7 100644 --- a/awx/ui/client/src/inventories/list/inventory-list.controller.js +++ b/awx/ui/client/src/inventories/list/inventory-list.controller.js @@ -14,16 +14,13 @@ function InventoriesList($scope, $rootScope, $location, $log, $stateParams, $compile, $filter, sanitizeFilter, Rest, Alert, InventoryList, generateList, Prompt, SearchInit, PaginateInit, ReturnToCaller, ClearScope, ProcessErrors, GetBasePath, Wait, - Find, Empty, $state) { + Find, Empty, $state, rbacUiControlService) { $scope.canAdd = false; - Rest.setUrl(GetBasePath('inventory')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd('inventory') + .then(function(canAdd) { + $scope.canAdd = canAdd; }); var list = InventoryList, @@ -386,4 +383,4 @@ function InventoriesList($scope, $rootScope, $location, $log, export default ['$scope', '$rootScope', '$location', '$log', '$stateParams', '$compile', '$filter', 'sanitizeFilter', 'Rest', 'Alert', 'InventoryList', 'generateList', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', - 'ClearScope', 'ProcessErrors', 'GetBasePath', 'Wait', 'Find', 'Empty', '$state', InventoriesList]; + 'ClearScope', 'ProcessErrors', 'GetBasePath', 'Wait', 'Find', 'Empty', '$state', 'rbacUiControlService', InventoriesList]; diff --git a/awx/ui/client/src/inventories/manage/groups/groups-list.controller.js b/awx/ui/client/src/inventories/manage/groups/groups-list.controller.js index 2b3889de18..a2f843793b 100644 --- a/awx/ui/client/src/inventories/manage/groups/groups-list.controller.js +++ b/awx/ui/client/src/inventories/manage/groups/groups-list.controller.js @@ -5,9 +5,9 @@ *************************************************/ export default ['$scope', '$rootScope', '$state', '$stateParams', 'InventoryGroups', 'generateList', 'InventoryUpdate', 'GroupManageService', 'GroupsCancelUpdate', 'ViewUpdateStatus', - 'InventoryManageService', 'groupsUrl', 'SearchInit', 'PaginateInit', 'GetSyncStatusMsg', 'GetHostsStatusMsg', 'Rest', 'GetBasePath', + 'InventoryManageService', 'groupsUrl', 'SearchInit', 'PaginateInit', 'GetSyncStatusMsg', 'GetHostsStatusMsg', 'Rest', 'GetBasePath', 'rbacUiControlService', function($scope, $rootScope, $state, $stateParams, InventoryGroups, generateList, InventoryUpdate, GroupManageService, GroupsCancelUpdate, ViewUpdateStatus, - InventoryManageService, groupsUrl, SearchInit, PaginateInit, GetSyncStatusMsg, GetHostsStatusMsg, Rest, GetBasePath){ + InventoryManageService, groupsUrl, SearchInit, PaginateInit, GetSyncStatusMsg, GetHostsStatusMsg, Rest, GetBasePath, rbacUiControlService){ var list = InventoryGroups, view = generateList, pageSize = 20; @@ -15,12 +15,9 @@ $scope.canAdd = false; - Rest.setUrl(GetBasePath('inventory') + $scope.inventory_id + "/groups"); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd(GetBasePath('inventory') + $scope.inventory_id + "/groups") + .then(function(canAdd) { + $scope.canAdd = canAdd; }); diff --git a/awx/ui/client/src/inventories/manage/hosts/hosts-list.controller.js b/awx/ui/client/src/inventories/manage/hosts/hosts-list.controller.js index 88219d9715..f42b9c39b3 100644 --- a/awx/ui/client/src/inventories/manage/hosts/hosts-list.controller.js +++ b/awx/ui/client/src/inventories/manage/hosts/hosts-list.controller.js @@ -5,24 +5,21 @@ *************************************************/ export default ['$scope', '$rootScope', '$state', '$stateParams', 'InventoryHosts', 'generateList', 'InventoryManageService', 'HostManageService', - 'hostsUrl', 'SearchInit', 'PaginateInit', 'SetStatus', 'Prompt', 'Wait', 'inventoryData', '$filter', 'Rest', 'GetBasePath', + 'hostsUrl', 'SearchInit', 'PaginateInit', 'SetStatus', 'Prompt', 'Wait', 'inventoryData', '$filter', 'Rest', 'GetBasePath', 'rbacUiControlService', function($scope, $rootScope, $state, $stateParams, InventoryHosts, generateList, InventoryManageService, HostManageService, - hostsUrl, SearchInit, PaginateInit, SetStatus, Prompt, Wait, inventoryData, $filter, Rest, GetBasePath){ + hostsUrl, SearchInit, PaginateInit, SetStatus, Prompt, Wait, inventoryData, $filter, Rest, GetBasePath, rbacUiControlService){ var list = InventoryHosts, view = generateList, pageSize = 20; - $scope.canAdd = false; - $scope.inventory_id = $stateParams.inventory_id; - Rest.setUrl(GetBasePath('inventory') + $scope.inventory_id + "/hosts"); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + $scope.canAdd = false; + + rbacUiControlService.canAdd(GetBasePath('inventory') + $scope.inventory_id + "/hosts") + .then(function(canAdd) { + $scope.canAdd = canAdd; }); // The ncy breadcrumb directive will look at this attribute when attempting to bind to the correct scope. diff --git a/awx/ui/client/src/inventory-scripts/list/list.controller.js b/awx/ui/client/src/inventory-scripts/list/list.controller.js index f8ad33e7db..08e0db5f67 100644 --- a/awx/ui/client/src/inventory-scripts/list/list.controller.js +++ b/awx/ui/client/src/inventory-scripts/list/list.controller.js @@ -7,11 +7,11 @@ export default [ '$rootScope','Wait', 'generateList', 'inventoryScriptsListObject', 'GetBasePath' , 'SearchInit' , 'PaginateInit', 'Rest' , 'ProcessErrors', - 'Prompt', '$state', '$filter', + 'Prompt', '$state', '$filter', 'rbacUiControlService', function( $rootScope,Wait, GenerateList, inventoryScriptsListObject, GetBasePath, SearchInit, PaginateInit, - Rest, ProcessErrors, Prompt, $state, $filter + Rest, ProcessErrors, Prompt, $state, $filter, rbacUiControlService ) { var scope = $rootScope.$new(), defaultUrl = GetBasePath('inventory_scripts'), @@ -20,12 +20,9 @@ export default scope.canAdd = false; - Rest.setUrl(GetBasePath('inventory_scripts')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - scope.canAdd = true; - } + rbacUiControlService.canAdd("inventory_scripts") + .then(function(canAdd) { + scope.canAdd = canAdd; }); view.inject( list, { diff --git a/awx/ui/client/src/job-templates/list/job-templates-list.controller.js b/awx/ui/client/src/job-templates/list/job-templates-list.controller.js index a2d4eedf4e..4e50f43fec 100644 --- a/awx/ui/client/src/job-templates/list/job-templates-list.controller.js +++ b/awx/ui/client/src/job-templates/list/job-templates-list.controller.js @@ -10,25 +10,22 @@ export default 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'ProcessErrors', 'GetBasePath', 'JobTemplateForm', 'CredentialList', 'LookUpInit', 'InitiatePlaybookRun', 'Wait', '$compile', - '$state', '$filter', + '$state', '$filter', 'rbacUiControlService', function( $scope, $rootScope, $location, $log, $stateParams, Rest, Alert, JobTemplateList, GenerateList, Prompt, SearchInit, PaginateInit, ReturnToCaller, ClearScope, ProcessErrors, GetBasePath, JobTemplateForm, CredentialList, LookUpInit, InitiatePlaybookRun, - Wait, $compile, $state, $filter + Wait, $compile, $state, $filter, rbacUiControlService ) { ClearScope(); $scope.canAdd = false; - Rest.setUrl(GetBasePath('job_templates')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd("job_templates") + .then(function(canAdd) { + $scope.canAdd = canAdd; }); var list = JobTemplateList, diff --git a/awx/ui/client/src/lists/Credentials.js b/awx/ui/client/src/lists/Credentials.js index bebfc7ccfb..d9496b1807 100644 --- a/awx/ui/client/src/lists/Credentials.js +++ b/awx/ui/client/src/lists/Credentials.js @@ -58,8 +58,7 @@ export default ngClick: 'addCredential()', awToolTip: 'Create a new credential', actionClass: 'btn List-buttonSubmit', - buttonContent: '+ ADD', - ngShow: 'canAdd' + buttonContent: '+ ADD' } }, diff --git a/awx/ui/client/src/notifications/notification-templates-list/list.controller.js b/awx/ui/client/src/notifications/notification-templates-list/list.controller.js index 09bf1393a8..7fc279534b 100644 --- a/awx/ui/client/src/notifications/notification-templates-list/list.controller.js +++ b/awx/ui/client/src/notifications/notification-templates-list/list.controller.js @@ -8,12 +8,12 @@ export default [ '$rootScope','Wait', 'generateList', 'NotificationTemplatesList', 'GetBasePath' , 'SearchInit' , 'PaginateInit', 'Rest' , 'ProcessErrors', 'Prompt', '$state', 'GetChoices', 'Empty', 'Find', - 'ngToast', '$compile', '$filter', + 'ngToast', '$compile', '$filter', 'rbacUiControlService', function( $rootScope,Wait, GenerateList, NotificationTemplatesList, GetBasePath, SearchInit, PaginateInit, Rest, ProcessErrors, Prompt, $state, GetChoices, Empty, Find, ngToast, - $compile, $filter) { + $compile, $filter, rbacUiControlService) { var scope = $rootScope.$new(), defaultUrl = GetBasePath('notification_templates'), list = NotificationTemplatesList, @@ -26,12 +26,9 @@ export default scope.canAdd = false; - Rest.setUrl(GetBasePath('notification_templates')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - scope.canAdd = true; - } + rbacUiControlService.canAdd("notification_templates") + .then(function(canAdd) { + scope.canAdd = canAdd; }); scope.removePostRefresh = scope.$on('PostRefresh', function () { diff --git a/awx/ui/client/src/notifications/shared/notification-list-init.factory.js b/awx/ui/client/src/notifications/shared/notification-list-init.factory.js index ba13bea4c9..e8a711e783 100644 --- a/awx/ui/client/src/notifications/shared/notification-list-init.factory.js +++ b/awx/ui/client/src/notifications/shared/notification-list-init.factory.js @@ -15,13 +15,21 @@ */ export default ['Wait', 'GetBasePath', 'ProcessErrors', 'Rest', 'GetChoices', - '$state', - function(Wait, GetBasePath, ProcessErrors, Rest, GetChoices, $state) { + '$state', '$rootScope', + function(Wait, GetBasePath, ProcessErrors, Rest, GetChoices, $state, $rootScope) { return function(params) { var scope = params.scope, url = params.url, id = params.id; + scope.current_user_admin_orgs = []; + + Rest.setUrl($rootScope.current_user.related.admin_of_organizations); + Rest.get() + .success(function(data) { + scope.current_user_admin_orgs = data.results.map(i => i.name); + }); + scope.addNotificationTemplate = function(){ $state.go('notifications.add'); }; 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 00a5bb9569..13ea87ce82 100644 --- a/awx/ui/client/src/organizations/list/organizations-list.controller.js +++ b/awx/ui/client/src/organizations/list/organizations-list.controller.js @@ -8,23 +8,20 @@ export default ['$stateParams', '$scope', '$rootScope', '$location', '$log', '$compile', 'Rest', 'PaginateInit', 'SearchInit', 'OrganizationList', 'Alert', 'Prompt', 'ClearScope', 'ProcessErrors', 'GetBasePath', 'Wait', - '$state', 'generateList', 'Refresh', '$filter', + '$state', 'generateList', 'Refresh', '$filter', 'rbacUiControlService', function($stateParams, $scope, $rootScope, $location, $log, $compile, Rest, PaginateInit, SearchInit, OrganizationList, Alert, Prompt, ClearScope, ProcessErrors, GetBasePath, Wait, - $state, generateList, Refresh, $filter) { + $state, generateList, Refresh, $filter, rbacUiControlService) { ClearScope(); $scope.canAdd = false; - Rest.setUrl(GetBasePath('organizations')); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd("organizations") + .then(function(canAdd) { + $scope.canAdd = canAdd; }); var defaultUrl = GetBasePath('organizations'), diff --git a/awx/ui/client/src/scheduler/scheduler.controller.js b/awx/ui/client/src/scheduler/scheduler.controller.js index 16681e1b66..db8eaa7b29 100644 --- a/awx/ui/client/src/scheduler/scheduler.controller.js +++ b/awx/ui/client/src/scheduler/scheduler.controller.js @@ -14,11 +14,11 @@ export default [ '$scope', '$compile', '$location', '$stateParams', 'SchedulesList', 'Rest', 'ProcessErrors', 'ReturnToCaller', 'ClearScope', 'GetBasePath', 'Wait', - 'Find', 'LoadSchedulesScope', 'GetChoices', '$q', '$state', + 'Find', 'LoadSchedulesScope', 'GetChoices', '$q', '$state', 'rbacUiControlService', function ($scope, $compile, $location, $stateParams, SchedulesList, Rest, ProcessErrors, ReturnToCaller, ClearScope, GetBasePath, Wait, Find, LoadSchedulesScope, GetChoices, - $q, $state) { + $q, $state, rbacUiControlService) { var schedList = _.cloneDeep(SchedulesList); ClearScope(); @@ -51,12 +51,9 @@ export default [ $scope.canAdd = false; - Rest.setUrl(url); - Rest.options() - .success(function(data) { - if (data.actions.POST) { - $scope.canAdd = true; - } + rbacUiControlService.canAdd(url) + .then(function(canAdd) { + $scope.canAdd = canAdd; }); schedList.well = true; diff --git a/awx/ui/client/src/shared/main.js b/awx/ui/client/src/shared/main.js index 6279fbf1e7..60c812e12c 100644 --- a/awx/ui/client/src/shared/main.js +++ b/awx/ui/client/src/shared/main.js @@ -11,12 +11,14 @@ import lodashAsPromised from './lodash-as-promised'; import stringFilters from './string-filters/main'; import truncatedText from './truncated-text.directive'; import stateExtender from './stateExtender.provider'; +import rbacUiControl from './rbacUiControl'; export default angular.module('shared', [listGenerator.name, pagination.name, stringFilters.name, - 'ui.router' + 'ui.router', + rbacUiControl.name ]) .factory('lodashAsPromised', lodashAsPromised) .directive('truncatedText', truncatedText) diff --git a/awx/ui/client/src/shared/rbacUiControl.js b/awx/ui/client/src/shared/rbacUiControl.js new file mode 100644 index 0000000000..7ec06d66c2 --- /dev/null +++ b/awx/ui/client/src/shared/rbacUiControl.js @@ -0,0 +1,30 @@ +/************************************************* + * Copyright (c) 2015 Ansible, Inc. + * + * All Rights Reserved + *************************************************/ + +export default + angular.module('rbacUiControl', []) + .service('rbacUiControlService', ['$q', 'GetBasePath', 'Rest', function($q, GetBasePath, Rest){ + this.canAdd = function(apiPath) { + var canAddVal = $q.defer(); + + if (apiPath.indexOf("api/v1") > -1) { + Rest.setUrl(apiPath); + } else { + Rest.setUrl(GetBasePath(apiPath)); + } + + Rest.options() + .success(function(data) { + if (data.actions.POST) { + canAddVal.resolve(true); + } else { + canAddVal.reject(false); + } + }); + + return canAddVal.promise; + }; + }]);