form.js 7.8 KB

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