summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-FE-common/client/app/views/role/role-create-edit-popup-controller.js
blob: 2c5312628d94e8fd61937ae71cb2b0b33a3c44bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * 
 */
app.controller('roleCreateEditController',function($scope, conf, $http, $modalInstance, confirmBoxService, message, RoleService) {
	$scope.isGlobalRole = false; 
	$scope.availableRoles = message.availableRoles;
	$scope.roleFunctions = message.availableRoleFunctions;
	$scope.appId = message.appId;
	if($scope.appId == 1){
		$scope.isGlobalRole = true;
	}
	$scope.role = message.role;
	$scope.isGlobalRoleChecked={
			isChecked : false
	}
	if(message.role.name)
		$scope.isGlobalRoleChecked.isChecked=(message.role.name.indexOf('global_')==-1)?false:true;		
	$scope.availableRoleFunctions=[];
	$scope.finalSelectedRoleFunctions=[];

	if($scope.roleFunctions)
		for(var i=0; i< $scope.roleFunctions.length; i++){
			var availableRoleFunction = $scope.roleFunctions[i];
			availableRoleFunction.selected = false;
			for(var j=0; j< $scope.role.roleFunctions.length; j++){
				if($scope.roleFunctions[i].code === $scope.role.roleFunctions[j].code
						&& $scope.roleFunctions[i].type === $scope.role.roleFunctions[j].type
						&& $scope.roleFunctions[i].action === $scope.role.roleFunctions[j].action) {
					availableRoleFunction.selected = true;
					console.log(availableRoleFunction.selected);
				}
			};
			$scope.availableRoleFunctions.push(availableRoleFunction);	    
		};
	
		$scope.toggleRoleFunction = function(selected,selectedRoleFunction){
			if($scope.roleFunctions){
				for(var i=0; i< $scope.roleFunctions.length; i++){
					var availableRoleFunction = $scope.roleFunctions[i];					
						if(availableRoleFunction.selected){
							$scope.finalSelectedRoleFunctions.push(availableRoleFunction);
						}
				}
			}
			if(!selected) {
				for(var i=0; i<$scope.finalSelectedRoleFunctions.length; i++){
					var availableRoleFunction = $scope.finalSelectedRoleFunctions[i];
					if(availableRoleFunction.code == selectedRoleFunction.code
							&& availableRoleFunction.type == selectedRoleFunction.type
							&& availableRoleFunction.action == selectedRoleFunction.action){
						$scope.finalSelectedRoleFunctions.splice(i, 1);
					}
				}					
			}
		}
		
		
		$scope.saveRole = function() {			
			var exists = false,x;	
			for(x in $scope.availableRoles){
				if($scope.availableRoles[x].name==$scope.role.name){
					exists = true;
				}
			}
			if (exists) {
				confirmBoxService.showInformation( "Role already exists.");
			}			
			else {	
				console.log($scope.role);
				var uuu = conf.api.saveRole.replace(':appId', $scope.appId);
				if($scope.isGlobalRoleChecked.isChecked ){
					$scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
					saveOrUpdateRole(uuu);
				}else{
					var roleName = $scope.role.name.toLowerCase();
					if(roleName.includes('global_')){
						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 => {
						});
					} else{
						saveOrUpdateRole(uuu);
					}
				}
			}
		};
		
        let saveOrUpdateRole = (uuu) => {
			//overriding the final list of rolefunctions to role
			if($scope.finalSelectedRoleFunctions.length > 0)
				$scope.role.roleFunctions = $scope.finalSelectedRoleFunctions;
			var postData = {
					role: $scope.role,
					childRoles: $scope.role.childRoles,
					roleFunctions :  $scope.role.roleFunctions
			};
			
			$http.post(uuu, postData).then(function(res) {
				if (res && res.data && res.data.role){
					confirmBoxService.showInformation("Update Successful.").then(confirmed => {
						$modalInstance.close("confirmed");
					});
				}
				
				else{
					confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
				}
			},
			function(res){
				console.log('post failed', res.data);
				if (res.data.includes('BAD_REQUEST')){
					confirmBoxService.showInformation("Error while saving." + "BAD_REQUEST");
					}
				else{
					confirmBoxService.showInformation("Error while saving." + res.data.error);
					}					
			}
			);
		}
		
})