f55d1e0e0b1d6ea13a40da361c042a6af54e9695
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-controller.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39
40 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams,$modal,utilsService){
41         //$scope.role=${role};
42                 
43         $( "#dialogRoleFunction" ).hide();
44         $( "#dialogChildRole" ).hide();
45         $scope.routeRoleId = $stateParams.roleId;
46
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 ==''){
53                                 var msg={
54                                                 content:'Something is wrong. Please contact the administrator for more information'
55                                 };
56                                 confirmBoxService.reloadPageConfirm(msg);
57                         }else{
58                                 var j = data;
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;
64                                 
65                                 $scope.availableRoleFunctions=[];
66
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;
74                                                         }
75                                                 });
76                                                 $scope.availableRoleFunctions.push(availableRoleFunction);          
77                                         });     
78                                 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
79                                 $scope.availableRoles=[];
80
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){ 
87                                                                 if(a.id === b.id) {
88                                                                         availableRole.selected = true;
89                                                                 }
90                                                         });
91                                                 };
92                                                 $scope.availableRoles.push(availableRole);          
93                                         });
94                         }
95                 },function(error){
96                         console.log("RoleService.getRole failed", error);
97                 }).finally(function(){
98                         utilsService.hideLoadingLayer();
99                         $scope.isLoadingRoleFunctions = false;
100                 });
101         }
102         
103         $scope.fetchRoles();
104
105         $scope.isGlobalRoleChecked;
106
107         $scope.saveRole = function() {
108                                 var exists = false,x;   
109                                 for(x in $scope.availableRoles){
110                                         if($scope.availableRoles[x].name==$scope.role.name){
111                                                 exists = true;
112                                                 // $modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
113                                         }
114                                 }
115                                 if (exists) {
116                                         confirmBoxService.showInformation( "Role already exists.");
117                                 }
118                                 else {
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);
122                                                 
123                                         }else{
124                                                 $scope.role.name=$scope.role.name.replace('global_','');
125                                         }
126                                         for(let key in $scope.role.roleFunctions){
127                                                 if($scope.role.roleFunctions[key].hasOwnProperty('selected')){
128                                                         delete $scope.role.roleFunctions[key].selected;
129                                                 }
130                                         }
131                                         var postData = {
132                                                         role: $scope.role, 
133                                                         childRoles: $scope.role.childRoles,
134                                                         roleFunctions : $scope.role.roleFunctions
135                                         };
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;
143                                                         $scope.role='';
144                                                 }
145                                                 
146                                                 else{
147                                                         confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
148                                                 }
149                                         },
150                                         function(res){
151                                                 console.log('post failed', res.data);
152                                                 confirmBoxService.showInformation("Error while saving.");
153                                         }
154                                         );
155                                 }
156                         };
157                 
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',
163             resolve: {
164                 roleId: function () {
165                                   return $stateParams.roleId;
166                                 },
167                         role: function () {
168                           return $scope.role;
169                         },
170                         availableRoles: function () {
171                                   return $scope.ociavailableRoles;
172                             },
173                             availableRoleFunctions: function () {
174                                   return $scope.ociavailableRoleFunctions;
175                             }
176             }
177         });
178         
179         modalInstance.result.finally(function () {
180             });
181         };
182                 
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',
188                     resolve: {
189                         roleId: function () {
190                                           return $stateParams.roleId;
191                                         },
192                                 role: function () {
193                                   return $scope.role;
194                                 },
195                                 availableRoles: function () {
196                                           return $scope.ociavailableRoles;
197                                     },
198                                     availableRoleFunctions: function () {
199                                           return $scope.ociavailableRoleFunctions;
200                                     }
201                     }
202                 });
203                 
204                 modalInstance.result.finally(function () {
205                         if($stateParams.roleId === '0'){
206                                         return $scope.role;
207                                 }else{
208                                         $scope.fetchRoles();
209                                 }
210                     });
211                 };
212                 
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(
215                                 function(confirmed){
216                                                         if(confirmed) { 
217                                                                 var index = $scope.role.roleFunctions.indexOf(roleFunction);
218                                                                 if(index>=0)
219                                                                         $scope.role.roleFunctions.splice(index, 1);
220                                                                 return;
221                                                                 }
222                                 
223                 });
224                 
225                 };
226                 
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(
229                                 function(confirmed){
230                                         var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
231                                           var postData={childRole:childRole};
232                                           if(confirmed) {
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) {
237                                                                                 c.selected = false;
238                                                                         }
239                                                                     });
240                                                                   },
241                                                                   
242                                                                   function(data) {
243                                                                           confirmBoxService.showInformation("Error while saving.");
244                                                                   });
245                                                 }                               
246                 });
247                         
248                 };
249                 
250 });