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.getRolesForSelectedCentralizedApp(res[i].appId);
156                                   $scope.apps.selectedCentralizedApp = res[i].appId;
157                                   return;
158                               }  
159                               $scope.getRolesForSelectedCentralizedApp(res[0].appId);
160                               $scope.apps.selectedCentralizedApp = res[0].appId;
161                    }
162                 }
163             }).catch(err=> {
164                 $log.error('RoleListCtrl::centralizedApps retrieval error: ', err);
165             }).finally(() => {
166                // this.isLoadingTable = false;
167             });
168         
169                 }
170                 
171                    $scope.syncRolesFromExternalAuthSystem = function(appId){
172                 applicationsService.syncRolesEcompFromExtAuthSystem(appId).then(function(res){
173                         if(res.data != null || res.data.status != null || res.data.status == 'OK'){
174                      confirmBoxService.showInformation('Sync operation completed successfully!').then(isConfirmed => {
175                         $scope.getRolesForSelectedCentralizedApp(appId);
176                      });                                
177                         } else{
178                                  confirmBoxService.showInformation('Sync operation failed for '+app + res.data.message).then(isConfirmed => {});                                
179                         }
180                 }).catch(err=> {
181                                 confirmBoxService.showInformation("Sync operation failed for: " + err);
182                         }); 
183            };
184            
185                                 
186                 $scope.getRolesForSelectedCentralizedApp = function(val) {
187                         $scope.showSpinner = true;
188                           applicationsService.getSingleAppInfoById(val).then(function(res) {
189                           if(res.centralAuth == true){
190                                   $scope.syncRolesApplied = true;
191                           }
192                     });
193                         RoleService.getRoles(val).then(function(data){
194                                 var j = data;
195                                 $scope.data = JSON.parse(j.data);
196                                 $scope.availableRoles =$scope.data.availableRoles;
197                                 $scope.showSpinner = false;
198                         
199                         },function(error){
200                                 $scope.showSpinner = false;
201                                 confirmBoxService.showInformation('Failed to get roles. Please try again!').then(isConfirmed => {});
202                                 $scope.availableRoles = [];
203                                 $log.debug('failed');
204                         });
205                 }
206                 
207                 $scope.fnManageRoleFunctions = function(){
208                         RoleService.setManageRoleDetails($scope.centralizedApps, $scope.apps.selectedCentralizedApp);
209                 };
210                 
211                 function init(){        
212                         $scope.apps = {
213                                         selectedCentralizedApp:''
214                         };
215                         $scope.getCentralizedApps(sessionStorage.userId);
216                 }
217                 
218                 init();
219                 
220                 // edit Role
221                 $scope.editRoleModalPopup = function(appId, availableRole) {
222                         $scope.showSpinner = true;
223                         if(!availableRole.active)
224                                  return confirmBoxService.showInformation('Edit is diabled! Please toggle the role to activate it.').then(isConfirmed => {});
225                         $scope.editRole = availableRole;
226                         if(appId != undefined && availableRole.id != undefined){
227                                 RoleService.getRole(appId, availableRole.id).then(function(data){
228                                         $scope.showSpinner = false;
229                                         var response = JSON.parse(data.data);                                   
230                                         var role = JSON.parse(response.role);
231                                         var availableRoles = JSON.parse(response.availableRoles);
232                                         var availableRoleFunctions = JSON.parse(response.availableRoleFunctions);                                       
233                                         $scope.availableRoleFunctions = response.roleFunctions;
234                                         var modalInstance = $modal.open({
235                                      templateUrl: 'app/views/role/popup_createedit_role.html',
236                                      controller: 'roleCreateEditController',
237                                      sizeClass: 'modal-large', 
238                                      windowClass: "modal-docked",
239                                      resolve: {
240                                                 message: function () {
241                                                         var message = {
242                                                                         availableRoles: availableRoles,
243                                                                         availableRoleFunctions: availableRoleFunctions,
244                                                                         appId: $scope.apps.selectedCentralizedApp,
245                                                                         role: role
246                                                         };
247                                                   return message;
248                                                 }
249                                               }
250                                           });
251                                         modalInstance.result.then(function (confirmed) {
252                                                 if(confirmed == 'confirmed'){
253                                                         $scope.getRolesForSelectedCentralizedApp(appId);
254                                                 }
255                                         });
256                                 },function(error){
257                                         $log.debug('Failed to editRole');
258                                         $scope.showSpinner = false;
259                                 });
260                         }
261                          
262                 };
263                 
264                 // add Role
265                 $scope.addRoleModalPopup = function(appId) {
266                         $scope.showSpinner = true;
267                         if(appId){
268                                 var roleId = -1;
269                                 RoleService.getRole(appId, roleId).then(function(data){ 
270                                         $scope.showSpinner = false;
271                                         var response = JSON.parse(data.data);                                   
272                                         var role = JSON.parse(response.role);
273                                         var availableRoles = JSON.parse(response.availableRoles);
274                                         var availableRoleFunctions = JSON.parse(response.availableRoleFunctions);                                       
275                                         $scope.availableRoleFunctions = response.roleFunctions;
276                                         var modalInstance = $modal.open({
277                                      templateUrl: 'app/views/role/popup_createedit_role.html',
278                                      controller: 'roleCreateEditController',
279                                      sizeClass: 'modal-large', 
280                                      windowClass: "modal-docked",
281                                      resolve: {
282                                                 message: function () {
283                                                         var message = {
284                                                                         role: role,
285                                                                         appId: $scope.apps.selectedCentralizedApp
286                                              };                                                 
287                                                   return message;
288                                                 }
289                                               }
290                                           });   
291                                         modalInstance.result.then(function (confirmed) {
292                                                 if(confirmed == 'confirmed'){
293                                                         $scope.getRolesForSelectedCentralizedApp(appId);
294                                                 }
295                                         });
296                                 },function(error){
297                                         $log.debug('Failed to AddRole');
298                                 });
299                         }                        
300                 };
301
302 });