Remove compass4nfv weekly danube job
[releng.git] / utils / test / testapi / opnfv_testapi / ui / profile / profileController.js
1 /*
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 (function () {
17     'use strict';
18
19     angular
20         .module('testapiApp')
21         .factory('PubKeys', PubKeys);
22
23     PubKeys.$inject = ['$resource', 'testapiApiUrl'];
24
25     /**
26      * This is a provider for the user's uploaded public keys.
27      */
28     function PubKeys($resource, testapiApiUrl) {
29         return $resource(testapiApiUrl + '/user/pubkeys/:id', null, null);
30     }
31
32     angular
33         .module('testapiApp')
34         .controller('ProfileController', ProfileController);
35
36     ProfileController.$inject = [
37         '$scope', '$http', 'testapiApiUrl', 'PubKeys',
38         '$uibModal', 'raiseAlert', '$state'
39     ];
40
41     /**
42      * TestAPI Profile Controller
43      * This controller handles user's profile page, where a user can view
44      * account-specific information.
45      */
46     function ProfileController($scope, $http, testapiApiUrl,
47         PubKeys, $uibModal, raiseAlert, $state) {
48
49         var ctrl = this;
50
51         ctrl.updatePubKeys = updatePubKeys;
52         ctrl.openImportPubKeyModal = openImportPubKeyModal;
53         ctrl.openShowPubKeyModal = openShowPubKeyModal;
54
55         // Must be authenticated to view this page.
56         if (!$scope.auth.isAuthenticated) {
57             $state.go('home');
58         }
59
60         /**
61          * This function will fetch all the user's public keys from the
62          * server and store them in an array.
63          */
64         function updatePubKeys() {
65             var keys = PubKeys.query(function() {
66                 ctrl.pubkeys = [];
67                 angular.forEach(keys, function (key) {
68                     ctrl.pubkeys.push({
69                         'resource': key,
70                         'format': key.format,
71                         'shortKey': [
72                             key.pubkey.slice(0, 10),
73                             '.',
74                             key.pubkey.slice(-10)
75                         ].join('.'),
76                         'pubkey': key.pubkey,
77                         'comment': key.comment
78                     });
79                 });
80             });
81         }
82
83         /**
84          * This function will open the modal that will give the user a form
85          * for importing a public key.
86          */
87         function openImportPubKeyModal() {
88             $uibModal.open({
89                 templateUrl: '/components/profile/importPubKeyModal.html',
90                 backdrop: true,
91                 windowClass: 'modal',
92                 controller: 'ImportPubKeyModalController as modal'
93             }).result.finally(function() {
94                 ctrl.updatePubKeys();
95             });
96         }
97
98         /**
99          * This function will open the modal that will give the full
100          * information regarding a specific public key.
101          * @param {Object} pubKey resource
102          */
103         function openShowPubKeyModal(pubKey) {
104             $uibModal.open({
105                 templateUrl: '/components/profile/showPubKeyModal.html',
106                 backdrop: true,
107                 windowClass: 'modal',
108                 controller: 'ShowPubKeyModalController as modal',
109                 resolve: {
110                     pubKey: function() {
111                         return pubKey;
112                     }
113                 }
114             }).result.finally(function() {
115                 ctrl.updatePubKeys();
116             });
117         }
118
119         ctrl.authRequest = $scope.auth.doSignCheck().then(ctrl.updatePubKeys);
120     }
121
122     angular
123         .module('testapiApp')
124         .controller('ImportPubKeyModalController', ImportPubKeyModalController);
125
126     ImportPubKeyModalController.$inject = [
127         '$uibModalInstance', 'PubKeys', 'raiseAlert'
128     ];
129
130     /**
131      * Import Pub Key Modal Controller
132      * This controller is for the modal that appears if a user wants to import
133      * a public key.
134      */
135     function ImportPubKeyModalController($uibModalInstance,
136         PubKeys, raiseAlert) {
137
138         var ctrl = this;
139
140         ctrl.importPubKey = importPubKey;
141         ctrl.cancel = cancel;
142
143         /**
144          * This function will save a new public key resource to the API server.
145          */
146         function importPubKey() {
147             var newPubKey = new PubKeys(
148                 {raw_key: ctrl.raw_key, self_signature: ctrl.self_signature}
149             );
150             newPubKey.$save(
151                 function(newPubKey_) {
152                     raiseAlert('success', '', 'Public key saved successfully');
153                     $uibModalInstance.close(newPubKey_);
154                 },
155                 function(httpResp) {
156                     raiseAlert('danger',
157                         httpResp.statusText, httpResp.data.title);
158                     ctrl.cancel();
159                 }
160             );
161         }
162
163         /**
164          * This function will dismiss the modal.
165          */
166         function cancel() {
167             $uibModalInstance.dismiss('cancel');
168         }
169     }
170
171     angular
172         .module('testapiApp')
173         .controller('ShowPubKeyModalController', ShowPubKeyModalController);
174
175     ShowPubKeyModalController.$inject = [
176         '$uibModalInstance', 'raiseAlert', 'pubKey'
177     ];
178
179     /**
180      * Show Pub Key Modal Controller
181      * This controller is for the modal that appears if a user wants to see the
182      * full details of one of their public keys.
183      */
184     function ShowPubKeyModalController($uibModalInstance, raiseAlert, pubKey) {
185         var ctrl = this;
186
187         ctrl.deletePubKey = deletePubKey;
188         ctrl.cancel = cancel;
189
190         ctrl.pubKey = pubKey.resource;
191         ctrl.rawKey = [pubKey.format, pubKey.pubkey, pubKey.comment].join('\n');
192
193         /**
194          * This function will delete a public key resource.
195          */
196         function deletePubKey() {
197             ctrl.pubKey.$remove(
198                 {id: ctrl.pubKey.id},
199                 function() {
200                     raiseAlert('success',
201                         '', 'Public key deleted successfully');
202                     $uibModalInstance.close(ctrl.pubKey.id);
203                 },
204                 function(httpResp) {
205                     raiseAlert('danger',
206                         httpResp.statusText, httpResp.data.title);
207                     ctrl.cancel();
208                 }
209             );
210         }
211
212         /**
213          * This method will dismiss the modal.
214          */
215         function cancel() {
216             $uibModalInstance.dismiss('cancel');
217         }
218     }
219 })();