Merge pull request #108 from mabashian/7271-permissions-sorting

Fixed permissions checkboxes on list contents change
This commit is contained in:
Michael Abashian
2017-08-01 08:04:08 -04:00
committed by GitHub
6 changed files with 150 additions and 133 deletions

View File

@@ -13,7 +13,14 @@
export default ['$rootScope', '$scope', 'GetBasePath', 'Rest', '$q', 'Wait', 'ProcessErrors', function(rootScope, scope, GetBasePath, Rest, $q, Wait, ProcessErrors) { export default ['$rootScope', '$scope', 'GetBasePath', 'Rest', '$q', 'Wait', 'ProcessErrors', function(rootScope, scope, GetBasePath, Rest, $q, Wait, ProcessErrors) {
scope.allSelected = []; init();
function init(){
let resources = ['users', 'teams'];
scope.allSelected = {};
_.each(resources, (type) => scope.allSelected[type] = {});
// the object permissions are being added to // the object permissions are being added to
scope.object = scope.resourceData.data; scope.object = scope.resourceData.data;
@@ -37,24 +44,15 @@ export default ['$rootScope', '$scope', 'GetBasePath', 'Rest', '$q', 'Wait', 'Pr
scope.showKeyPane = false; scope.showKeyPane = false;
scope.removeObject = function(obj){ scope.tab = {
_.remove(scope.allSelected, {id: obj.id}); users: true,
obj.isSelected = false; teams: false,
};
scope.toggleKeyPane = function() {
scope.showKeyPane = !scope.showKeyPane;
};
// handle form tab changes
scope.toggleFormTabs = function(list) {
scope.usersSelected = (list === 'users');
scope.teamsSelected = !scope.usersSelected;
}; };
// pop/push into unified collection of selected users & teams // pop/push into unified collection of selected users & teams
scope.$on("selectedOrDeselected", function(e, value) { scope.$on("selectedOrDeselected", function(e, value) {
let item = value.value; let resourceType = scope.currentTab(),
item = value.value;
function buildName(user) { function buildName(user) {
return (user.first_name && return (user.first_name &&
@@ -68,38 +66,55 @@ export default ['$rootScope', '$scope', 'GetBasePath', 'Rest', '$q', 'Wait', 'Pr
if (item.type === 'user') { if (item.type === 'user') {
item.name = buildName(item); item.name = buildName(item);
} }
scope.allSelected.push(item); scope.allSelected[resourceType][item.id] = item;
scope.allSelected[resourceType][item.id].roles = [];
} else { } else {
_.remove(scope.allSelected, { id: item.id }); delete scope.allSelected[resourceType][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 || []) scope.currentTab = function(){
.map(function(role) { return _.findKey(scope.tab, (tab) => tab);
return {
url: url,
id: role.value || role.id
}; };
scope.removeObject = function(obj){
_.remove(scope.allSelected, {id: obj.id});
obj.isSelected = false;
};
scope.toggleKeyPane = function() {
scope.showKeyPane = !scope.showKeyPane;
};
scope.hasSelectedRows = function(){
return _.any(scope.allSelected, (type) => Object.keys(type).length > 0);
};
scope.selectTab = function(selected){
_.each(scope.tab, (value, key, collection) => {
collection[key] = (selected === key);
}); });
})); };
}, true);
// post roles to api // post roles to api
scope.updatePermissions = function() { scope.updatePermissions = function() {
Wait('start'); Wait('start');
var requests = scope.posts let requests = [];
.map(function(post) {
Rest.setUrl(post.url); _.forEach(scope.allSelected, (selectedValues) => {
return Rest.post({ "id": post.id }); _.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) $q.all(requests)

View File

@@ -21,7 +21,7 @@ export default ['templateUrl', '$state',
controller: controller, controller: controller,
templateUrl: templateUrl('access/add-rbac-resource/rbac-resource'), templateUrl: templateUrl('access/add-rbac-resource/rbac-resource'),
link: function(scope, element, attrs) { link: function(scope, element, attrs) {
scope.toggleFormTabs('users'); scope.selectTab('users');
$('#add-permissions-modal').modal('show'); $('#add-permissions-modal').modal('show');
scope.closeModal = function() { scope.closeModal = function() {

View File

@@ -33,29 +33,27 @@
<div class="Form-tabHolder" ng-hide='withoutTeamPermissions'> <div class="Form-tabHolder" ng-hide='withoutTeamPermissions'>
<div id="users_tab" class="Form-tab" <div id="users_tab" class="Form-tab"
ng-click="toggleFormTabs('users')" ng-click="selectTab('users')"
ng-class="{'is-selected': usersSelected }" translate> ng-class="{'is-selected': tab.users }" translate>
Users Users
</div> </div>
<div id="teams_tab" class="Form-tab" <div id="teams_tab" class="Form-tab"
ng-click="toggleFormTabs('teams')" ng-click="selectTab('teams')"
ng-class="{'is-selected': teamsSelected }" translate> ng-class="{'is-selected': tab.teams }" translate>
Teams Teams
</div> </div>
</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> <rbac-multiselect-list view="Users" all-selected="allSelected" dataset="usersDataset"></rbac-multiselect-list>
</div> </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> <rbac-multiselect-list view="Teams" all-selected="allSelected" dataset="teamsDataset"></rbac-multiselect-list>
</div> </div>
<div class="AddPermissions-separator" <span ng-show="hasSelectedRows()">
ng-show="allSelected && allSelected.length > 0"></div> <div class="AddPermissions-separator"></div>
<div class="AddPermissions-directions">
<div class="AddPermissions-directions"
ng-show="allSelected && allSelected.length > 0">
<span class="AddPermissions-directionNumber"> <span class="AddPermissions-directionNumber">
2 2
</span> </span>
@@ -80,8 +78,8 @@
</div> </div>
<form name="userForm" novalidate> <form name="userForm" novalidate>
<ng-form name="userRoleForm"> <ng-form name="userRoleForm">
<div class="AddPermissions-roleRow" <span ng-repeat="(type, collection) in allSelected">
ng-repeat="obj in allSelected"> <div class="AddPermissions-roleRow" ng-repeat="obj in collection">
<div class="AddPermissions-roleName"> <div class="AddPermissions-roleName">
<span class="AddPermissions-roleNameVal"> <span class="AddPermissions-roleNameVal">
{{ obj.name }} {{ obj.name }}
@@ -97,8 +95,10 @@
<i class="fa fa-times"></i> <i class="fa fa-times"></i>
</button> </button>
</div> </div>
</span>
</ng-form> </ng-form>
</form> </form>
</span>
</div> </div>
<div class="AddPermissions-footer"> <div class="AddPermissions-footer">
<div class="buttons Form-buttons AddPermissions-buttons"> <div class="buttons Form-buttons AddPermissions-buttons">
@@ -110,7 +110,7 @@
<button type="button" <button type="button"
class="btn btn-sm Form-saveButton" class="btn btn-sm Form-saveButton"
ng-click="updatePermissions()" ng-click="updatePermissions()"
ng-disabled="userRoleForm.$invalid || !allSelected || !allSelected.length" translate> ng-disabled="userRoleForm.$invalid || !allSelected || !hasSelectedRows()" translate>
Save Save
</button> </button>
</div> </div>

View File

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

View File

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

View File

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