164 lines
5.6 KiB
JavaScript
Raw Normal View History

2015-07-21 13:36:03 -07:00
'use strict';
2015-06-22 13:47:27 -07:00
angular.module('lemur')
.service('CertificateApi', function (LemurRestangular, DomainService) {
LemurRestangular.extendModel('certificates', function (obj) {
return angular.extend(obj, {
attachAuthority: function (authority) {
this.authority = authority;
this.authority.maxDate = moment(this.authority.notAfter).subtract(1, 'days').format('YYYY/MM/DD');
},
attachSubAltName: function () {
if (this.extensions === undefined) {
this.extensions = {};
}
if (this.extensions.subAltNames === undefined) {
this.extensions.subAltNames = {'names': []};
}
if (!angular.isString(this.subAltType)) {
this.subAltType = 'CNAME';
}
if (angular.isString(this.subAltValue) && angular.isString(this.subAltType)) {
this.extensions.subAltNames.names.push({'nameType': this.subAltType, 'value': this.subAltValue});
this.findDuplicates();
}
this.subAltType = null;
this.subAltValue = null;
},
removeSubAltName: function (index) {
this.extensions.subAltNames.names.splice(index, 1);
this.findDuplicates();
},
attachCustom: function () {
if (this.extensions === undefined || this.extensions.custom === undefined) {
this.extensions = {'custom': []};
}
if (angular.isString(this.customOid) && angular.isString(this.customEncoding) && angular.isString(this.customValue)) {
this.extensions.custom.push(
{
'oid': this.customOid,
'isCritical': this.customIsCritical,
'encoding': this.customEncoding,
'value': this.customValue
}
);
}
this.customOid = null;
this.customIsCritical = null;
this.customEncoding = null;
this.customValue = null;
},
removeCustom: function (index) {
this.extensions.custom.splice(index, 1);
},
attachDestination: function (destination) {
this.selectedDestination = null;
if (this.destinations === undefined) {
this.destinations = [];
2015-06-22 13:47:27 -07:00
}
this.destinations.push(destination);
2015-06-22 13:47:27 -07:00
},
removeDestination: function (index) {
this.destinations.splice(index, 1);
2015-06-22 13:47:27 -07:00
},
attachNotification: function (notification) {
this.selectedNotification = null;
if (this.notifications === undefined) {
this.notifications = [];
}
this.notifications.push(notification);
},
removeNotification: function (index) {
this.notifications.splice(index, 1);
},
2015-06-22 13:47:27 -07:00
findDuplicates: function () {
DomainService.findDomainByName(this.extensions.subAltNames[0]).then(function (domains) { //We should do a better job of searching for multiple domains
2015-06-22 13:47:27 -07:00
this.duplicates = domains.total;
});
},
useTemplate: function () {
this.extensions = this.template.extensions;
}
});
});
return LemurRestangular.all('certificates');
})
2015-09-04 15:52:56 -07:00
.service('CertificateService', function ($location, CertificateApi, LemurRestangular, DefaultService, toaster) {
2015-06-22 13:47:27 -07:00
var CertificateService = this;
CertificateService.findCertificatesByName = function (filterValue) {
return CertificateApi.getList({'filter[name]': filterValue})
.then(function (certificates) {
return certificates;
});
};
CertificateService.create = function (certificate) {
certificate.attachSubAltName();
2015-11-23 14:30:23 -08:00
return CertificateApi.post(certificate);
2015-06-22 13:47:27 -07:00
};
CertificateService.update = function (certificate) {
2015-11-23 14:30:23 -08:00
return LemurRestangular.copy(certificate).put();
2015-06-22 13:47:27 -07:00
};
CertificateService.upload = function (certificate) {
2015-11-23 14:30:23 -08:00
return CertificateApi.customPOST(certificate, 'upload');
2015-06-22 13:47:27 -07:00
};
CertificateService.getAuthority = function (certificate) {
return certificate.customGET('authority').then(function (authority) {
2015-06-22 13:47:27 -07:00
certificate.authority = authority;
});
};
CertificateService.getCreator = function (certificate) {
return certificate.customGET('creator').then(function (creator) {
2015-06-22 13:47:27 -07:00
certificate.creator = creator;
});
};
CertificateService.getDestinations = function (certificate) {
return certificate.getList('destinations').then(function (destinations) {
certificate.destinations = destinations;
2015-06-22 13:47:27 -07:00
});
};
CertificateService.getNotifications = function (certificate) {
return certificate.getList('notifications').then(function (notifications) {
certificate.notifications = notifications;
});
};
2015-06-22 13:47:27 -07:00
CertificateService.getDomains = function (certificate) {
return certificate.getList('domains').then(function (domains) {
2015-06-22 13:47:27 -07:00
certificate.domains = domains;
});
};
CertificateService.getDefaults = function (certificate) {
2015-09-04 15:52:56 -07:00
return DefaultService.get().then(function (defaults) {
certificate.country = defaults.country;
certificate.state = defaults.state;
certificate.location = defaults.location;
certificate.organization = defaults.organization;
certificate.organizationalUnit = defaults.organizationalUnit;
});
};
2015-11-23 14:30:23 -08:00
CertificateService.loadPrivateKey = function (certificate) {
return certificate.customGET('key');
};
2015-06-22 13:47:27 -07:00
CertificateService.updateActive = function (certificate) {
2015-11-23 14:30:23 -08:00
return certificate.put();
2015-06-22 13:47:27 -07:00
};
return CertificateService;
});