form.js 6.6 KB

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