blob: 45b8c997b17d0fba648f8f4b7fb1aa29af6f9c12 [file] [log] [blame]
beccabroek309b5da2018-11-07 12:22:31 -06001window.angular && (function(angular) {
2 'use strict';
3
4 angular.module('app.common.directives').directive('certificate', [
5 'APIUtils',
6 function(APIUtils) {
7 return {
8 'restrict': 'E',
9 'template': require('./certificate.html'),
10 'scope': {'cert': '=', 'reload': '&'},
11 'controller': [
Yoshie Muranakadbafdc52019-06-28 09:41:19 -050012 '$scope', 'APIUtils', 'toastService', 'Constants',
13 function($scope, APIUtils, toastService, Constants) {
beccabroek309b5da2018-11-07 12:22:31 -060014 var certificateType = 'PEM';
Yoshie Muranakadbafdc52019-06-28 09:41:19 -050015 var availableCertificateTypes = Constants.CERTIFICATE_TYPES;
16
17 /**
18 * This function is needed to map the backend Description to what
19 * should appear in the GUI. This is needed specifically for CA
20 * certificate types. The backend description for the certificate
21 * type is 'TrustStore Certificate', this function will make sure we
22 * display 'CA Certificate' on the frontend
23 * @param {string} : certificate Description property
24 * @returns {string} : certificate name that should appear on GUI
25 */
26 $scope.getCertificateName = function(certificateDescription) {
27 var matched =
28 availableCertificateTypes.find(function(certificate) {
29 return certificate.Description === certificateDescription;
30 });
31 if (matched === undefined) {
32 return '';
33 } else {
34 return matched.name;
35 }
36 };
37
beccabroek309b5da2018-11-07 12:22:31 -060038 $scope.replaceCertificate = function(certificate) {
39 $scope.loading = true;
40 if (certificate.file.name.split('.').pop() !==
41 certificateType.toLowerCase()) {
42 toastService.error(
43 'Certificate must be replaced with a .pem file.');
44 return;
45 }
46 var file =
47 document
48 .getElementById(
49 'upload_' + certificate.Description + certificate.Id)
50 .files[0];
51 var reader = new FileReader();
52 reader.onloadend = function(e) {
53 var data = {};
54 data.CertificateString = e.target.result;
55 data.CertificateUri = {'@odata.id': certificate['@odata.id']};
56 data.CertificateType = certificateType;
57 APIUtils.replaceCertificate(data).then(
58 function(data) {
59 $scope.loading = false;
60 toastService.success(
Yoshie Muranakadbafdc52019-06-28 09:41:19 -050061 $scope.getCertificateName(certificate.Description) +
62 ' was replaced.');
beccabroek309b5da2018-11-07 12:22:31 -060063 $scope.reload();
64 },
65 function(error) {
66 console.log(error);
67 $scope.loading = false;
68 toastService.error(
Yoshie Muranakadbafdc52019-06-28 09:41:19 -050069 'Unable to replace ' +
70 $scope.getCertificateName(certificate.Description));
beccabroek309b5da2018-11-07 12:22:31 -060071 });
72 };
73 reader.readAsBinaryString(file);
74 };
75 }
76 ]
77 };
78 }
79 ]);
80})(window.angular);