Merge "Portal Spring Boot Development"
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-list-controller.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 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 app.controller('roleListController', function ($scope,RoleService, applicationsService, confirmBoxService,conf,$state,$http,$log, $modal){
39         $scope.showSpinner = true;      
40         $scope.syncRolesApplied = false; 
41          $scope.app = {
42                         appName: ''
43                     }
44         $scope.goToUrl = function(roleIdVal) {
45                         $state.go("root.role", {"roleId":roleIdVal});
46                 }
47          
48         $scope.toggleRole = function(appId, selected, availableRole) {          
49                 var toggleType = null;
50                 if(selected) {
51                         toggleType = "activate";
52                 } else {
53                         toggleType = "inactivate";
54                 }
55                 if((availableRole.id == "1") || (availableRole.id =="999"))
56                 {
57                  confirmBoxService.showInformation(availableRole.name+" role cannot be disabled");
58                  availableRole.active=!availableRole.active
59          return;
60                 }
61                 else if((appId != '1') && (availableRole.name.indexOf('global_')!=-1))
62                         {
63                         confirmBoxService.showInformation("Global role cannot be disabled");
64                         availableRole.active=!availableRole.active
65                         return;
66                         }
67                 confirmBoxService.confirm("You are about to "+toggleType+" the role "+availableRole.name+". Do you want to continue?").then(
68                 function(confirmed){
69                         if(confirmed) {
70                         var uuu = conf.api.toggleRole + '/' + appId + '/' + availableRole.id;
71                                                 var postData={
72                                                                 appId: appId,
73                                                                 role:availableRole
74                                                                 };
75                                                 $http.post(uuu, postData).then(function(response) {
76                                                         var data = response.data;
77                                                         if (typeof data === 'object' & data.restcallStatus=='Success') {
78                                                                 $scope.availableRoles = data.availableRoles; 
79                                                                 $log.debug('role::availableRoles:'+$scope.availableRoles);
80                                                         } else {
81                                                                 confirmBoxService.showInformation("Error while saving." + data.restCallStatus);
82                                                                 availableRole.active=!availableRole.active;
83                                                         }
84
85                                                 }, function(response) {
86                                                         debug.log('response:'+response);
87                                                         availableRole.active=!availableRole.active;
88                                                         confirmBoxService.showInformation("Error while saving." + data.restCallStatus);
89                                                 });
90                                                 
91                                  }
92
93         }) .catch(function(err) {
94                 $log.error('roleListController::confirmBoxService.confirm error:', err);
95                 availableRole.active=!availableRole.active;                                     
96         });
97         };
98
99                 $scope.removeRole = function(appId, availableRole) {
100                 if ((appId != '1') && (availableRole.name.indexOf('global_')!=-1)){
101                                 confirmBoxService.showInformation("Global role cannot be deleted");
102                         }
103                         else{
104                                 confirmBoxService.confirm("You are about to delete the role "+availableRole.name+". Do you want to continue?").then(
105                                 function(confirmed){
106                                                         var uuu = conf.api.removeRole + '/' + appId + '/' + availableRole.id;
107                                                           var postData={ 
108                                                                           appId: appId,
109                                                                           availableRoleId: availableRole.id
110                                                                           };
111                                                           $http.post(uuu, postData).then(function(response) {
112                                                                         var data = response.data;
113                                                                         if (typeof data === 'object' & data.restCallStatus == 'Success') {
114                                                                                 $scope.availableRoles = data.availableRoles; 
115                                                                         } else {
116                                                                                 confirmBoxService.showInformation('Failed to remove role '+ data.error )
117                                                                         }
118
119                                                                 }, function(response) {
120                                                                         confirmBoxService.showInformation("Error while deleting: "+ data.error);
121                                                                 });
122                                 
123                         }); 
124                 }  
125                 };
126                 
127                  $scope.openBulkUploadRolesAndFunctionsModal = function(appId) {
128                     var modalInstance = $modal.open({
129                         templateUrl: 'app/views/role/bulk-upload-dialogs/bulk-upload-role-functions-modal.html',
130                         controller: 'BulkRoleAndFunctionsModalCtrl as bulkRoleAndFunctions',
131                         sizeClass: 'modal-medium',
132                         resolve: {
133                                 message: function () {
134                                                 var message = {
135                                                                 appid: appId
136                                      };
137                                           return message;
138                                         }
139                                 }
140                     });         
141                         modalInstance.result.then(function (confirmed) {
142                                         if(confirmed == 'confirmed'){
143                                            // update role list table
144                                         }
145                                 });
146                 };
147                                 
148                 // getCentalizedApps
149                 $scope.getCentralizedApps = function(userId) {
150                         RoleService.getCentralizedApps(userId).then(res=> {
151                if (res.length>0) {
152                    $scope.centralizedApps = res;
153                    for(var i = 0; i<res.length; i++){
154                               if(res[i].appId == 1){
155                                   $scope.apps.selectedCentralizedApp = res[i].appId;
156                                   return;
157                               }  
158                               $scope.apps.selectedCentralizedApp = res[0].appId;
159                    }
160                 }
161             }).catch(err=> {
162                 $log.error('RoleListCtrl::centralizedApps retrieval error: ', err);
163             }).finally(() => {
164                // this.isLoadingTable = false;
165             });
166         
167                 }
168                 
169                    $scope.syncRolesFromExternalAuthSystem = function(appId){
170                 applicationsService.syncRolesEcompFromExtAuthSystem(appId).then(function(res){
171                         if(res.data != null || res.data.status != null || res.data.status == 'OK'){
172                      confirmBoxService.showInformation('Sync operation completed successfully!').then(isConfirmed => {
173                         $scope.getRolesForSelectedCentralizedApp(appId);
174                      });                                
175                         } else{
176                                  confirmBoxService.showInformation('Sync operation failed for '+app + res.data.message).then(isConfirmed => {});                                
177                         }
178                 }).catch(err=> {
179                                 confirmBoxService.showInformation("Sync operation failed for: " + err);
180                         }); 
181            };
182            
183                                 
184                 $scope.getRolesForSelectedCentralizedApp = function(val) {
185                         $scope.showSpinner = true;
186                           applicationsService.getSingleAppInfoById(val).then(function(res) {
187                           if(res.centralAuth == true){
188                                   $scope.syncRolesApplied = true;
189                           }
190                     });
191                         RoleService.getRoles(val).then(function(data){
192                                 var j = data;
193                                 $scope.data = JSON.parse(j.data);
194                                 $scope.availableRoles =$scope.data.availableRoles;
195                                 $scope.showSpinner = false;
196                         
197                         },function(error){
198                                 $scope.showSpinner = false;
199                                 confirmBoxService.showInformation('Failed to get roles. Please try again!').then(isConfirmed => {});
200                                 $scope.availableRoles = [];
201                                 $log.debug('failed');
202                         });
203                 }
204                 
205                 $scope.fnManageRoleFunctions = function(){
206                         RoleService.setManageRoleDetails($scope.centralizedApps, $scope.apps.selectedCentralizedApp);
207                 };
208                 
209                 function init(){        
210                         $scope.apps = {
211                                         selectedCentralizedApp:''
212                         };
213                         $scope.getCentralizedApps(sessionStorage.userId);
214                 }
215                 
216                 init();
217                 
218                 // edit Role
219                 $scope.editRoleModalPopup = function(appId, availableRole) {
220                         $scope.showSpinner = true;
221                         if(!availableRole.active)
222                                  return confirmBoxService.showInformation('Edit is diabled! Please toggle the role to activate it.').then(isConfirmed => {});
223                         $scope.editRole = availableRole;
224                         if(appId != undefined && availableRole.id != undefined){
225                                 RoleService.getRole(appId, availableRole.id).then(function(data){
226                                         $scope.showSpinner = false;
227                                         var response = JSON.parse(data.data);                                   
228                                         var role = JSON.parse(response.role);
229                                         var availableRoles = JSON.parse(response.availableRoles);
230                                         var availableRoleFunctions = JSON.parse(response.availableRoleFunctions);                                       
231                                         $scope.availableRoleFunctions = response.roleFunctions;
232                                         var modalInstance = $modal.open({
233                                      templateUrl: 'app/views/role/popup_createedit_role.html',
234                                      controller: 'roleCreateEditController',
235                                      sizeClass: 'modal-large', 
236                                      windowClass: "modal-docked",
237                                      resolve: {
238                                                 message: function () {
239                                                         var message = {
240                                                                         availableRoles: availableRoles,
241                                                                         availableRoleFunctions: availableRoleFunctions,
242                                                                         appId: $scope.apps.selectedCentralizedApp,
243                                                                         role: role
244                                                         };
245                                                   return message;
246                                                 }
247                                               }
248                                           });
249                                         modalInstance.result.then(function (confirmed) {
250                                                 if(confirmed == 'confirmed'){
251                                                         $scope.getRolesForSelectedCentralizedApp(appId);
252                                                 }
253                                         });
254                                 },function(error){
255                                         $log.debug('Failed to editRole');
256                                         $scope.showSpinner = false;
257                                 });
258                         }
259                          
260                 };
261                 
262                 // add Role
263                 $scope.addRoleModalPopup = function(appId) {
264                         $scope.showSpinner = true;
265                         if(appId){
266                                 var roleId = -1;
267                                 RoleService.getRole(appId, roleId).then(function(data){ 
268                                         $scope.showSpinner = false;
269                                         var response = JSON.parse(data.data);                                   
270                                         var role = JSON.parse(response.role);
271                                         var availableRoles = JSON.parse(response.availableRoles);
272                                         var availableRoleFunctions = JSON.parse(response.availableRoleFunctions);                                       
273                                         $scope.availableRoleFunctions = response.roleFunctions;
274                                         var modalInstance = $modal.open({
275                                      templateUrl: 'app/views/role/popup_createedit_role.html',
276                                      controller: 'roleCreateEditController',
277                                      sizeClass: 'modal-large', 
278                                      windowClass: "modal-docked",
279                                      resolve: {
280                                                 message: function () {
281                                                         var message = {
282                                                                         role: role,
283                                                                         appId: $scope.apps.selectedCentralizedApp
284                                              };                                                 
285                                                   return message;
286                                                 }
287                                               }
288                                           });   
289                                         modalInstance.result.then(function (confirmed) {
290                                                 if(confirmed == 'confirmed'){
291                                                         $scope.getRolesForSelectedCentralizedApp(appId);
292                                                 }
293                                         });
294                                 },function(error){
295                                         $log.debug('Failed to AddRole');
296                                 });
297                         }                        
298                 };
299
300 });