Application Onboarding page changes
[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                                 } else if($scope.role.name.toLowerCase() == "admin"){
118                                         confirmBoxService.showInformation( "Role '"+$scope.role.name+"' is not acceptable.");
119                                 }
120                                 else {
121                                         var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;
122                                         if($scope.isGlobalRoleChecked ){
123                                                 $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
124                                                 
125                                         }else{
126                                                 $scope.role.name=$scope.role.name.replace('global_','');
127                                         }
128                                         for(let key in $scope.role.roleFunctions){
129                                                 if($scope.role.roleFunctions[key].hasOwnProperty('selected')){
130                                                         delete $scope.role.roleFunctions[key].selected;
131                                                 }
132                                         }
133                                         var postData = {
134                                                         role: $scope.role, 
135                                                         childRoles: $scope.role.childRoles,
136                                                         roleFunctions : $scope.role.roleFunctions
137                                         };
138                                         $http.post(uuu, JSON.stringify(postData,$stateParams.roleId)).then(function(res) {
139                                                 // console.log('roleController::saveRole: ' +
140                                                 // JSON.stringify(res));
141                                                 if (res && res.data && res.data.role){
142                                                         confirmBoxService.showInformation("Update Successful.");
143                                                         $scope.routeRoleId = res.role;
144                                                         $scope.isSaveClicked=true;
145                                                         $scope.role='';
146                                                 }
147                                                 
148                                                 else{
149                                                         confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
150                                                 }
151                                         },
152                                         function(res){
153                                                 console.log('post failed', res.data);
154                                                 confirmBoxService.showInformation("Error while saving.");
155                                         }
156                                         );
157                                 }
158                         };
159                 
160         $scope.addNewRoleFunctionModalPopup = function() {
161                 var modalInstance = $modal.open({
162             templateUrl: 'app/views/role/role_functions_popup.html',
163             controller: 'rolepopupController',
164             sizeClass: 'modal-medium',
165             resolve: {
166                 roleId: function () {
167                                   return $stateParams.roleId;
168                                 },
169                         role: function () {
170                           return $scope.role;
171                         },
172                         availableRoles: function () {
173                                   return $scope.ociavailableRoles;
174                             },
175                             availableRoleFunctions: function () {
176                                   return $scope.ociavailableRoleFunctions;
177                             }
178             }
179         });
180         
181         modalInstance.result.finally(function () {
182             });
183         };
184                 
185          $scope.addNewChildRoleModalPopup = function() {
186                    var modalInstance = $modal.open({
187                     templateUrl: 'app/views/role/role_childrole_popup.html',
188                     controller: 'rolepopupController',
189                     sizeClass: 'modal-medium',
190                     resolve: {
191                         roleId: function () {
192                                           return $stateParams.roleId;
193                                         },
194                                 role: function () {
195                                   return $scope.role;
196                                 },
197                                 availableRoles: function () {
198                                           return $scope.ociavailableRoles;
199                                     },
200                                     availableRoleFunctions: function () {
201                                           return $scope.ociavailableRoleFunctions;
202                                     }
203                     }
204                 });
205                 
206                 modalInstance.result.finally(function () {
207                         if($stateParams.roleId === '0'){
208                                         return $scope.role;
209                                 }else{
210                                         $scope.fetchRoles();
211                                 }
212                     });
213                 };
214                 
215                 $scope.removeRoleFunction = function(roleFunction) {
216                         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                                 function(confirmed){
218                                                         if(confirmed) { 
219                                                                 var index = $scope.role.roleFunctions.indexOf(roleFunction);
220                                                                 if(index>=0)
221                                                                         $scope.role.roleFunctions.splice(index, 1);
222                                                                 return;
223                                                                 }
224                                 
225                 });
226                 
227                 };
228                 
229                 $scope.removeChildRole = function(childRole) {
230                         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(
231                                 function(confirmed){
232                                         var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
233                                           var postData={childRole:childRole};
234                                           if(confirmed) {
235                                                           $http.post(uuu,postData).then( function(response) {
236                                                                   $scope.role=response.data.role;
237                                                                   $.each($scope.availableRoles, function(k, c){ 
238                                                                         if(c.id === childRole.id) {
239                                                                                 c.selected = false;
240                                                                         }
241                                                                     });
242                                                                   },
243                                                                   
244                                                                   function(data) {
245                                                                           confirmBoxService.showInformation("Error while saving.");
246                                                                   });
247                                                 }                               
248                 });
249                         
250                 };
251                 
252 });