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