2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams,$modal,utilsService){
23 //$scope.role=${role};
25 $( "#dialogRoleFunction" ).hide();
26 $( "#dialogChildRole" ).hide();
27 $scope.routeRoleId = $stateParams.roleId;
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 ==''){
36 content:'Something is wrong. Please contact the administrator for more information'
38 confirmBoxService.reloadPageConfirm(msg);
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;
47 $scope.availableRoleFunctions=[];
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;
58 $scope.availableRoleFunctions.push(availableRoleFunction);
60 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
61 $scope.availableRoles=[];
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){
70 availableRole.selected = true;
74 $scope.availableRoles.push(availableRole);
78 console.log("RoleService.getRole failed", error);
79 }).finally(function(){
80 utilsService.hideLoadingLayer();
81 $scope.isLoadingRoleFunctions = false;
87 $scope.isGlobalRoleChecked;
89 $scope.saveRole = function() {
91 for(x in $scope.availableRoles){
92 if($scope.availableRoles[x].name==$scope.role.name){
94 // $modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
98 confirmBoxService.showInformation( "Role already exists.");
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);
106 $scope.role.name=$scope.role.name.replace('global_','');
110 childRoles: $scope.role.childRoles,
111 roleFunctions : $scope.role.roleFunctions
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;
124 confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
128 console.log('post failed', res.data);
129 confirmBoxService.showInformation("Error while saving.");
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',
141 roleId: function () {
142 return $stateParams.roleId;
147 availableRoles: function () {
148 return $scope.ociavailableRoles;
150 availableRoleFunctions: function () {
151 return $scope.ociavailableRoleFunctions;
156 modalInstance.result.finally(function () {
157 if($stateParams.roleId === '0'){
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',
171 roleId: function () {
172 return $stateParams.roleId;
177 availableRoles: function () {
178 return $scope.ociavailableRoles;
180 availableRoleFunctions: function () {
181 return $scope.ociavailableRoleFunctions;
186 modalInstance.result.finally(function () {
187 if($stateParams.roleId === '0'){
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(
198 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + $stateParams.roleId;
199 var postData={roleFunction:roleFunction};
201 $http.post(uuu, postData).then(
203 $scope.role= response.data.role;
204 $.each($scope.availableRoleFunctions, function(k, c){
205 if(c.code === roleFunction.code) {
211 confirmBoxService.showInformation("Error while saving.");
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(
223 var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
224 var postData={childRole:childRole};
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) {
236 confirmBoxService.showInformation("Error while saving.");