form.js 7.1 KB

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