2018-07-25 18:23:32 -04:00
|
|
|
'use strict';
|
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
const Mn = require('backbone.marionette');
|
|
|
|
const App = require('../../main');
|
|
|
|
const DeadHostModel = require('../../../models/dead-host');
|
|
|
|
const template = require('./form.ejs');
|
|
|
|
const certListItemTemplate = require('../certificates-list-item.ejs');
|
|
|
|
const Helpers = require('../../../lib/helpers');
|
2018-07-25 18:23:32 -04:00
|
|
|
|
|
|
|
require('jquery-serializejson');
|
|
|
|
require('selectize');
|
|
|
|
|
|
|
|
module.exports = Mn.View.extend({
|
2018-08-15 23:08:56 -04:00
|
|
|
template: template,
|
|
|
|
className: 'modal-dialog',
|
2018-07-25 18:23:32 -04:00
|
|
|
|
|
|
|
ui: {
|
2018-08-15 23:08:56 -04:00
|
|
|
form: 'form',
|
|
|
|
domain_names: 'input[name="domain_names"]',
|
|
|
|
buttons: '.modal-footer button',
|
|
|
|
cancel: 'button.cancel',
|
|
|
|
save: 'button.save',
|
|
|
|
certificate_select: 'select[name="certificate_id"]',
|
|
|
|
ssl_forced: 'input[name="ssl_forced"]',
|
|
|
|
letsencrypt: '.letsencrypt'
|
2018-07-25 18:23:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2018-08-15 23:08:56 -04:00
|
|
|
'change @ui.certificate_select': function () {
|
|
|
|
let id = this.ui.certificate_select.val();
|
|
|
|
if (id === 'new') {
|
|
|
|
this.ui.letsencrypt.show().find('input').prop('disabled', false);
|
|
|
|
} else {
|
|
|
|
this.ui.letsencrypt.hide().find('input').prop('disabled', true);
|
|
|
|
}
|
2018-07-25 18:23:32 -04:00
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
let enabled = id === 'new' || parseInt(id, 10) > 0;
|
|
|
|
this.ui.ssl_forced.prop('disabled', !enabled).parents('.form-group').css('opacity', enabled ? 1 : 0.5);
|
2018-07-25 18:23:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
'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();
|
|
|
|
|
|
|
|
// Manipulate
|
2018-08-15 23:08:56 -04:00
|
|
|
if (typeof data.ssl_forced !== 'undefined' && data.ssl_forced === '1') {
|
|
|
|
data.ssl_forced = true;
|
2018-07-25 18:23:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data.domain_names === 'string' && data.domain_names) {
|
|
|
|
data.domain_names = data.domain_names.split(',');
|
|
|
|
}
|
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
// Check for any domain names containing wildcards, which are not allowed with letsencrypt
|
|
|
|
if (data.certificate_id === 'new') {
|
|
|
|
let domain_err = false;
|
|
|
|
data.domain_names.map(function (name) {
|
|
|
|
if (name.match(/\*/im)) {
|
|
|
|
domain_err = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (domain_err) {
|
|
|
|
alert('Cannot request Let\'s Encrypt Certificate for wildcard domains');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.meta.letsencrypt_agree = data.meta.letsencrypt_agree === '1';
|
|
|
|
} else {
|
|
|
|
data.certificate_id = parseInt(data.certificate_id, 0);
|
|
|
|
}
|
2018-07-25 18:23:32 -04:00
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
let method = App.Api.Nginx.DeadHosts.create;
|
|
|
|
let is_new = true;
|
2018-07-25 18:23:32 -04:00
|
|
|
|
|
|
|
if (this.model.get('id')) {
|
|
|
|
// edit
|
|
|
|
is_new = false;
|
|
|
|
method = App.Api.Nginx.DeadHosts.update;
|
|
|
|
data.id = this.model.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
|
|
|
method(data)
|
|
|
|
.then(result => {
|
|
|
|
view.model.set(result);
|
|
|
|
|
|
|
|
App.UI.closeModal(function () {
|
|
|
|
if (is_new) {
|
|
|
|
App.Controller.showNginxDead();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
alert(err.message);
|
|
|
|
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
templateContext: {
|
|
|
|
getLetsencryptEmail: function () {
|
2018-08-15 23:08:56 -04:00
|
|
|
return App.Cache.User.get('email');
|
2018-07-25 18:23:32 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onRender: function () {
|
2018-08-15 23:08:56 -04:00
|
|
|
let view = this;
|
2018-07-25 18:23:32 -04:00
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
// Domain names
|
2018-07-25 18:23:32 -04:00
|
|
|
this.ui.domain_names.selectize({
|
|
|
|
delimiter: ',',
|
|
|
|
persist: false,
|
|
|
|
maxOptions: 15,
|
|
|
|
create: function (input) {
|
|
|
|
return {
|
|
|
|
value: input,
|
|
|
|
text: input
|
|
|
|
};
|
|
|
|
},
|
|
|
|
createFilter: /^(?:\*\.)?(?:[^.*]+\.?)+[^.]$/
|
|
|
|
});
|
2018-08-15 23:08:56 -04:00
|
|
|
|
|
|
|
// Certificates
|
|
|
|
this.ui.letsencrypt.hide();
|
|
|
|
this.ui.certificate_select.selectize({
|
|
|
|
valueField: 'id',
|
|
|
|
labelField: 'nice_name',
|
|
|
|
searchField: ['nice_name', 'domain_names'],
|
|
|
|
create: false,
|
|
|
|
preload: true,
|
|
|
|
allowEmptyOption: true,
|
|
|
|
render: {
|
|
|
|
option: function (item) {
|
|
|
|
item.i18n = App.i18n;
|
|
|
|
item.formatDbDate = Helpers.formatDbDate;
|
|
|
|
return certListItemTemplate(item);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
load: function (query, callback) {
|
|
|
|
App.Api.Nginx.Certificates.getAll()
|
|
|
|
.then(rows => {
|
|
|
|
callback(rows);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onLoad: function () {
|
|
|
|
view.ui.certificate_select[0].selectize.setValue(view.model.get('certificate_id'));
|
|
|
|
}
|
|
|
|
});
|
2018-07-25 18:23:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
if (typeof options.model === 'undefined' || !options.model) {
|
|
|
|
this.model = new DeadHostModel.Model();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|