3821ed91b8e6480dcc44d1baebff5abe3c5a709a
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-function-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 "use strict";
39
40 app.controller('roleFunctionListController', function ($scope, RoleService, applicationsService, $http, $state, conf, confirmBoxService, ngDialog, $modal) {
41         $("#dialog").hide();
42         $scope.centralizedApps = RoleService.getManageRoleDetails().apps;
43         $scope.apps = {
44                 selectedCentralizedApp: RoleService.getManageRoleDetails().id
45         };
46
47         $scope.getRoleFunctions = function (id) {
48                 $scope.isLoadingRoleFunctions = true;
49                 RoleService.getRoleFunctionList(id).then(function (data) {
50                         var j = data;
51                         $scope.data = JSON.parse(j.data);
52                         $scope.availableRoleFunctions = $scope.data.availableRoleFunctions;
53                 }, function (error) {
54                         confirmBoxService.showInformation("Failed to get role functions. Please try again!");
55                         console.log("failed");
56                 }).finally(function(){
57                         $scope.isLoadingRoleFunctions = false;
58                 });
59         };
60
61         function init() {
62                 if($scope.apps.selectedCentralizedApp){
63                         $scope.getRoleFunctions($scope.apps.selectedCentralizedApp);
64                 }
65                 if($scope.apps.selectedCentralizedApp == undefined){
66                         $scope.getCentralizedApps(sessionStorage.userId);
67                 }
68         }
69
70         //getCentalizedApps
71         $scope.getCentralizedApps = function(userId) {
72                 RoleService.getCentralizedApps(userId).then(res=> {
73            if (res.length>0) {
74                    $scope.centralizedApps = res;
75                    for(var i = 0; i<res.length; i++){
76                               if(res[i].appId == 1){
77                               $scope.apps.selectedCentralizedApp = res[i].appId;
78                                   return;
79                               }
80                                $scope.apps.selectedCentralizedApp = res[0].appId;
81                    }
82             }
83         }).catch(err=> {
84             $log.error('RoleListCtrl::centralizedApps retrieval error: ', err);
85         }).finally(() => {
86            // this.isLoadingTable = false;
87         });
88
89         }
90         
91         init();
92
93         $scope.editRoleFunction = null;
94         var dialog = null;
95
96         $scope.editRoleFunctionModalPopup = function (availableRoleFunction) {
97                 $scope.editRoleFunction = availableRoleFunction;
98                 $scope.availableRoleFunctionsTemp = $scope.availableRoleFunctions;
99
100                 var modalInstance = $modal.open({
101                         templateUrl: 'app/views/role/popup_modal_rolefunction.html',
102                         controller: 'rolefunctionpopupController',
103                         sizeClass: 'modal-small',
104                         resolve: {
105                                 message: function message() {
106                                         var message = {
107                                                 availableRoleFunction: $scope.editRoleFunction,
108                                                 availableRoleFunctions: $scope.availableRoleFunctionsTemp,
109                                                 appId: $scope.apps.selectedCentralizedApp
110                                         };
111                                         return message;
112                                 },
113                                 isEditing: function isEditing() {
114                                         return true;
115                                 }
116                         }
117                 });
118
119                 modalInstance.result.then(function (response) {
120                         if (response) {
121                                 $scope.getRoleFunctions($scope.apps.selectedCentralizedApp);
122                         }
123                         else
124                                 $scope.availableRoleFunctions = $scope.availableRoleFunctionsTemp;
125                 });
126         };
127
128         $scope.addNewRoleFunctionModalPopup = function (availableRoleFunction) {
129
130                 $scope.editRoleFunction = null;
131                 $scope.availableRoleFunctionsTemp = $scope.availableRoleFunctions;
132                 var modalInstance = $modal.open({
133                         templateUrl: 'app/views/role/popup_modal_rolefunction.html',
134                         controller: 'rolefunctionpopupController',
135                         sizeClass: 'modal-small',
136                         resolve: {
137                                 message: function message() {
138                                         var message = {
139                                                 availableRoleFunction: $scope.editRoleFunction,
140                                                 availableRoleFunctions: $scope.availableRoleFunctionsTemp,
141                                                 appId: $scope.apps.selectedCentralizedApp
142                                         };
143                                         return message;
144                                 },
145                                 isEditing: function isEditing() {
146                                         return false;
147                                 }
148                         }
149                 });
150
151                 modalInstance.result.then(function (response) {
152                         if (response) {
153                                 $scope.getRoleFunctions($scope.apps.selectedCentralizedApp);
154                         }
155                         if (response.availableRoleFunctions != undefined) 
156                                 $scope.availableRoleFunctions = response.availableRoleFunctions;else $scope.availableRoleFunctions = $scope.availableRoleFunctionsTemp;
157                 });
158         };
159
160         $scope.addNewRoleFunctionPopup = function () {
161                 $scope.editRoleFunction = null;
162                 $("#dialog").dialog({
163                         modal: true
164                 });
165         };
166
167          $scope.syncRoleFunctionsFromExternalAuthSystem = function(appId){
168                 applicationsService.syncFunctionsFromExternalAuthSystem(appId).then(function(res){
169                         if(res.status == 200){
170                      confirmBoxService.showInformation('Sync operation completed successfully!').then(isConfirmed => {
171                          $scope.getRoleFunctions(appId);
172                      });                                
173                         } else{
174                                  confirmBoxService.showInformation('Sync operation failed for '+app).then(isConfirmed => {});                           
175                         }
176                 });
177         };
178         
179         $scope.saveRoleFunction = function (availableRoleFunction) {
180                 var uuu = conf.api.saveRoleFuncion;
181                 var postData = { availableRoleFunction: availableRoleFunction };
182                 $http.post(uuu, postData).then(function (response) {
183                         var data = response.data;
184                         $scope.availableRoleFunctions = data.availableRoleFunctions;
185                         $scope.editRoleFunction = null;
186                 }, function () {
187
188                         confirmBoxService.showInformation("Error while saving");
189                 });
190         };
191
192         $scope.removeRole = function (availableRoleFunction) {
193                 confirmBoxService.confirm("You are about to delete the role function " + availableRoleFunction.name + ". Do you want to continue?").then(function (confirmed) {
194                         if (confirmed) {
195                                 $scope.availableRoleFunctionsTemp = $scope.availableRoleFunctions;
196                                 var uuu = conf.api.removeRoleFunction.replace(':appId', $scope.apps.selectedCentralizedApp);
197                                 var postData = availableRoleFunction;
198                                 $http.post(uuu, postData).then(function (response) {
199                                         if(response.data.status == 'OK'){
200                                                 confirmBoxService.showInformation(response.data.message);
201                                                 $scope.availableRoleFunctionsTemp.splice($scope.availableRoleFunctionsTemp.indexOf(availableRoleFunction), 1);
202                                                 $scope.availableRoleFunctions = $scope.availableRoleFunctionsTemp;
203                                         } else{
204                                                 confirmBoxService.showInformation("Error while deleting: " + response.data.message);
205                                         }
206                                 }, function () {
207                                         $scope.availableRoleFunctions = $scope.availableRoleFunctionsTemp;
208                                 });
209                         }
210                 });
211         };
212 });