From 7f8fff18a0df26fe0a84ed0753e0f91f7ff6f9dc Mon Sep 17 00:00:00 2001 From: Jared Tabor Date: Mon, 12 Jan 2015 17:37:37 -0500 Subject: [PATCH] Permissions for org admin The series of events for checking a user's access level was happening out of order for loading the permissions page/form. I added a callback so that the access level can be checked first and then a secondary action (like filling a form as readonly or not) could happen next. --- awx/ui/static/js/controllers/Permissions.js | 130 +++++++++++--------- awx/ui/static/js/helpers/Access.js | 8 +- 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/awx/ui/static/js/controllers/Permissions.js b/awx/ui/static/js/controllers/Permissions.js index 8ed1b89e76..bb1cfd652f 100644 --- a/awx/ui/static/js/controllers/Permissions.js +++ b/awx/ui/static/js/controllers/Permissions.js @@ -215,78 +215,86 @@ function PermissionsEdit($scope, $rootScope, $compile, $location, $log, $routePa generator.reset(); - CheckAccess({ scope: $scope }); + $scope.selectCategory = function (resetIn) { var reset = (resetIn === false) ? false : true; PermissionCategoryChange({ scope: $scope, reset: reset }); }; - - // Retrieve detail record and prepopulate the form - Wait('start'); - Rest.setUrl(defaultUrl); - Rest.get() - .success(function (data) { - var fld, sourceModel, sourceField; - LoadBreadCrumbs({ path: '/users/' + base_id + '/permissions/' + id, title: data.name }); - for (fld in form.fields) { - if (data[fld]) { - if (form.fields[fld].sourceModel) { - sourceModel = form.fields[fld].sourceModel; - sourceField = form.fields[fld].sourceField; - $scope[sourceModel + '_' + sourceField] = data.summary_fields[sourceModel][sourceField]; - master[sourceModel + '_' + sourceField] = data.summary_fields[sourceModel][sourceField]; + if ($scope.removeFillForm) { + $scope.removeFillForm(); + } + $scope.removeFillForm = $scope.$on('FillForm', function () { + // Retrieve detail record and prepopulate the form + Wait('start'); + Rest.setUrl(defaultUrl); + Rest.get() + .success(function (data) { + var fld, sourceModel, sourceField; + LoadBreadCrumbs({ path: '/users/' + base_id + '/permissions/' + id, title: data.name }); + for (fld in form.fields) { + if (data[fld]) { + if (form.fields[fld].sourceModel) { + sourceModel = form.fields[fld].sourceModel; + sourceField = form.fields[fld].sourceField; + $scope[sourceModel + '_' + sourceField] = data.summary_fields[sourceModel][sourceField]; + master[sourceModel + '_' + sourceField] = data.summary_fields[sourceModel][sourceField]; + } + $scope[fld] = data[fld]; + master[fld] = $scope[fld]; } - $scope[fld] = data[fld]; - master[fld] = $scope[fld]; } - } - $scope.category = 'Deploy'; - if (data.permission_type !== 'run' && data.permission_type !== 'check') { - $scope.category = 'Inventory'; - } - master.category = $scope.category; - $scope.selectCategory(false); //call without resetting $scope.category value + $scope.category = 'Deploy'; + if (data.permission_type !== 'run' && data.permission_type !== 'check') { + $scope.category = 'Inventory'; + } + master.category = $scope.category; + $scope.selectCategory(false); //call without resetting $scope.category value - LookUpInit({ - scope: $scope, - form: form, - current_item: data.inventory, - list: InventoryList, - field: 'inventory', - input_type: "radio" - }); - - LookUpInit({ - scope: $scope, - form: form, - current_item: data.project, - list: ProjectList, - field: 'project', - input_type: 'radio' - }); - - if (!$scope.PermissionAddAllowed) { - // If not a privileged user, disable access - $('form[name="permission_form"]').find('select, input, button').each(function () { - if ($(this).is('input') || $(this).is('select')) { - $(this).attr('readonly', 'readonly'); - } - if ($(this).is('input[type="checkbox"]') || - $(this).is('input[type="radio"]') || - $(this).is('button')) { - $(this).attr('disabled', 'disabled'); - } + LookUpInit({ + scope: $scope, + form: form, + current_item: data.inventory, + list: InventoryList, + field: 'inventory', + input_type: "radio" }); - } - Wait('stop'); - }) - .error(function (data, status) { - ProcessErrors($scope, data, status, form, { hdr: 'Error!', - msg: 'Failed to retrieve Permission: ' + id + '. GET status: ' + status }); - }); + LookUpInit({ + scope: $scope, + form: form, + current_item: data.project, + list: ProjectList, + field: 'project', + input_type: 'radio' + }); + + if (!$scope.PermissionAddAllowed) { + // If not a privileged user, disable access + $('form[name="permission_form"]').find('select, input, button').each(function () { + if ($(this).is('input') || $(this).is('select')) { + $(this).attr('readonly', 'readonly'); + } + if ($(this).is('input[type="checkbox"]') || + $(this).is('input[type="radio"]') || + $(this).is('button')) { + $(this).attr('disabled', 'disabled'); + } + }); + } + Wait('stop'); + }) + .error(function (data, status) { + ProcessErrors($scope, data, status, form, { hdr: 'Error!', + msg: 'Failed to retrieve Permission: ' + id + '. GET status: ' + status }); + }); + }); + + CheckAccess({ + scope: $scope, + callback: 'FillForm' + }); // Save changes to the parent $scope.formSave = function () { diff --git a/awx/ui/static/js/helpers/Access.js b/awx/ui/static/js/helpers/Access.js index 2f5689a3ed..adaf205111 100644 --- a/awx/ui/static/js/helpers/Access.js +++ b/awx/ui/static/js/helpers/Access.js @@ -20,7 +20,9 @@ angular.module('AccessHelper', ['RestServices', 'Utilities']) return function (params) { // set PermissionAddAllowed to true or false based on user access. admins and org admins are granted // accesss. - var scope = params.scope, me; + var scope = params.scope, + callback = params.callback || undefined, + me; // uer may have refreshed the browser, in which case retrieve current user info from session cookie me = ($rootScope.current_user) ? $rootScope.current_user : $cookieStore.get('current_user'); @@ -37,6 +39,7 @@ angular.module('AccessHelper', ['RestServices', 'Utilities']) } else { scope.PermissionAddAllowed = false; } + }) .error(function (data, status) { ProcessErrors(scope, data, status, null, { @@ -47,6 +50,9 @@ angular.module('AccessHelper', ['RestServices', 'Utilities']) }); } } + if(callback){ + scope.$emit(callback); + } //if (!access) { // Alert('Access Denied', 'You do not have access to this function. Please contact your system administrator.'); //}