2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 app.controller('roleCreateEditController',function($scope, conf, $http, $modalInstance, confirmBoxService, message, RoleService) {
39 $scope.isGlobalRole = false;
40 $scope.availableRoles = message.availableRoles;
41 $scope.roleFunctions = message.availableRoleFunctions;
42 $scope.appId = message.appId;
43 if($scope.appId == 1){
44 $scope.isGlobalRole = true;
46 $scope.role = message.role;
47 $scope.isGlobalRoleChecked={
51 $scope.isGlobalRoleChecked.isChecked=(message.role.name.indexOf('global_')==-1)?false:true;
52 $scope.availableRoleFunctions=[];
53 $scope.finalSelectedRoleFunctions=[];
55 if($scope.roleFunctions)
56 for(var i=0; i< $scope.roleFunctions.length; i++){
57 var availableRoleFunction = $scope.roleFunctions[i];
58 availableRoleFunction.selected = false;
59 for(var j=0; j< $scope.role.roleFunctions.length; j++){
60 if($scope.roleFunctions[i].code === $scope.role.roleFunctions[j].code) {
61 availableRoleFunction.selected = true;
62 console.log(availableRoleFunction.selected);
65 $scope.availableRoleFunctions.push(availableRoleFunction);
68 $scope.toggleRoleFunction = function(selected,selectedRoleFunction){
69 if($scope.roleFunctions){
70 for(var i=0; i< $scope.roleFunctions.length; i++){
71 var availableRoleFunction = $scope.roleFunctions[i];
72 if(availableRoleFunction.selected){
73 $scope.finalSelectedRoleFunctions.push(availableRoleFunction);
78 for(var i=0; i<$scope.finalSelectedRoleFunctions.length; i++){
79 var availableRoleFunction = $scope.finalSelectedRoleFunctions[i];
80 if(availableRoleFunction.code == selectedRoleFunction.code){
81 $scope.finalSelectedRoleFunctions.splice(i, 1);
88 $scope.saveRole = function() {
90 for(x in $scope.availableRoles){
91 if($scope.availableRoles[x].name==$scope.role.name){
96 confirmBoxService.showInformation( "Role already exists.");
99 console.log($scope.role);
100 var uuu = conf.api.saveRole.replace(':appId', $scope.appId);
101 if($scope.isGlobalRoleChecked.isChecked ){
102 $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
103 saveOrUpdateRole(uuu);
105 var roleName = $scope.role.name.toLowerCase();
106 if(roleName.includes('global_')){
107 confirmBoxService.showInformation('Global prefix:"global_" can only be used when the global flag is checked for the role name:' +$scope.role.name+ '. Please try again!' ).then(confirmed => {
110 saveOrUpdateRole(uuu);
116 let saveOrUpdateRole = (uuu) => {
117 //overriding the final list of rolefunctions to role
118 if($scope.finalSelectedRoleFunctions.length > 0)
119 $scope.role.roleFunctions = $scope.finalSelectedRoleFunctions;
122 childRoles: $scope.role.childRoles,
123 roleFunctions : $scope.role.roleFunctions
126 $http.post(uuu, postData).then(function(res) {
127 if (res && res.data && res.data.role){
128 confirmBoxService.showInformation("Update Successful.").then(confirmed => {
129 $modalInstance.close("confirmed");
134 confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
138 console.log('post failed', res.data);
139 if (res.data.includes('BAD_REQUEST')){
140 confirmBoxService.showInformation("Error while saving." + "BAD_REQUEST");
143 confirmBoxService.showInformation("Error while saving." + res.data.error);