form.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const _ = require('underscore');
  2. const Mn = require('backbone.marionette');
  3. const App = require('../../main');
  4. const CertificateModel = require('../../../models/certificate');
  5. const template = require('./form.ejs');
  6. require('jquery-serializejson');
  7. require('selectize');
  8. module.exports = Mn.View.extend({
  9. template: template,
  10. className: 'modal-dialog',
  11. max_file_size: 102400,
  12. ui: {
  13. form: 'form',
  14. domain_names: 'input[name="domain_names"]',
  15. buttons: '.modal-footer button',
  16. cancel: 'button.cancel',
  17. save: 'button.save',
  18. other_certificate: '#other_certificate',
  19. other_certificate_key: '#other_certificate_key',
  20. other_intermediate_certificate: '#other_intermediate_certificate'
  21. },
  22. events: {
  23. 'click @ui.save': function (e) {
  24. e.preventDefault();
  25. if (!this.ui.form[0].checkValidity()) {
  26. $('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
  27. return;
  28. }
  29. let view = this;
  30. let data = this.ui.form.serializeJSON();
  31. data.provider = this.model.get('provider');
  32. // Manipulate
  33. if (typeof data.meta !== 'undefined' && typeof data.meta.letsencrypt_agree !== 'undefined') {
  34. data.meta.letsencrypt_agree = !!data.meta.letsencrypt_agree;
  35. }
  36. if (typeof data.domain_names === 'string' && data.domain_names) {
  37. data.domain_names = data.domain_names.split(',');
  38. }
  39. let ssl_files = [];
  40. // check files are attached
  41. if (this.model.get('provider') === 'other' && !this.model.hasSslFiles()) {
  42. if (!this.ui.other_certificate[0].files.length || !this.ui.other_certificate[0].files[0].size) {
  43. alert('Certificate file is not attached');
  44. return;
  45. } else {
  46. if (this.ui.other_certificate[0].files[0].size > this.max_file_size) {
  47. alert('Certificate file is too large (> 100kb)');
  48. return;
  49. }
  50. ssl_files.push({name: 'certificate', file: this.ui.other_certificate[0].files[0]});
  51. }
  52. if (!this.ui.other_certificate_key[0].files.length || !this.ui.other_certificate_key[0].files[0].size) {
  53. alert('Certificate key file is not attached');
  54. return;
  55. } else {
  56. if (this.ui.other_certificate_key[0].files[0].size > this.max_file_size) {
  57. alert('Certificate key file is too large (> 100kb)');
  58. return;
  59. }
  60. ssl_files.push({name: 'certificate_key', file: this.ui.other_certificate_key[0].files[0]});
  61. }
  62. if (this.ui.other_intermediate_certificate[0].files.length && this.ui.other_intermediate_certificate[0].files[0].size) {
  63. if (this.ui.other_intermediate_certificate[0].files[0].size > this.max_file_size) {
  64. alert('Intermediate Certificate file is too large (> 100kb)');
  65. return;
  66. }
  67. ssl_files.push({name: 'intermediate_certificate', file: this.ui.other_intermediate_certificate[0].files[0]});
  68. }
  69. }
  70. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  71. // compile file data
  72. let form_data = new FormData();
  73. if (view.model.get('provider') && ssl_files.length) {
  74. ssl_files.map(function (file) {
  75. form_data.append(file.name, file.file);
  76. });
  77. }
  78. new Promise(resolve => {
  79. if (view.model.get('provider') === 'other') {
  80. resolve(App.Api.Nginx.Certificates.validate(form_data));
  81. } else {
  82. resolve();
  83. }
  84. })
  85. .then(() => {
  86. return App.Api.Nginx.Certificates.create(data);
  87. })
  88. .then(result => {
  89. view.model.set(result);
  90. // Now upload the certs if we need to
  91. if (view.model.get('provider') === 'other') {
  92. return App.Api.Nginx.Certificates.upload(view.model.get('id'), form_data)
  93. .then(result => {
  94. view.model.set('meta', _.assign({}, view.model.get('meta'), result));
  95. });
  96. }
  97. })
  98. .then(() => {
  99. App.UI.closeModal(function () {
  100. App.Controller.showNginxCertificates();
  101. });
  102. })
  103. .catch(err => {
  104. alert(err.message);
  105. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  106. });
  107. }
  108. },
  109. templateContext: {
  110. getLetsencryptEmail: function () {
  111. return typeof this.meta.letsencrypt_email !== 'undefined' ? this.meta.letsencrypt_email : App.Cache.User.get('email');
  112. },
  113. getLetsencryptAgree: function () {
  114. return typeof this.meta.letsencrypt_agree !== 'undefined' ? this.meta.letsencrypt_agree : false;
  115. }
  116. },
  117. onRender: function () {
  118. this.ui.domain_names.selectize({
  119. delimiter: ',',
  120. persist: false,
  121. maxOptions: 15,
  122. create: function (input) {
  123. return {
  124. value: input,
  125. text: input
  126. };
  127. },
  128. createFilter: /^(?:[^.*]+\.?)+[^.]$/
  129. });
  130. },
  131. initialize: function (options) {
  132. if (typeof options.model === 'undefined' || !options.model) {
  133. this.model = new CertificateModel.Model({provider: 'letsencrypt'});
  134. }
  135. }
  136. });