2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
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;
46 $scope.role = message.role;
47 $scope.isGlobalRoleChecked={
51 $scope.isGlobalRoleChecked.isChecked=(message.role.name.indexOf('global_')==-1)?false:true;
52 $scope.availableRoleFunctions=[];
53 $scope.finalSelectedRoleFunctions=[];
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 && $scope.roleFunctions[i].type === $scope.role.roleFunctions[j].type
62 && $scope.roleFunctions[i].action === $scope.role.roleFunctions[j].action) {
63 availableRoleFunction.selected = true;
64 console.log(availableRoleFunction.selected);
67 $scope.availableRoleFunctions.push(availableRoleFunction);
70 $scope.toggleRoleFunction = function(selected,selectedRoleFunction){
71 if($scope.roleFunctions){
72 for(var i=0; i< $scope.roleFunctions.length; i++){
73 var availableRoleFunction = $scope.roleFunctions[i];
74 if(availableRoleFunction.selected){
75 $scope.finalSelectedRoleFunctions.push(availableRoleFunction);
80 for(var i=0; i<$scope.finalSelectedRoleFunctions.length; i++){
81 var availableRoleFunction = $scope.finalSelectedRoleFunctions[i];
82 if(availableRoleFunction.code == selectedRoleFunction.code
83 && availableRoleFunction.type == selectedRoleFunction.type
84 && availableRoleFunction.action == selectedRoleFunction.action){
85 $scope.finalSelectedRoleFunctions.splice(i, 1);
92 $scope.saveRole = function() {
94 for(x in $scope.availableRoles){
95 if($scope.availableRoles[x].name==$scope.role.name){
100 confirmBoxService.showInformation( "Role already exists.");
103 console.log($scope.role);
104 var uuu = conf.api.saveRole.replace(':appId', $scope.appId);
105 if($scope.isGlobalRoleChecked.isChecked ){
106 $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
107 saveOrUpdateRole(uuu);
109 var roleName = $scope.role.name.toLowerCase();
110 if(roleName.includes('global_')){
111 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 => {
114 saveOrUpdateRole(uuu);
120 let saveOrUpdateRole = (uuu) => {
121 //overriding the final list of rolefunctions to role
122 if($scope.finalSelectedRoleFunctions.length > 0)
123 $scope.role.roleFunctions = $scope.finalSelectedRoleFunctions;
126 childRoles: $scope.role.childRoles,
127 roleFunctions : $scope.role.roleFunctions
130 $http.post(uuu, postData).then(function(res) {
131 if (res && res.data && res.data.role){
132 confirmBoxService.showInformation("Update Successful.").then(confirmed => {
133 $modalInstance.close("confirmed");
138 confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
142 console.log('post failed', res.data);
143 if (res.data.includes('BAD_REQUEST')){
144 confirmBoxService.showInformation("Error while saving." + "BAD_REQUEST");
147 confirmBoxService.showInformation("Error while saving." + res.data.error);