form.js 8.6 KB

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