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============================================
40 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams,$modal,utilsService){
41 //$scope.role=${role};
43 $( "#dialogRoleFunction" ).hide();
44 $( "#dialogChildRole" ).hide();
45 $scope.routeRoleId = $stateParams.roleId;
47 // $scope.ociavailableRoleFunctions=${availableRoleFunctions};
48 $scope.fetchRoles = function() {
49 $scope.isLoadingRoleFunctions = true;
50 utilsService.showLoadingLayer(); // show the loading layer
51 RoleService.getRole($stateParams.roleId).then(function(data){
52 if(data==null || data ==''){
54 content:'Something is wrong. Please contact the administrator for more information'
56 confirmBoxService.reloadPageConfirm(msg);
59 $scope.data = JSON.parse(j.data);
60 $scope.role =JSON.parse($scope.data.role);
61 $scope.routeRoleId = $stateParams.roleId;
62 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
63 $scope.isGlobalRoleChecked=($scope.role.name.indexOf('global_')==-1)?false:true;
65 $scope.availableRoleFunctions=[];
67 if($scope.ociavailableRoleFunctions)
68 $.each($scope.ociavailableRoleFunctions, function(i, a){
69 var availableRoleFunction = a;
70 availableRoleFunction.selected = false;
71 $.each($scope.role.roleFunctions, function(j, b){
72 if(a.code === b.code) {
73 availableRoleFunction.selected = true;
76 $scope.availableRoleFunctions.push(availableRoleFunction);
78 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
79 $scope.availableRoles=[];
81 if($scope.ociavailableRoles)
82 $.each($scope.ociavailableRoles, function(i, a){
83 var availableRole = a;
84 availableRole.selected = false;
85 if($scope.role.childRoles){
86 $.each($scope.role.childRoles, function(j, b){
88 availableRole.selected = true;
92 $scope.availableRoles.push(availableRole);
96 console.log("RoleService.getRole failed", error);
97 }).finally(function(){
98 utilsService.hideLoadingLayer();
99 $scope.isLoadingRoleFunctions = false;
105 $scope.isGlobalRoleChecked;
107 $scope.saveRole = function() {
108 var exists = false,x;
109 for(x in $scope.availableRoles){
110 if($scope.availableRoles[x].name==$scope.role.name){
112 // $modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
116 confirmBoxService.showInformation( "Role already exists.");
117 } else if($scope.role.name.toLowerCase() == "admin"){
118 confirmBoxService.showInformation( "Role '"+$scope.role.name+"' is not acceptable.");
121 var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;
122 if($scope.isGlobalRoleChecked ){
123 $scope.role.name = ($scope.role.name.indexOf('global_')==-1)?('global_'+$scope.role.name):($scope.role.name);
126 $scope.role.name=$scope.role.name.replace('global_','');
128 for(let key in $scope.role.roleFunctions){
129 if($scope.role.roleFunctions[key].hasOwnProperty('selected')){
130 delete $scope.role.roleFunctions[key].selected;
135 childRoles: $scope.role.childRoles,
136 roleFunctions : $scope.role.roleFunctions
138 $http.post(uuu, JSON.stringify(postData,$stateParams.roleId)).then(function(res) {
139 // console.log('roleController::saveRole: ' +
140 // JSON.stringify(res));
141 if (res && res.data && res.data.role){
142 confirmBoxService.showInformation("Update Successful.");
143 $scope.routeRoleId = res.role;
144 $scope.isSaveClicked=true;
149 confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
153 console.log('post failed', res.data);
154 confirmBoxService.showInformation("Error while saving.");
160 $scope.addNewRoleFunctionModalPopup = function() {
161 var modalInstance = $modal.open({
162 templateUrl: 'app/views/role/role_functions_popup.html',
163 controller: 'rolepopupController',
164 sizeClass: 'modal-medium',
166 roleId: function () {
167 return $stateParams.roleId;
172 availableRoles: function () {
173 return $scope.ociavailableRoles;
175 availableRoleFunctions: function () {
176 return $scope.ociavailableRoleFunctions;
181 modalInstance.result.finally(function () {
185 $scope.addNewChildRoleModalPopup = function() {
186 var modalInstance = $modal.open({
187 templateUrl: 'app/views/role/role_childrole_popup.html',
188 controller: 'rolepopupController',
189 sizeClass: 'modal-medium',
191 roleId: function () {
192 return $stateParams.roleId;
197 availableRoles: function () {
198 return $scope.ociavailableRoles;
200 availableRoleFunctions: function () {
201 return $scope.ociavailableRoleFunctions;
206 modalInstance.result.finally(function () {
207 if($stateParams.roleId === '0'){
215 $scope.removeRoleFunction = function(roleFunction) {
216 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(
219 var index = $scope.role.roleFunctions.indexOf(roleFunction);
221 $scope.role.roleFunctions.splice(index, 1);
229 $scope.removeChildRole = function(childRole) {
230 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(
232 var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
233 var postData={childRole:childRole};
235 $http.post(uuu,postData).then( function(response) {
236 $scope.role=response.data.role;
237 $.each($scope.availableRoles, function(k, c){
238 if(c.id === childRole.id) {
245 confirmBoxService.showInformation("Error while saving.");