diff --git a/awx/ui/client/src/access/add-rbac-resource/rbac-resource.controller.js b/awx/ui/client/src/access/add-rbac-resource/rbac-resource.controller.js index 1af53c230b..f283399998 100644 --- a/awx/ui/client/src/access/add-rbac-resource/rbac-resource.controller.js +++ b/awx/ui/client/src/access/add-rbac-resource/rbac-resource.controller.js @@ -13,29 +13,71 @@ export default ['$rootScope', '$scope', 'GetBasePath', 'Rest', '$q', 'Wait', 'ProcessErrors', function(rootScope, scope, GetBasePath, Rest, $q, Wait, ProcessErrors) { - scope.allSelected = []; + init(); - // the object permissions are being added to - scope.object = scope.resourceData.data; - // array for all possible roles for the object - scope.roles = _.omit(scope.object.summary_fields.object_roles, (key) => { - return key.name === 'Read'; - }); + function init(){ - // TODO: get working with api - // array w roles and descriptions for key - scope.roleKey = Object - .keys(scope.object.summary_fields.object_roles) - .map(function(key) { - return { - name: scope.object.summary_fields - .object_roles[key].name, - description: scope.object.summary_fields - .object_roles[key].description - }; + let resources = ['users', 'teams']; + + scope.allSelected = {}; + _.each(resources, (type) => scope.allSelected[type] = {}); + + // the object permissions are being added to + scope.object = scope.resourceData.data; + // array for all possible roles for the object + scope.roles = _.omit(scope.object.summary_fields.object_roles, (key) => { + return key.name === 'Read'; }); - scope.showKeyPane = false; + // TODO: get working with api + // array w roles and descriptions for key + scope.roleKey = Object + .keys(scope.object.summary_fields.object_roles) + .map(function(key) { + return { + name: scope.object.summary_fields + .object_roles[key].name, + description: scope.object.summary_fields + .object_roles[key].description + }; + }); + + scope.showKeyPane = false; + + scope.tab = { + users: true, + teams: false, + }; + + // pop/push into unified collection of selected users & teams + scope.$on("selectedOrDeselected", function(e, value) { + let resourceType = scope.currentTab(), + item = value.value; + + function buildName(user) { + return (user.first_name && + user.last_name) ? + user.first_name + " " + + user.last_name : + user.username; + } + + if (value.isSelected) { + if (item.type === 'user') { + item.name = buildName(item); + } + scope.allSelected[resourceType][item.id] = item; + scope.allSelected[resourceType][item.id].roles = []; + } else { + delete scope.allSelected[resourceType][item.id]; + } + }); + + } + + scope.currentTab = function(){ + return _.findKey(scope.tab, (tab) => tab); + }; scope.removeObject = function(obj){ _.remove(scope.allSelected, {id: obj.id}); @@ -46,61 +88,34 @@ export default ['$rootScope', '$scope', 'GetBasePath', 'Rest', '$q', 'Wait', 'Pr scope.showKeyPane = !scope.showKeyPane; }; - // handle form tab changes - scope.toggleFormTabs = function(list) { - scope.usersSelected = (list === 'users'); - scope.teamsSelected = !scope.usersSelected; + scope.showSection2Container = function(){ + return _.any(scope.allSelected, (type) => Object.keys(type).length > 0); }; - // pop/push into unified collection of selected users & teams - scope.$on("selectedOrDeselected", function(e, value) { - let item = value.value; - - function buildName(user) { - return (user.first_name && - user.last_name) ? - user.first_name + " " + - user.last_name : - user.username; - } - - if (value.isSelected) { - if (item.type === 'user') { - item.name = buildName(item); - } - scope.allSelected.push(item); - } else { - _.remove(scope.allSelected, { id: item.id }); - } - }); - - // update post url list - scope.$watch("allSelected", function(val) { - scope.posts = _ - .flatten((val || []) - .map(function(owner) { - var url = GetBasePath(owner.type + "s") + owner.id + - "/roles/"; - - return (owner.roles || []) - .map(function(role) { - return { - url: url, - id: role.value || role.id - }; - }); - })); - }, true); + scope.selectTab = function(selected){ + _.each(scope.tab, (value, key, collection) => { + collection[key] = (selected === key); + }); + }; // post roles to api scope.updatePermissions = function() { Wait('start'); - var requests = scope.posts - .map(function(post) { - Rest.setUrl(post.url); - return Rest.post({ "id": post.id }); + let requests = []; + + _.forEach(scope.allSelected, (selectedValues) => { + _.forEach(selectedValues, (selectedValue) => { + var url = GetBasePath(selectedValue.type + "s") + selectedValue.id + + "/roles/"; + + (selectedValue.roles || []) + .map(function(role) { + Rest.setUrl(url); + requests.push(Rest.post({ "id": role.value || role.id })); + }); }); + }); $q.all(requests) .then(function() { diff --git a/awx/ui/client/src/access/add-rbac-resource/rbac-resource.directive.js b/awx/ui/client/src/access/add-rbac-resource/rbac-resource.directive.js index 7c1ec307d9..f125c64afd 100644 --- a/awx/ui/client/src/access/add-rbac-resource/rbac-resource.directive.js +++ b/awx/ui/client/src/access/add-rbac-resource/rbac-resource.directive.js @@ -21,7 +21,7 @@ export default ['templateUrl', '$state', controller: controller, templateUrl: templateUrl('access/add-rbac-resource/rbac-resource'), link: function(scope, element, attrs) { - scope.toggleFormTabs('users'); + scope.selectTab('users'); $('#add-permissions-modal').modal('show'); scope.closeModal = function() { diff --git a/awx/ui/client/src/access/add-rbac-resource/rbac-resource.partial.html b/awx/ui/client/src/access/add-rbac-resource/rbac-resource.partial.html index e364b3110a..6ea6bb18a5 100644 --- a/awx/ui/client/src/access/add-rbac-resource/rbac-resource.partial.html +++ b/awx/ui/client/src/access/add-rbac-resource/rbac-resource.partial.html @@ -33,72 +33,72 @@