form.js 6.8 KB

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