blob: 4e83606a6e627fb225b3a09be5fba5f0a13e5eb2 [file] [log] [blame]
window.angular && (function(angular) {
'use strict';
angular.module('app.common.directives').directive('ldapUserRoles', [
'APIUtils',
function(APIUtils) {
return {
restrict: 'E',
template: require('./ldap-user-roles.html'),
scope: {roleGroups: '=', enabled: '=', roleGroupType: '='},
controller: [
'$scope', 'APIUtils', 'toastService', '$q',
function($scope, APIUtils, toastService, $q) {
$scope.privileges = [];
$scope.loading = true;
$scope.newGroup = {};
$scope.selectedGroupIndex = '';
$scope.editGroup = false;
$scope.removeGroup = false;
$scope.removeMultipleGroups = false;
$scope.all = false;
$scope.sortPropertyName = 'id';
$scope.reverse = false;
$scope.addGroup = false;
$scope.hasSelectedGroup = false;
APIUtils.getAccountServiceRoles()
.then(
(data) => {
$scope.privileges = data;
},
(error) => {
console.log(JSON.stringify(error));
})
.finally(() => {
$scope.loading = false;
});
$scope.addGroupFn = () => {
$scope.addGroup = true;
};
$scope.addRoleGroup = () => {
const newGroup = {};
newGroup.RemoteGroup = $scope.newGroup.RemoteGroup;
newGroup.LocalRole = $scope.newGroup.LocalRole;
$scope.loading = true;
const data = {};
if ($scope.roleGroupType == 'ldap') {
data.LDAP = {};
data.LDAP.RemoteRoleMapping = $scope.roleGroups;
data.LDAP.RemoteRoleMapping.push(newGroup);
} else {
data.ActiveDirectory = {};
data.ActiveDirectory.RemoteRoleMapping = $scope.roleGroups;
data.ActiveDirectory.RemoteRoleMapping.push(newGroup);
}
APIUtils.saveLdapProperties(data)
.then(
(response) => {
toastService.success(
'Group has been created successfully.');
},
(error) => {
toastService.error('Failed to create new group.');
})
.finally(() => {
$scope.loading = false;
});
};
$scope.editGroupFn = (group, role, index) => {
$scope.editGroup = true;
$scope.selectedGroupIndex = index;
$scope.newGroup.RemoteGroup = group;
$scope.newGroup.LocalRole = role;
};
$scope.editRoleGroup = () => {
$scope.loading = true;
const data = {};
if ($scope.roleGroupType == 'ldap') {
data.LDAP = {};
data.LDAP.RemoteRoleMapping = $scope.roleGroups;
data.LDAP.RemoteRoleMapping[$scope.selectedGroupIndex]
.LocalRole = $scope.newGroup.LocalRole;
} else {
data.ActiveDirectory = {};
data.ActiveDirectory.RemoteRoleMapping = $scope.roleGroups;
data.ActiveDirectory
.RemoteRoleMapping[$scope.selectedGroupIndex]
.LocalRole = $scope.newGroup.LocalRole;
}
APIUtils.saveLdapProperties(data)
.then(
(response) => {
toastService.success(
'Group has been edited successfully.');
},
(error) => {
toastService.error('Failed to edit group.');
})
.finally(() => {
$scope.loading = false;
});
$scope.editGroup = false;
};
$scope.removeGroupFn = (index) => {
$scope.removeGroup = true;
$scope.selectedGroupIndex = index;
};
$scope.removeRoleGroup = () => {
$scope.loading = true;
const data = {};
if ($scope.roleGroupType == 'ldap') {
data.LDAP = {};
data.LDAP.RemoteRoleMapping = $scope.roleGroups;
data.LDAP.RemoteRoleMapping[$scope.selectedGroupIndex] =
$scope.newGroup;
} else {
data.ActiveDirectory = {};
data.ActiveDirectory.RemoteRoleMapping = $scope.roleGroups;
data.ActiveDirectory
.RemoteRoleMapping[$scope.selectedGroupIndex] =
$scope.newGroup;
}
$scope.roleGroups[$scope.selectedGroupIndex] = null;
APIUtils.saveLdapProperties(data)
.then(
(response) => {
toastService.success(
'Group has been removed successfully.');
},
(error) => {
toastService.error('Failed to remove group.');
})
.finally(() => {
$scope.loading = false;
$scope.$parent.loadLdap();
});
$scope.removeGroup = false;
};
$scope.removeMultipleRoleGroupsFn = () => {
$scope.removeMultipleGroups = true;
};
$scope.roleGroupIsSelectedChanged = () => {
let groupSelected = false;
$scope.roleGroups.forEach(group => {
if (group['isSelected']) {
groupSelected = true;
}
});
$scope.hasSelectedGroup = groupSelected;
};
$scope.removeMultipleRoleGroups = () => {
$scope.loading = true;
const data = {};
if ($scope.roleGroupType == 'ldap') {
data.LDAP = {};
data.LDAP.RemoteRoleMapping = $scope.roleGroups.map((group) => {
if (group['isSelected']) {
return null;
} else {
return group;
}
});
} else {
data.ActiveDirectory = {};
data.ActiveDirectory.RemoteRoleMapping =
$scope.roleGroups.map((group) => {
if (group['isSelected']) {
return null;
} else {
return group;
}
});
}
APIUtils.saveLdapProperties(data)
.then(
(response) => {
toastService.success(
'Groups has been removed successfully.');
},
(error) => {
toastService.error('Failed to remove groups.');
})
.finally(() => {
$scope.loading = false;
$scope.$parent.loadLdap();
});
$scope.removeMultipleGroups = false;
};
$scope.toggleAll = () => {
let toggleStatus = !$scope.all;
$scope.roleGroups.forEach((group) => {
group.isSelected = toggleStatus;
});
};
$scope.optionToggled = () => {
$scope.all = $scope.roleGroups.every((group) => {
return group.isSelected;
});
};
$scope.sortBy = (propertyName, isReverse) => {
$scope.reverse = isReverse;
$scope.sortPropertyName = propertyName;
};
}
]
};
}
]);
})(window.angular);