2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
21 app.controller('roleController', function ($scope, $http, confirmBoxService, ngDialog, RoleService, conf, $stateParams){
\r
22 //$scope.role=${role};
\r
24 $( "#dialogRoleFunction" ).hide();
\r
25 $( "#dialogChildRole" ).hide();
\r
27 //$scope.ociavailableRoleFunctions=${availableRoleFunctions};
\r
28 $scope.fetchRoles = function() {
\r
30 RoleService.getRole($stateParams.roleId).then(function(data){
\r
33 $scope.data = JSON.parse(j.data);
\r
35 $scope.role =JSON.parse($scope.data.role);
\r
37 $scope.ociavailableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
\r
38 $scope.availableRoleFunctions=[];
\r
40 if($scope.ociavailableRoleFunctions)
\r
41 $.each($scope.ociavailableRoleFunctions, function(i, a){
\r
42 var availableRoleFunction = a;
\r
43 availableRoleFunction.selected = false;
\r
44 $.each($scope.role.roleFunctions, function(j, b){
\r
45 if(a.code === b.code) {
\r
46 availableRoleFunction.selected = true;
\r
49 $scope.availableRoleFunctions.push(availableRoleFunction);
\r
53 $scope.ociavailableRoles=JSON.parse($scope.data.availableRoles);
\r
54 $scope.availableRoles=[];
\r
56 if($scope.ociavailableRoles)
\r
57 $.each($scope.ociavailableRoles, function(i, a){
\r
58 var availableRole = a;
\r
59 availableRole.selected = false;
\r
60 if($scope.role.childRoles){
\r
61 $.each($scope.role.childRoles, function(j, b){
\r
63 availableRole.selected = true;
\r
67 $scope.availableRoles.push(availableRole);
\r
72 console.log("RoleService.getRole failed", error);
\r
77 $scope.fetchRoles();
\r
79 $scope.saveRole = function() {
\r
80 var exists = false,x;
\r
81 for(x in $scope.availableRoles){
\r
82 if($scope.availableRoles[x].name==$scope.role.name){
\r
84 //$modalInstance.close({availableRoleFunctions:message.availableRoleFunctions});
\r
88 confirmBoxService.showInformation( "Role already exists.");
\r
91 var uuu = conf.api.saveRole + "?role_id="+$stateParams.roleId;
\r
94 childRoles: $scope.role.childRoles,
\r
95 roleFunctions : $scope.role.roleFunctions
\r
97 $http.post(uuu, JSON.stringify(postData)).then(function(res) {
\r
98 // console.log('roleController::saveRole: ' + JSON.stringify(res));
\r
99 if (res && res.data && res.data.role)
\r
100 confirmBoxService.showInformation("Update Successful.");
\r
102 confirmBoxService.showInformation('Failed to create role: ' + res.data.error)
\r
105 console.log('post failed', res.data);
\r
106 confirmBoxService.showInformation("Error while saving.");
\r
112 $scope.addNewRoleFunctionModalPopup = function() {
\r
113 var modalInstance = ngDialog.open({
\r
114 templateUrl: 'app/views/role/role_functions_popup.html',
\r
115 controller: 'rolepopupController',
\r
118 roleId: function () {
\r
119 return $stateParams.roleId;
\r
121 role: function () {
\r
122 return $scope.role;
\r
124 availableRoles: function () {
\r
125 return $scope.ociavailableRoles;
\r
127 availableRoleFunctions: function () {
\r
128 return $scope.ociavailableRoleFunctions;
\r
132 modalInstance.closePromise.then(response => {
\r
133 if($stateParams.roleId === '0'){
\r
134 return $scope.role;
\r
136 $scope.fetchRoles();
\r
138 // $scope.role=response.role;
\r
142 $scope.addNewChildRoleModalPopup = function() {
\r
143 var modalInstance = ngDialog.open({
\r
144 templateUrl: 'app/views/role/role_childrole_popup.html',
\r
145 controller: 'rolepopupController',
\r
148 roleId: function () {
\r
149 return $stateParams.roleId;
\r
151 role: function () {
\r
152 return $scope.role;
\r
154 availableRoles: function () {
\r
155 return $scope.ociavailableRoles;
\r
157 availableRoleFunctions: function () {
\r
158 return $scope.ociavailableRoleFunctions;
\r
161 }).closePromise.then(function(response){
\r
162 if($stateParams.roleId === '0'){
\r
163 return $scope.role;
\r
165 $scope.fetchRoles();
\r
167 //$scope.role=response.role;
\r
171 $scope.removeRoleFunction = function(roleFunction) {
\r
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(
\r
173 function(confirmed){
\r
174 var uuu = conf.api.toggleRoleRoleFunction + "?role_id=" + $stateParams.roleId;
\r
175 var postData={roleFunction:roleFunction};
\r
177 $http.post(uuu, postData).then(
\r
178 function(response) {
\r
179 $scope.role= response.data.role;
\r
180 $.each($scope.availableRoleFunctions, function(k, c){
\r
181 if(c.code === roleFunction.code) {
\r
182 c.selected = false;
\r
186 function(response) {
\r
187 confirmBoxService.showInformation("Error while saving.");
\r
196 $scope.removeChildRole = function(childRole) {
\r
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(
\r
198 function(confirmed){
\r
199 var uuu = conf.api.toggleRoleChildRole + "?role_id=" + $stateParams.roleId;
\r
200 var postData={childRole:childRole};
\r
202 $http.post(uuu,postData).then( function(response) {
\r
203 $scope.role=response.data.role;
\r
204 $.each($scope.availableRoles, function(k, c){
\r
205 if(c.id === childRole.id) {
\r
206 c.selected = false;
\r
212 confirmBoxService.showInformation("Error while saving.");
\r