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============================================
40 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams,$modal,utilsService){
41 //$scope.role=${role};
43 $( "#dialogRoleFunction" ).hide();
44 $( "#dialogChildRole" ).hide();
45 $scope.routeRoleId = $stateParams.roleId;
47 // $scope.ociavailableRoleFunctions=${availableRoleFunctions};
48 $scope.fetchRoles = function() {
49 $scope.isLoadingRoleFunctions = true;
50 utilsService.showLoadingLayer(); // show the loading layer
51 RoleService.getRole($stateParams.roleId).then(function(data){
52 if(data==null || data ==''){
54 content:'Something is wrong. Please contact the administrator for more information'
56 confirmBoxService.reloadPageConfirm(msg);
59 $scope.data = JSON.parse(j.data);
60 $scope.role =JSON.parse($scope.data.role);
61 $scope.routeRoleId = $stateParams.roleId;
62 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
63 $scope.isGlobalRoleChecked=($scope.role.name.indexOf('global_')==-1)?false:true;
65 $scope.availableRoleFunctions=[];
67 if($scope.ociavailableRoleFunctions)
68 $.each($scope.ociavailableRoleFunctions, function(i, a){
69 var availableRoleFunction = a;
70 availableRoleFunction.selected = false;
71 $.each($scope.role.roleFunctions, function(j, b){
72 if(a.code === b.code) {
73 availableRoleFunction.selected = true;
76 $scope.availableRoleFunctions.push(availableRoleFunction);
78 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
79 $scope.availableRoles=[];
81 if($scope.ociavailableRoles)
82 $.each($scope.ociavailableRoles, function(i, a){
83 var availableRole = a;
84 availableRole.selected = false;
85 if($scope.role.childRoles){
86 $.each($scope.role.childRoles, function(j, b){
88 availableRole.selected = true;
92 $scope.availableRoles.push(availableRole);
96 console.log("RoleService.getRole failed", error);
97 }).finally(function(){
98 utilsService.hideLoadingLayer();
99 $scope.isLoadingRoleFunctions = false;
105 $scope.isGlobalRoleChecked;
107 $scope.saveRole = function() {
108 var exists = false,x;
109 for(x in $scope.availableRoles){
110 if($scope.availableRoles[x].name==$scope.role.name){
112 // $modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
116 confirmBoxService.showInformation( "Role already exists.");
119 var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;
120 if($scope.isGlobalRoleChecked ){
121 $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
124 $scope.role.name=$scope.role.name.replace('global_','');
126 for(let key in $scope.role.roleFunctions){
127 if($scope.role.roleFunctions[key].hasOwnProperty('selected')){
128 delete $scope.role.roleFunctions[key].selected;
133 childRoles: $scope.role.childRoles,
134 roleFunctions : $scope.role.roleFunctions
136 $http.post(uuu, JSON.stringify(postData,$stateParams.roleId)).then(function(res) {
137 // console.log('roleController::saveRole: ' +
138 // JSON.stringify(res));
139 if (res && res.data && res.data.role){
140 confirmBoxService.showInformation("Update Successful.");
141 $scope.routeRoleId = res.role;
142 $scope.isSaveClicked=true;
147 confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
151 console.log('post failed', res.data);
152 confirmBoxService.showInformation("Error while saving.");
158 $scope.addNewRoleFunctionModalPopup = function() {
159 var modalInstance = $modal.open({
160 templateUrl: 'app/views/role/role_functions_popup.html',
161 controller: 'rolepopupController',
162 sizeClass: 'modal-medium',
164 roleId: function () {
165 return $stateParams.roleId;
170 availableRoles: function () {
171 return $scope.ociavailableRoles;
173 availableRoleFunctions: function () {
174 return $scope.ociavailableRoleFunctions;
179 modalInstance.result.finally(function () {
183 $scope.addNewChildRoleModalPopup = function() {
184 var modalInstance = $modal.open({
185 templateUrl: 'app/views/role/role_childrole_popup.html',
186 controller: 'rolepopupController',
187 sizeClass: 'modal-medium',
189 roleId: function () {
190 return $stateParams.roleId;
195 availableRoles: function () {
196 return $scope.ociavailableRoles;
198 availableRoleFunctions: function () {
199 return $scope.ociavailableRoleFunctions;
204 modalInstance.result.finally(function () {
205 if($stateParams.roleId === '0'){
213 $scope.removeRoleFunction = function(roleFunction) {
214 confirmBoxService.confirm("You are about to remove the role function "+roleFunction.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
217 var index = $scope.role.roleFunctions.indexOf(roleFunction);
219 $scope.role.roleFunctions.splice(index, 1);
227 $scope.removeChildRole = function(childRole) {
228 confirmBoxService.confirm("You are about to remove the child role "+childRole.name+" from the role for "+$scope.role.name+". Do you want to continue?").then(
230 var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
231 var postData={childRole:childRole};
233 $http.post(uuu,postData).then( function(response) {
234 $scope.role=response.data.role;
235 $.each($scope.availableRoles, function(k, c){
236 if(c.id === childRole.id) {
243 confirmBoxService.showInformation("Error while saving.");