blob: 7fa42a1017602cb7a54633d5b2f4cc7a0661336e [file] [log] [blame]
/**
* Controller for Certificate Management
*
* @module app/configuration
* @exports certificateController
* @name certificateController
*/
window.angular && (function(angular) {
'use strict';
angular.module('app.configuration').controller('certificateController', [
'$scope', 'APIUtils', '$q', 'Constants', 'toastService',
function($scope, APIUtils, $q, Constants, toastService) {
$scope.loading = false;
$scope.certificates = [];
$scope.availableCertificateTypes = [];
$scope.addCertificateModal = false;
$scope.newCertificate = {};
$scope.submitted = false;
$scope.loadCertificates = function() {
$scope.certificates = [];
$scope.availableCertificateTypes = Constants.CERTIFICATE_TYPES;
$scope.loading = true;
// Use Certificate Service to get the locations of all the certificates,
// then add a promise for fetching each certificate
APIUtils.getCertificateLocations().then(
function(data) {
var promises = [];
var locations = data.Links.Certificates;
for (var i in locations) {
var location = locations[i];
promises.push(getCertificatePromise(location['@odata.id']));
}
$q.all(promises)
.catch(function(error) {
toastService.error('Failed to load certificates.');
console.log(JSON.stringify(error));
})
.finally(function() {
$scope.loading = false;
});
},
function(error) {
$scope.loading = false;
$scope.availableCertificateTypes = [];
toastService.error('Failed to load certificates.');
console.log(JSON.stringify(error));
});
};
$scope.uploadCertificate = function() {
if ($scope.newCertificate.file.name.split('.').pop() !== 'pem') {
toastService.error('Certificate must be a .pem file.');
return;
}
$scope.addCertificateModal = false;
APIUtils
.addNewCertificate(
$scope.newCertificate.file, $scope.newCertificate.selectedType)
.then(
function(data) {
toastService.success(
$scope.newCertificate.selectedType.Description +
' was uploaded.');
$scope.newCertificate = {};
$scope.loadCertificates();
},
function(error) {
toastService.error(
$scope.newCertificate.selectedType.Description +
' failed upload.');
console.log(JSON.stringify(error));
});
};
var getCertificatePromise = function(url) {
var promise = APIUtils.getCertificate(url).then(function(data) {
var certificate = data;
isExpiring(certificate);
updateAvailableTypes(certificate);
$scope.certificates.push(certificate);
});
return promise;
};
var isExpiring = function(certificate) {
// if ValidNotAfter is less than or equal to 30 days from today
// (2592000000), isExpiring. If less than or equal to 0, is expired.
var difference = certificate.ValidNotAfter - new Date();
if (difference <= 0) {
certificate.isExpired = true;
} else if (difference <= 2592000000) {
certificate.isExpiring = true;
} else {
certificate.isExpired = false;
certificate.isExpiring = false;
}
};
var updateAvailableTypes = function(certificate) {
// TODO: at this time only one of each type of certificate is allowed.
// When this changes, this will need to be updated.
// Removes certificate type from available types to be added.
$scope.availableCertificateTypes =
$scope.availableCertificateTypes.filter(function(type) {
return type.Description !== certificate.Description;
});
};
$scope.getDays = function(endDate) {
// finds number of days until certificate expiration
var ms = endDate - new Date();
return Math.floor(ms / (24 * 60 * 60 * 1000));
};
$scope.loadCertificates();
}
]);
})(angular);