Add capability for multi-role support
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / PolicyAddScopeRoleController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20 app.controller('editRoleController' ,  function ($scope, PolicyAppService, $modalInstance, message){
21     if (message.editRoleData != null) {
22         $scope.label='Edit Role'
23         $scope.disableCd=true;
24     } else {
25         $scope.label='Add Role'
26         $scope.disableCd=false;
27         message.editRoleData = {
28             role : "mechid"
29         }
30     }
31
32     $scope.editRole = message.editRoleData;
33
34     $scope.activeScopes = [];
35     if (message.editRoleData != null && message.editRoleData.scope != null) {
36         if (message.editRoleData.scope.constructor === Array) {
37             $scope.activeScopes = message.editRoleData.scope;
38         } else {
39             $scope.activeScopes = message.editRoleData.scope.split(',');
40         }
41     }
42
43     PolicyAppService.getData('get_PolicyRolesScopeData').then(function (data) {
44         var j = data;
45         $scope.data = JSON.parse(j.data);
46         console.log($scope.data);
47         $scope.scopeDatas = JSON.parse($scope.data.scopeDatas);
48         console.log($scope.scopeDatas);
49     }, function (error) {
50         console.log("failed");
51     });
52
53     $scope.saveRole = function(editRoleData) {
54         var uuu = "save_NonSuperRolesData.htm";
55         editRoleData.scope = $scope.activeScopes;
56         var postData={editRoleData: editRoleData};
57         $.ajax({
58             type : 'POST',
59             url : uuu,
60             dataType: 'json',
61             contentType: 'application/json',
62             data: JSON.stringify(postData),
63             success : function(data){
64                 $scope.$apply(function(){
65                     $scope.rolesDatas=data.rolesDatas;});
66                 console.log($scope.rolesDatas);
67                 $modalInstance.close({rolesDatas:$scope.rolesDatas});
68             },
69             error : function(data){
70                 alert("Error while saving Role.");
71             }
72         });
73     };
74
75
76     $scope.createMechidScope = function(editRoleData) {
77         var uuu = "save_NewMechidScopesData";
78         editRoleData.scope = $scope.activeScopes;
79         var postData={editRoleData: editRoleData};
80         $.ajax({
81             type : 'POST',
82             url : uuu,
83             dataType: 'json',
84             contentType: 'application/json',
85             data: JSON.stringify(postData),
86             success : function(data){
87                 $scope.$apply(function(){
88                     $scope.rolesDatas=data.rolesDatas;});
89                 console.log($scope.rolesDatas);
90                 $modalInstance.close({rolesDatas:$scope.rolesDatas});
91             },
92             error : function(data) {
93                 alert("Error while Creating Mechid scopes.");
94             }
95         });
96     };
97
98
99
100     $scope.addScope = function(scopes) {
101         for (var i = 0; i < scopes.length; i++) {
102             if ($.inArray(scopes[i], $scope.activeScopes) === -1) {
103                 $scope.activeScopes.push(scopes[i]);
104             }
105         }
106     };
107     $scope.deleteScope = function(index) {
108         $scope.activeScopes.splice(index, 1);
109     };
110 });