1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
window.angular && (function(angular) {
'use strict';
angular.module('app.common.directives').directive('certificate', [
'APIUtils',
function(APIUtils) {
return {
'restrict': 'E',
'template': require('./certificate.html'),
'scope': {'cert': '=', 'reload': '&'},
'controller': [
'$scope', 'APIUtils', 'toastService', 'Constants', '$uibModal',
function($scope, APIUtils, toastService, Constants, $uibModal) {
var certificateType = 'PEM';
var availableCertificateTypes = Constants.CERTIFICATE_TYPES;
/**
* This function is needed to map the backend Description to what
* should appear in the GUI. This is needed specifically for CA
* certificate types. The backend description for the certificate
* type is 'TrustStore Certificate', this function will make sure we
* display 'CA Certificate' on the frontend
* @param {string} : certificate Description property
* @returns {string} : certificate name that should appear on GUI
*/
$scope.getCertificateName = function(certificateDescription) {
var matched =
availableCertificateTypes.find(function(certificate) {
return certificate.Description === certificateDescription;
});
if (matched === undefined) {
return '';
} else {
return matched.name;
}
};
$scope.isDeletable = function(certificate) {
return certificate.Description == 'TrustStore Certificate';
};
$scope.confirmDeleteCert = function(certificate) {
initRemoveModal(certificate);
};
/**
* Intiate remove certificate modal
* @param {*} certificate
*/
function initRemoveModal(certificate) {
const template = require('./certificate-modal-remove.html');
$uibModal
.open({
template,
windowTopClass: 'uib-modal',
ariaLabelledBy: 'dialog_label',
controllerAs: 'modalCtrl',
controller: function() {
this.certificate = certificate;
}
})
.result
.then(() => {
deleteCert(certificate);
})
.catch(
() => {
// do nothing
})
};
/**
* Removes certificate
* @param {*} certificate
*/
function deleteCert(certificate) {
$scope.confirm_delete = false;
APIUtils.deleteRedfishObject(certificate['@odata.id'])
.then(
function(data) {
$scope.loading = false;
toastService.success(
$scope.getCertificateName(certificate.Description) +
' was deleted.');
$scope.reload();
},
function(error) {
console.log(error);
$scope.loading = false;
toastService.error(
'Unable to delete ' +
$scope.getCertificateName(certificate.Description));
});
return;
};
$scope.replaceCertificate = function(certificate) {
$scope.loading = true;
if (certificate.file.name.split('.').pop() !==
certificateType.toLowerCase()) {
toastService.error(
'Certificate must be replaced with a .pem file.');
return;
}
var file =
document
.getElementById(
'upload_' + certificate.Description + certificate.Id)
.files[0];
var reader = new FileReader();
reader.onloadend = function(e) {
var data = {};
data.CertificateString = e.target.result;
data.CertificateUri = {'@odata.id': certificate['@odata.id']};
data.CertificateType = certificateType;
APIUtils.replaceCertificate(data).then(
function(data) {
$scope.loading = false;
toastService.success(
$scope.getCertificateName(certificate.Description) +
' was replaced.');
$scope.reload();
},
function(error) {
console.log(error);
$scope.loading = false;
toastService.error(
'Unable to replace ' +
$scope.getCertificateName(certificate.Description));
});
};
reader.readAsBinaryString(file);
};
}
]
};
}
]);
})(window.angular);
|