@@ -90,6 +90,7 @@ module.exports = Mn.View.extend({
9090 }
9191
9292 let data = this . ui . form . serializeJSON ( ) ;
93+ data . id = this . model . get ( 'id' ) ;
9394 data . provider = this . model . get ( 'provider' ) ;
9495 let ssl_files = [ ] ;
9596
@@ -125,30 +126,38 @@ module.exports = Mn.View.extend({
125126 if ( typeof data . domain_names === 'string' && data . domain_names ) {
126127 data . domain_names = data . domain_names . split ( ',' ) ;
127128 }
128- } else if ( data . provider === 'other' && ! this . model . hasSslFiles ( ) ) {
129+ } else if ( data . provider === 'other' ) {
130+ const isNew = data . id == null ;
129131 // check files are attached
130- if ( ! this . ui . other_certificate [ 0 ] . files . length || ! this . ui . other_certificate [ 0 ] . files [ 0 ] . size ) {
132+ // Check Certificate
133+ const hasCertificateFile = this . ui . other_certificate [ 0 ] . files . length && this . ui . other_certificate [ 0 ] . files [ 0 ] . size
134+ if ( isNew && ! hasCertificateFile ) {
131135 alert ( 'Certificate file is not attached' ) ;
132136 return ;
133- } else {
137+ }
138+ if ( hasCertificateFile ) {
134139 if ( this . ui . other_certificate [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
135140 alert ( 'Certificate file is too large (> 100kb)' ) ;
136141 return ;
137142 }
138- ssl_files . push ( { name : 'certificate' , file : this . ui . other_certificate [ 0 ] . files [ 0 ] } ) ;
143+ ssl_files . push ( { name : 'certificate' , file : this . ui . other_certificate [ 0 ] . files [ 0 ] } ) ;
139144 }
140145
141- if ( ! this . ui . other_certificate_key [ 0 ] . files . length || ! this . ui . other_certificate_key [ 0 ] . files [ 0 ] . size ) {
146+ // Check Certificate Key
147+ const hasCertificateKeyFile = this . ui . other_certificate_key [ 0 ] . files . length && this . ui . other_certificate_key [ 0 ] . files [ 0 ] . size
148+ if ( isNew && ! hasCertificateKeyFile ) {
142149 alert ( 'Certificate key file is not attached' ) ;
143150 return ;
144- } else {
151+ }
152+ if ( hasCertificateKeyFile ) {
145153 if ( this . ui . other_certificate_key [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
146154 alert ( 'Certificate key file is too large (> 100kb)' ) ;
147155 return ;
148156 }
149- ssl_files . push ( { name : 'certificate_key' , file : this . ui . other_certificate_key [ 0 ] . files [ 0 ] } ) ;
157+ ssl_files . push ( { name : 'certificate_key' , file : this . ui . other_certificate_key [ 0 ] . files [ 0 ] } ) ;
150158 }
151159
160+ // Check Intermediate Certificate
152161 if ( this . ui . other_intermediate_certificate [ 0 ] . files . length && this . ui . other_intermediate_certificate [ 0 ] . files [ 0 ] . size ) {
153162 if ( this . ui . other_intermediate_certificate [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
154163 alert ( 'Intermediate Certificate file is too large (> 100kb)' ) ;
@@ -170,20 +179,23 @@ module.exports = Mn.View.extend({
170179 }
171180
172181 new Promise ( resolve => {
173- if ( data . provider === 'other' ) {
182+ if ( data . provider === 'other' && ssl_files . length ) {
174183 resolve ( App . Api . Nginx . Certificates . validate ( form_data ) ) ;
175184 } else {
176185 resolve ( ) ;
177186 }
178187 } )
179188 . then ( ( ) => {
180- return App . Api . Nginx . Certificates . create ( data ) ;
189+ return data . id == null
190+ ? App . Api . Nginx . Certificates . create ( data )
191+ : App . Api . Nginx . Certificates . update ( data ) ;
181192 } )
182193 . then ( result => {
183194 this . model . set ( result ) ;
184195
185196 // Now upload the certs if we need to
186- if ( data . provider === 'other' ) {
197+ const hasCertificateFiles = form_data . has ( 'certificate' ) || form_data . has ( 'certificate_key' ) || form_data . has ( 'intermediate_certificate' ) ;
198+ if ( data . provider === 'other' && hasCertificateFiles ) {
187199 return App . Api . Nginx . Certificates . upload ( this . model . get ( 'id' ) , form_data )
188200 . then ( result => {
189201 this . model . set ( 'meta' , _ . assign ( { } , this . model . get ( 'meta' ) , result ) ) ;
@@ -240,6 +252,9 @@ module.exports = Mn.View.extend({
240252 this . getUI ( ui ) . text ( e . target . files [ 0 ] . name )
241253 } ,
242254 templateContext : {
255+ isNew : function ( ) {
256+ return this . isNew ( ) ;
257+ } ,
243258 getLetsencryptEmail : function ( ) {
244259 return typeof this . meta . letsencrypt_email !== 'undefined' ? this . meta . letsencrypt_email : App . Cache . User . get ( 'email' ) ;
245260 } ,
0 commit comments