22b28e93ce5cd9b3df542725899bcd54fda437e1
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-controller.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20
21
22 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams,$modal,utilsService){
23         //$scope.role=${role};
24                 
25         $( "#dialogRoleFunction" ).hide();
26         $( "#dialogChildRole" ).hide();
27         $scope.routeRoleId = $stateParams.roleId;
28
29         // $scope.ociavailableRoleFunctions=${availableRoleFunctions};
30         $scope.fetchRoles = function() {
31                 $scope.isLoadingRoleFunctions = true;
32                 utilsService.showLoadingLayer(); // show the loading layer
33                 RoleService.getRole($stateParams.roleId).then(function(data){
34                         if(data==null || data ==''){
35                                 var msg={
36                                                 content:'Something is wrong. Please contact the administrator for more information'
37                                 };
38                                 confirmBoxService.reloadPageConfirm(msg);
39                         }else{
40                                 var j = data;
41                                 $scope.data = JSON.parse(j.data);
42                                 $scope.role =JSON.parse($scope.data.role);
43                                 $scope.routeRoleId = $stateParams.roleId;
44                                 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
45                                 $scope.isGlobalRoleChecked=($scope.role.name.indexOf('global_')==-1)?false:true;
46
47                                 $scope.availableRoleFunctions=[];
48
49                                 if($scope.ociavailableRoleFunctions)
50                                         $.each($scope.ociavailableRoleFunctions, function(i, a){ 
51                                                 var availableRoleFunction = a;
52                                                 availableRoleFunction.selected = false;
53                                                 $.each($scope.role.roleFunctions, function(j, b){ 
54                                                         if(a.code === b.code) {
55                                                                 availableRoleFunction.selected = true;
56                                                         }
57                                                 });
58                                                 $scope.availableRoleFunctions.push(availableRoleFunction);          
59                                         });     
60                                 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
61                                 $scope.availableRoles=[];
62
63                                 if($scope.ociavailableRoles)
64                                         $.each($scope.ociavailableRoles, function(i, a){ 
65                                                 var availableRole = a;
66                                                 availableRole.selected = false;
67                                                 if($scope.role.childRoles){
68                                                         $.each($scope.role.childRoles, function(j, b){ 
69                                                                 if(a.id === b.id) {
70                                                                         availableRole.selected = true;
71                                                                 }
72                                                         });
73                                                 };
74                                                 $scope.availableRoles.push(availableRole);          
75                                         });
76                         }
77                 },function(error){
78                         console.log("RoleService.getRole failed", error);
79                 }).finally(function(){
80                         utilsService.hideLoadingLayer();
81                         $scope.isLoadingRoleFunctions = false;
82                 });
83         }
84         
85         $scope.fetchRoles();
86
87         $scope.isGlobalRoleChecked;
88
89         $scope.saveRole = function() {
90                                 var exists = false,x;   
91                                 for(x in $scope.availableRoles){
92                                         if($scope.availableRoles[x].name==$scope.role.name){
93                                                 exists = true;
94                                                 // $modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
95                                         }
96                                 }
97                                 if (exists) {
98                                         confirmBoxService.showInformation( "Role already exists.");
99                                 }
100                                 else {
101                                         var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;
102                                         if($scope.isGlobalRoleChecked ){
103                                                 $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
104                                                 
105                                         }else{
106                                                 $scope.role.name=$scope.role.name.replace('global_','');
107                                         }
108                                         var postData = {
109                                                         role: $scope.role, 
110                                                         childRoles: $scope.role.childRoles,
111                                                         roleFunctions : $scope.role.roleFunctions
112                                         };
113                                         $http.post(uuu, JSON.stringify(postData,$stateParams.roleId)).then(function(res) {
114                                                 // console.log('roleController::saveRole: ' +
115                                                 // JSON.stringify(res));
116                                                 if (res && res.data && res.data.role){
117                                                         confirmBoxService.showInformation("Update Successful.");
118                                                         $scope.routeRoleId = res.role;
119                                                         $scope.isSaveClicked=true;
120                                                         $scope.role='';
121                                                 }
122                                                 
123                                                 else{
124                                                         confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
125                                                 }
126                                         },
127                                         function(res){
128                                                 console.log('post failed', res.data);
129                                                 confirmBoxService.showInformation("Error while saving.");
130                                         }
131                                         );
132                                 }
133                         };
134                 
135         $scope.addNewRoleFunctionModalPopup = function() {
136                 var modalInstance = $modal.open({
137             templateUrl: 'app/views/role/role_functions_popup.html',
138             controller: 'rolepopupController',
139             sizeClass: 'modal-medium',
140             resolve: {
141                 roleId: function () {
142                                   return $stateParams.roleId;
143                                 },
144                         role: function () {
145                           return $scope.role;
146                         },
147                         availableRoles: function () {
148                                   return $scope.ociavailableRoles;
149                             },
150                             availableRoleFunctions: function () {
151                                   return $scope.ociavailableRoleFunctions;
152                             }
153             }
154         });
155         
156         modalInstance.result.finally(function () {
157                 if($stateParams.roleId === '0'){
158                                 return $scope.role;
159                         }else{
160                                 $scope.fetchRoles();
161                         }
162             });
163         };
164                 
165          $scope.addNewChildRoleModalPopup = function() {
166                    var modalInstance = $modal.open({
167                     templateUrl: 'app/views/role/role_childrole_popup.html',
168                     controller: 'rolepopupController',
169                     sizeClass: 'modal-medium',
170                     resolve: {
171                         roleId: function () {
172                                           return $stateParams.roleId;
173                                         },
174                                 role: function () {
175                                   return $scope.role;
176                                 },
177                                 availableRoles: function () {
178                                           return $scope.ociavailableRoles;
179                                     },
180                                     availableRoleFunctions: function () {
181                                           return $scope.ociavailableRoleFunctions;
182                                     }
183                     }
184                 });
185                 
186                 modalInstance.result.finally(function () {
187                         if($stateParams.roleId === '0'){
188                                         return $scope.role;
189                                 }else{
190                                         $scope.fetchRoles();
191                                 }
192                     });
193                 };
194                 
195                 $scope.removeRoleFunction = function(roleFunction) {
196                         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(
197                                 function(confirmed){
198                                                 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + $stateParams.roleId;
199                                                   var postData={roleFunction:roleFunction};
200                                                         if(confirmed) { 
201                                                                 $http.post(uuu, postData).then(
202                                                                                 function(response) {
203                                                                                         $scope.role= response.data.role;
204                                                                                         $.each($scope.availableRoleFunctions, function(k, c){ 
205                                                                                         if(c.code === roleFunction.code) {
206                                                                                                 c.selected = false;
207                                                                                         }
208                                                                                     });
209                                                                                 }, 
210                                                                                 function(response) {
211                                                                                         confirmBoxService.showInformation("Error while saving.");
212                                                                                 }
213                                                                 );                                                                      
214                                                                 }
215                                 
216                 });
217                 
218                 };
219                 
220                 $scope.removeChildRole = function(childRole) {
221                         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(
222                                 function(confirmed){
223                                         var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
224                                           var postData={childRole:childRole};
225                                           if(confirmed) {
226                                                           $http.post(uuu,postData).then( function(response) {
227                                                                   $scope.role=response.data.role;
228                                                                   $.each($scope.availableRoles, function(k, c){ 
229                                                                         if(c.id === childRole.id) {
230                                                                                 c.selected = false;
231                                                                         }
232                                                                     });
233                                                                   },
234                                                                   
235                                                                   function(data) {
236                                                                           confirmBoxService.showInformation("Error while saving.");
237                                                                   });
238                                                 }                               
239                 });
240                         
241                 };
242                 
243 });