1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Fixed permissions checkboxes on list contents change

This commit is contained in:
mabashian 2017-07-28 17:46:24 -04:00
parent 9816ac418a
commit 5652ad0062
6 changed files with 150 additions and 133 deletions

View File

@ -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() {

View File

@ -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() {

View File

@ -33,72 +33,72 @@
<div class="Form-tabHolder" ng-hide='withoutTeamPermissions'>
<div id="users_tab" class="Form-tab"
ng-click="toggleFormTabs('users')"
ng-class="{'is-selected': usersSelected }" translate>
ng-click="selectTab('users')"
ng-class="{'is-selected': tab.users }" translate>
Users
</div>
<div id="teams_tab" class="Form-tab"
ng-click="toggleFormTabs('teams')"
ng-class="{'is-selected': teamsSelected }" translate>
ng-click="selectTab('teams')"
ng-class="{'is-selected': tab.teams }" translate>
Teams
</div>
</div>
<div id="AddPermissions-users" class="AddPermissions-list" ng-show="usersSelected">
<div id="AddPermissions-users" class="AddPermissions-list" ng-show="tab.users">
<rbac-multiselect-list view="Users" all-selected="allSelected" dataset="usersDataset"></rbac-multiselect-list>
</div>
<div id="AddPermissions-teams" class="AddPermissions-list" ng-if="teamsSelected">
<div id="AddPermissions-teams" class="AddPermissions-list" ng-if="tab.teams">
<rbac-multiselect-list view="Teams" all-selected="allSelected" dataset="teamsDataset"></rbac-multiselect-list>
</div>
<div class="AddPermissions-separator"
ng-show="allSelected && allSelected.length > 0"></div>
<div class="AddPermissions-directions"
ng-show="allSelected && allSelected.length > 0">
<span class="AddPermissions-directionNumber">
2
</span>
<translate>Please assign roles to the selected users/teams</translate>
<div class="AddPermissions-keyToggle btn"
ng-class="{'is-active': showKeyPane}"
ng-click="toggleKeyPane()" translate>
Key
</div>
</div>
<div class="AddPermissions-keyPane"
ng-show="showKeyPane">
<div class="AddPermissions-keyRow"
ng-repeat="key in roleKey | filter:{name: '!Read'}">
<div class="AddPermissions-keyName">
{{ key.name }}
</div>
<div class="AddPermissions-keyDescription">
{{ key.description || "No description provided" }}
<span ng-show="showSection2Container()">
<div class="AddPermissions-separator"></div>
<div class="AddPermissions-directions">
<span class="AddPermissions-directionNumber">
2
</span>
<translate>Please assign roles to the selected users/teams</translate>
<div class="AddPermissions-keyToggle btn"
ng-class="{'is-active': showKeyPane}"
ng-click="toggleKeyPane()" translate>
Key
</div>
</div>
</div>
<form name="userForm" novalidate>
<ng-form name="userRoleForm">
<div class="AddPermissions-roleRow"
ng-repeat="obj in allSelected">
<div class="AddPermissions-roleName">
<span class="AddPermissions-roleNameVal">
{{ obj.name }}
<div class="AddPermissions-keyPane"
ng-show="showKeyPane">
<div class="AddPermissions-keyRow"
ng-repeat="key in roleKey | filter:{name: '!Read'}">
<div class="AddPermissions-keyName">
{{ key.name }}
</div>
<div class="AddPermissions-keyDescription">
{{ key.description || "No description provided" }}
</div>
</div>
</div>
<form name="userForm" novalidate>
<ng-form name="userRoleForm">
<span ng-repeat="(type, collection) in allSelected">
<div class="AddPermissions-roleRow" ng-repeat="obj in collection">
<div class="AddPermissions-roleName">
<span class="AddPermissions-roleNameVal">
{{ obj.name }}
</span>
<span class="Form-title--roleType">
{{ obj.type }}
</span>
</div>
<rbac-multiselect-role class="AddPermissions-roleSelect" roles="roles" model="obj.roles">
</rbac-multiselect-role>
<button class="AddPermissions-roleRemove"
ng-click="removeObject(obj)">
<i class="fa fa-times"></i>
</button>
</div>
</span>
<span class="Form-title--roleType">
{{ obj.type }}
</span>
</div>
<rbac-multiselect-role class="AddPermissions-roleSelect" roles="roles" model="obj.roles">
</rbac-multiselect-role>
<button class="AddPermissions-roleRemove"
ng-click="removeObject(obj)">
<i class="fa fa-times"></i>
</button>
</div>
</ng-form>
</form>
</ng-form>
</form>
</span>
</div>
<div class="AddPermissions-footer">
<div class="buttons Form-buttons AddPermissions-buttons">
@ -110,7 +110,7 @@
<button type="button"
class="btn btn-sm Form-saveButton"
ng-click="updatePermissions()"
ng-disabled="userRoleForm.$invalid || !allSelected || !allSelected.length" translate>
ng-disabled="userRoleForm.$invalid || !allSelected || !showSection2Container()" translate>
Save
</button>
</div>

View File

@ -26,8 +26,8 @@ function(scope, $state, i18n, CreateSelect2, Rest, $q, Wait, ProcessErrors) {
// selected[type][id] === { roles: [ ... ], ... }
// collection of resources selected in section 1
scope.selected = {};
_.each(resources, (type) => scope.selected[type] = {});
scope.allSelected = {};
_.each(resources, (type) => scope.allSelected[type] = {});
// collection of assignable roles per type of resource
scope.keys = {};
@ -94,17 +94,17 @@ function(scope, $state, i18n, CreateSelect2, Rest, $q, Wait, ProcessErrors) {
};
scope.showSection2Container = function(){
return _.any(scope.selected, (type) => Object.keys(type).length > 0);
return _.any(scope.allSelected, (type) => Object.keys(type).length > 0);
};
scope.showSection2Tab = function(tab){
return Object.keys(scope.selected[tab]).length > 0;
return Object.keys(scope.allSelected[tab]).length > 0;
};
scope.saveEnabled = function(){
let missingRole = false;
let resourceSelected = false;
_.forOwn(scope.selected, function(value, key) {
_.forOwn(scope.allSelected, function(value, key) {
if(Object.keys(value).length > 0) {
// A resource from this tab has been selected
resourceSelected = true;
@ -129,11 +129,11 @@ function(scope, $state, i18n, CreateSelect2, Rest, $q, Wait, ProcessErrors) {
item = value.value;
if (value.isSelected) {
scope.selected[resourceType][item.id] = item;
scope.selected[resourceType][item.id].roles = [];
scope.allSelected[resourceType][item.id] = item;
scope.allSelected[resourceType][item.id].roles = [];
aggregateKey(item, resourceType);
} else {
delete scope.selected[resourceType][item.id];
delete scope.allSelected[resourceType][item.id];
}
});
@ -142,7 +142,7 @@ function(scope, $state, i18n, CreateSelect2, Rest, $q, Wait, ProcessErrors) {
//Wait('start');
// builds an array of role entities to apply to current user or team
let roles = _(scope.selected).map( (resources, type) => {
let roles = _(scope.allSelected).map( (resources, type) => {
return _.map(resources, (resource) => {
return resource.summary_fields.object_roles[scope.roleSelection[type]];
});

View File

@ -173,11 +173,11 @@
<!-- lists of selected resources -->
<!-- (type, collection) => ('resource', {id: {}, ... }) -->
<div ng-repeat="(type, collection) in selected">
<div ng-repeat="(type, collection) in allSelected">
<rbac-selected-list
resource-type="type"
collection="collection"
selected="selected"
selected="allSelected"
ng-show="tab[type]">
</rbac-selected-list>
</div>

View File

@ -172,9 +172,11 @@ export default ['addPermissionsTeamsList', 'addPermissionsUsersList', 'TemplateL
}
function isSelected(item){
if(_.find(scope.allSelected, {id: item.id, type: item.type})){
item.isSelected = true;
}
_.forEach(scope.allSelected[list.name], (selectedRow) => {
if(selectedRow.id === item.id) {
item.isSelected = true;
}
});
return item;
}
element.append(list_html);