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 * ================================================================================
21 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams){
22 //$scope.role=${role};
24 $( "#dialogRoleFunction" ).hide();
25 $( "#dialogChildRole" ).hide();
27 //$scope.ociavailableRoleFunctions=${availableRoleFunctions};
28 $scope.fetchRoles = function() {
30 RoleService.getRole($stateParams.roleId).then(function(data){
33 $scope.data = JSON.parse(j.data);
35 $scope.role =JSON.parse($scope.data.role);
37 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
38 $scope.availableRoleFunctions=[];
40 if($scope.ociavailableRoleFunctions)
41 $.each($scope.ociavailableRoleFunctions, function(i, a){
42 var availableRoleFunction = a;
43 availableRoleFunction.selected = false;
44 $.each($scope.role.roleFunctions, function(j, b){
45 if(a.code === b.code) {
46 availableRoleFunction.selected = true;
49 $scope.availableRoleFunctions.push(availableRoleFunction);
53 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
54 $scope.availableRoles=[];
56 if($scope.ociavailableRoles)
57 $.each($scope.ociavailableRoles, function(i, a){
58 var availableRole = a;
59 availableRole.selected = false;
60 if($scope.role.childRoles){
61 $.each($scope.role.childRoles, function(j, b){
63 availableRole.selected = true;
67 $scope.availableRoles.push(availableRole);
72 console.log("RoleService.getRole failed", error);
79 $scope.saveRole = function() {
81 for(x in $scope.availableRoles){
82 if($scope.availableRoles[x].name==$scope.role.name){
84 //$modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
88 confirmBoxService.showInformation( "Role already exists.");
91 var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;
94 childRoles: $scope.role.childRoles,
95 roleFunctions : $scope.role.roleFunctions
97 $http.post(uuu, JSON.stringify(postData)).then(function(res) {
98 // console.log('roleController::saveRole: ' + JSON.stringify(res));
99 if (res && res.data && res.data.role)
100 confirmBoxService.showInformation("Update Successful.");
102 confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
105 console.log('post failed', res.data);
106 confirmBoxService.showInformation("Error while saving.");
112 $scope.addNewRoleFunctionModalPopup = function() {
113 var modalInstance = ngDialog.open({
114 templateUrl: 'app/views/role/role_functions_popup.html',
115 controller: 'rolepopupController',
118 roleId: function () {
119 return $stateParams.roleId;
124 availableRoles: function () {
125 return $scope.ociavailableRoles;
127 availableRoleFunctions: function () {
128 return $scope.ociavailableRoleFunctions;
132 modalInstance.closePromise.then(response => {
133 if($stateParams.roleId === '0'){
138 // $scope.role=response.role;
142 $scope.addNewChildRoleModalPopup = function() {
143 var modalInstance = ngDialog.open({
144 templateUrl: 'app/views/role/role_childrole_popup.html',
145 controller: 'rolepopupController',
148 roleId: function () {
149 return $stateParams.roleId;
154 availableRoles: function () {
155 return $scope.ociavailableRoles;
157 availableRoleFunctions: function () {
158 return $scope.ociavailableRoleFunctions;
161 }).closePromise.then(function(response){
162 if($stateParams.roleId === '0'){
167 //$scope.role=response.role;
171 $scope.removeRoleFunction = function(roleFunction) {
172 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(
174 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + $stateParams.roleId;
175 var postData={roleFunction:roleFunction};
177 $http.post(uuu, postData).then(
179 $scope.role= response.data.role;
180 $.each($scope.availableRoleFunctions, function(k, c){
181 if(c.code === roleFunction.code) {
187 confirmBoxService.showInformation("Error while saving.");
196 $scope.removeChildRole = function(childRole) {
197 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(
199 var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
200 var postData={childRole:childRole};
202 $http.post(uuu,postData).then( function(response) {
203 $scope.role=response.data.role;
204 $.each($scope.availableRoles, function(k, c){
205 if(c.id === childRole.id) {
212 confirmBoxService.showInformation("Error while saving.");