2018-08-02 05:48:47 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('underscore');
|
|
|
|
const Mn = require('backbone.marionette');
|
|
|
|
const App = require('../../main');
|
|
|
|
const CertificateModel = require('../../../models/certificate');
|
|
|
|
const template = require('./form.ejs');
|
|
|
|
|
|
|
|
require('jquery-serializejson');
|
|
|
|
require('selectize');
|
|
|
|
|
|
|
|
module.exports = Mn.View.extend({
|
|
|
|
template: template,
|
|
|
|
className: 'modal-dialog',
|
|
|
|
max_file_size: 5120,
|
|
|
|
|
|
|
|
ui: {
|
|
|
|
form: 'form',
|
|
|
|
domain_names: 'input[name="domain_names"]',
|
|
|
|
buttons: '.modal-footer button',
|
|
|
|
cancel: 'button.cancel',
|
|
|
|
save: 'button.save',
|
|
|
|
other_ssl_certificate: '#other_ssl_certificate',
|
2018-08-07 06:27:20 -04:00
|
|
|
other_ssl_certificate_key: '#other_ssl_certificate_key'
|
2018-08-02 05:48:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click @ui.save': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (!this.ui.form[0].checkValidity()) {
|
|
|
|
$('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let view = this;
|
|
|
|
let data = this.ui.form.serializeJSON();
|
2018-08-07 06:27:20 -04:00
|
|
|
data.provider = this.model.get('provider');
|
2018-08-02 05:48:47 -04:00
|
|
|
|
|
|
|
// Manipulate
|
2018-08-07 06:27:20 -04:00
|
|
|
if (typeof data.meta !== 'undefined' && typeof data.meta.letsencrypt_agree !== 'undefined') {
|
|
|
|
data.meta.letsencrypt_agree = !!data.meta.letsencrypt_agree;
|
|
|
|
}
|
2018-08-02 05:48:47 -04:00
|
|
|
|
|
|
|
if (typeof data.domain_names === 'string' && data.domain_names) {
|
|
|
|
data.domain_names = data.domain_names.split(',');
|
|
|
|
}
|
|
|
|
|
2018-08-07 06:27:20 -04:00
|
|
|
let method = App.Api.Nginx.Certificates.create;
|
|
|
|
let is_new = true;
|
|
|
|
let ssl_files = [];
|
2018-08-02 05:48:47 -04:00
|
|
|
|
|
|
|
if (this.model.get('id')) {
|
|
|
|
// edit
|
|
|
|
is_new = false;
|
2018-08-07 06:27:20 -04:00
|
|
|
method = App.Api.Nginx.Certificates.update;
|
2018-08-02 05:48:47 -04:00
|
|
|
data.id = this.model.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
// check files are attached
|
2018-08-07 06:27:20 -04:00
|
|
|
if (this.model.get('provider') === 'other' && !this.model.hasSslFiles()) {
|
2018-08-02 05:48:47 -04:00
|
|
|
if (!this.ui.other_ssl_certificate[0].files.length || !this.ui.other_ssl_certificate[0].files[0].size) {
|
2018-08-07 06:27:20 -04:00
|
|
|
alert('certificate file is not attached');
|
|
|
|
return;
|
2018-08-02 05:48:47 -04:00
|
|
|
} else {
|
|
|
|
if (this.ui.other_ssl_certificate[0].files[0].size > this.max_file_size) {
|
|
|
|
alert('certificate file is too large (> 5kb)');
|
|
|
|
return;
|
|
|
|
}
|
2018-08-07 06:27:20 -04:00
|
|
|
ssl_files.push({name: 'certificate', file: this.ui.other_ssl_certificate[0].files[0]});
|
2018-08-02 05:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.ui.other_ssl_certificate_key[0].files.length || !this.ui.other_ssl_certificate_key[0].files[0].size) {
|
2018-08-07 06:27:20 -04:00
|
|
|
alert('certificate key file is not attached');
|
|
|
|
return;
|
2018-08-02 05:48:47 -04:00
|
|
|
} else {
|
|
|
|
if (this.ui.other_ssl_certificate_key[0].files[0].size > this.max_file_size) {
|
|
|
|
alert('certificate key file is too large (> 5kb)');
|
|
|
|
return;
|
|
|
|
}
|
2018-08-07 06:27:20 -04:00
|
|
|
ssl_files.push({name: 'certificate_key', file: this.ui.other_ssl_certificate_key[0].files[0]});
|
2018-08-02 05:48:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
|
|
|
method(data)
|
|
|
|
.then(result => {
|
|
|
|
view.model.set(result);
|
|
|
|
|
|
|
|
// Now upload the certs if we need to
|
|
|
|
if (ssl_files.length) {
|
|
|
|
let form_data = new FormData();
|
|
|
|
|
|
|
|
ssl_files.map(function (file) {
|
|
|
|
form_data.append(file.name, file.file);
|
|
|
|
});
|
|
|
|
|
2018-08-07 06:27:20 -04:00
|
|
|
return App.Api.Nginx.Certificates.upload(view.model.get('id'), form_data)
|
2018-08-02 05:48:47 -04:00
|
|
|
.then(result => {
|
|
|
|
view.model.set('meta', _.assign({}, view.model.get('meta'), result));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
App.UI.closeModal(function () {
|
|
|
|
if (is_new) {
|
2018-08-07 06:27:20 -04:00
|
|
|
App.Controller.showNginxCertificates();
|
2018-08-02 05:48:47 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
alert(err.message);
|
|
|
|
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
templateContext: {
|
|
|
|
getLetsencryptEmail: function () {
|
|
|
|
return typeof this.meta.letsencrypt_email !== 'undefined' ? this.meta.letsencrypt_email : App.Cache.User.get('email');
|
|
|
|
},
|
|
|
|
|
|
|
|
getLetsencryptAgree: function () {
|
|
|
|
return typeof this.meta.letsencrypt_agree !== 'undefined' ? this.meta.letsencrypt_agree : false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onRender: function () {
|
|
|
|
this.ui.domain_names.selectize({
|
|
|
|
delimiter: ',',
|
|
|
|
persist: false,
|
|
|
|
maxOptions: 15,
|
|
|
|
create: function (input) {
|
|
|
|
return {
|
|
|
|
value: input,
|
|
|
|
text: input
|
|
|
|
};
|
|
|
|
},
|
2018-08-07 06:27:20 -04:00
|
|
|
createFilter: /^(?:[^.*]+\.?)+[^.]$/
|
2018-08-02 05:48:47 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
if (typeof options.model === 'undefined' || !options.model) {
|
2018-08-07 06:27:20 -04:00
|
|
|
this.model = new CertificateModel.Model({provider: 'letsencrypt'});
|
2018-08-02 05:48:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|