Security/ Package Name changes
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-create-edit-popup-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('roleCreateEditController',function($scope, conf, $http, $modalInstance, confirmBoxService, message, RoleService) {
39         $scope.isGlobalRole = false; 
40         $scope.availableRoles = message.availableRoles;
41         $scope.roleFunctions = message.availableRoleFunctions;
42         $scope.appId = message.appId;
43         if($scope.appId == 1){
44                 $scope.isGlobalRole = true;
45         }
46         $scope.role = message.role;
47         $scope.isGlobalRoleChecked={
48                         isChecked : false
49         }
50         if(message.role.name)
51                 $scope.isGlobalRoleChecked.isChecked=(message.role.name.indexOf('global_')==-1)?false:true;             
52         $scope.availableRoleFunctions=[];
53         $scope.finalSelectedRoleFunctions=[];
54
55         if($scope.roleFunctions)
56                 for(var i=0; i< $scope.roleFunctions.length; i++){
57                         var availableRoleFunction = $scope.roleFunctions[i];
58                         availableRoleFunction.selected = false;
59                         for(var j=0; j< $scope.role.roleFunctions.length; j++){
60                                 if($scope.roleFunctions[i].code === $scope.role.roleFunctions[j].code) {
61                                         availableRoleFunction.selected = true;
62                                         console.log(availableRoleFunction.selected);
63                                 }
64                         };
65                         $scope.availableRoleFunctions.push(availableRoleFunction);          
66                 };
67         
68                 $scope.toggleRoleFunction = function(selected,selectedRoleFunction){
69                         if($scope.roleFunctions){
70                                 for(var i=0; i< $scope.roleFunctions.length; i++){
71                                         var availableRoleFunction = $scope.roleFunctions[i];                                    
72                                                 if(availableRoleFunction.selected){
73                                                         $scope.finalSelectedRoleFunctions.push(availableRoleFunction);
74                                                 }
75                                 }
76                         }
77                         if(!selected) {
78                                 for(var i=0; i<$scope.finalSelectedRoleFunctions.length; i++){
79                                         var availableRoleFunction = $scope.finalSelectedRoleFunctions[i];
80                                         if(availableRoleFunction.code == selectedRoleFunction.code){
81                                                 $scope.finalSelectedRoleFunctions.splice(i, 1);
82                                         }
83                                 }                                       
84                         }
85                 }
86                 
87                 
88                 $scope.saveRole = function() {                  
89                         var exists = false,x;   
90                         for(x in $scope.availableRoles){
91                                 if($scope.availableRoles[x].name==$scope.role.name){
92                                         exists = true;
93                                 }
94                         }
95                         if (exists) {
96                                 confirmBoxService.showInformation( "Role already exists.");
97                         }                       
98                         else {  
99                                 console.log($scope.role);
100                                 var uuu = conf.api.saveRole.replace(':appId', $scope.appId);
101                                 if($scope.isGlobalRoleChecked.isChecked ){
102                                         $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
103                                         saveOrUpdateRole(uuu);
104                                 }else{
105                                         var roleName = $scope.role.name.toLowerCase();
106                                         if(roleName.includes('global_')){
107                                                 confirmBoxService.showInformation('Global prefix:"global_" can only be used when the global flag is checked for the role name:' +$scope.role.name+ '. Please try again!' ).then(confirmed => {
108                                                 });
109                                         } else{
110                                                 saveOrUpdateRole(uuu);
111                                         }
112                                 }
113                         }
114                 };
115                 
116         let saveOrUpdateRole = (uuu) => {
117                         //overriding the final list of rolefunctions to role
118                         if($scope.finalSelectedRoleFunctions.length > 0)
119                                 $scope.role.roleFunctions = $scope.finalSelectedRoleFunctions;
120                         var postData = {
121                                         role: $scope.role,
122                                         childRoles: $scope.role.childRoles,
123                                         roleFunctions :  $scope.role.roleFunctions
124                         };
125                         
126                         $http.post(uuu, postData).then(function(res) {
127                                 if (res && res.data && res.data.role){
128                                         confirmBoxService.showInformation("Update Successful.").then(confirmed => {
129                                                 $modalInstance.close("confirmed");
130                                         });
131                                 }
132                                 
133                                 else{
134                                         confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
135                                 }
136                         },
137                         function(res){
138                                 console.log('post failed', res.data);
139                                 if (res.data.includes('BAD_REQUEST')){
140                                         confirmBoxService.showInformation("Error while saving." + "BAD_REQUEST");
141                                         }
142                                 else{
143                                         confirmBoxService.showInformation("Error while saving." + res.data.error);
144                                         }                                       
145                         }
146                         );
147                 }
148                 
149 })