Deliver centralized role management feature
[portal.git] / ecomp-portal-FE-common / client / app / views / role / role-list-controller.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 app.controller('roleListController', function ($scope,RoleService,confirmBoxService,conf,$state,$http,$log){
21         $scope.showSpinner = true;
22         
23         RoleService.getRoles().then(function(data){
24                 $scope.showSpinner = true;
25                 var j = data;
26                 $scope.data = JSON.parse(j.data);
27                 $scope.availableRoles =JSON.parse($scope.data.availableRoles);
28                 $scope.showSpinner = false;
29         
30         },function(error){
31                 $log.debug('failed');
32         });
33         
34         
35         $scope.goToUrl = function(roleIdVal) {
36                         $state.go("root.role", {"roleId":roleIdVal});
37                 }
38                 $scope.toggleRole = function(selected,availableRole) {
39                                 var toggleType = null;
40                                 if(selected) {
41                                         toggleType = "activate";
42                                 } else {
43                                         toggleType = "inactivate";
44                                 }
45                                 
46                                 confirmBoxService.confirm("You are about to "+toggleType+" the role "+availableRole.name+". Do you want to continue?").then(
47                                 function(confirmed){
48                                                 
49                                         if(confirmed) {
50                                         var uuu = conf.api.toggleRole;
51                                                                 
52                                                                 var postData={role:availableRole};
53                                                                 $http.post(uuu, postData).then(function(response) {
54                                                                         var data = response.data;
55                                                                         if (typeof data === 'object') {
56                                                                                 $log.debug('data:'+data);
57                                                                                 $scope.availableRoles=data.availableRoles; 
58                                                                                 $log.debug('role::availableRoles:'+$scope.availableRoles);
59                                                                         } else {
60                                                                                 //
61                                                                         }
62
63                                                                 }, function(response) {
64                                                                         debug.log('response:'+response);
65                                                                         availableRole.active=!availableRole.active;
66                                                                         confirmBoxService.showInformation("Error while saving.");
67                                                                 });
68                                                                 
69                                                  }
70
71                         }) .catch(function(err) {
72                                 $log.error('roleListController::confirmBoxService.confirm error:', err);
73                                 availableRole.active=!availableRole.active;                                     
74                         });
75                                 
76                                   
77                 };
78
79                 $scope.removeRole = function(role) {
80                         
81                         confirmBoxService.confirm("You are about to delete the role "+role.name+". Do you want to continue?").then(
82                                 function(confirmed){
83                                                         var uuu = conf.api.removeRole;
84                                                           var postData={role:role};
85
86                                                           
87                                                           
88                                                           $http.post(uuu, postData).then(function(response) {
89                                                                         var data = response.data;
90                                                                         if (typeof data === 'object') {
91                                                                                 $scope.availableRoles=data.availableRoles; 
92                                                                         } else {
93                                                                                 //
94                                                                         }
95
96                                                                 }, function(response) {
97                                                                         $log.debug('response:'+response.data);
98                                                                         confirmBoxService.showInformation("Error while deleting: "+ data.responseText);
99                                                                 });
100                                 
101                 });
102                 
103                         
104                 };
105                 
106
107 });