form.js 6.9 KB

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