11'use strict' ;
22
3- const _ = require ( 'lodash' ) ;
4- const error = require ( '../lib/error' ) ;
5- const proxyHostModel = require ( '../models/proxy_host' ) ;
6- const internalHost = require ( './host' ) ;
7- const internalNginx = require ( './nginx' ) ;
8- const internalAuditLog = require ( './audit-log' ) ;
3+ const _ = require ( 'lodash' ) ;
4+ const error = require ( '../lib/error' ) ;
5+ const proxyHostModel = require ( '../models/proxy_host' ) ;
6+ const internalHost = require ( './host' ) ;
7+ const internalNginx = require ( './nginx' ) ;
8+ const internalAuditLog = require ( './audit-log' ) ;
9+ const internalCertificate = require ( './certificate' ) ;
910
1011function omissions ( ) {
1112 return [ 'is_deleted' ] ;
@@ -19,6 +20,12 @@ const internalProxyHost = {
1920 * @returns {Promise }
2021 */
2122 create : ( access , data ) => {
23+ let create_certificate = data . certificate_id === 'new' ;
24+
25+ if ( create_certificate ) {
26+ delete data . certificate_id ;
27+ }
28+
2229 return access . can ( 'proxy_hosts:create' , data )
2330 . then ( access_data => {
2431 // Get a list of the domain names and check each of them against existing records
@@ -46,14 +53,39 @@ const internalProxyHost = {
4653 . omit ( omissions ( ) )
4754 . insertAndFetch ( data ) ;
4855 } )
56+ . then ( row => {
57+ if ( create_certificate ) {
58+ return internalCertificate . createQuickCertificate ( access , data )
59+ . then ( cert => {
60+ // update host with cert id
61+ return internalProxyHost . update ( access , {
62+ id : row . id ,
63+ certificate_id : cert . id
64+ } ) ;
65+ } )
66+ . then ( ( ) => {
67+ return row ;
68+ } ) ;
69+ } else {
70+ return row ;
71+ }
72+ } )
73+ . then ( row => {
74+ // re-fetch with cert
75+ return internalProxyHost . get ( access , {
76+ id : row . id ,
77+ expand : [ 'certificate' , 'owner' ]
78+ } ) ;
79+ } )
4980 . then ( row => {
5081 // Configure nginx
5182 return internalNginx . configure ( proxyHostModel , 'proxy_host' , row )
5283 . then ( ( ) => {
53- return internalProxyHost . get ( access , { id : row . id , expand : [ 'owner' ] } ) ;
84+ return row ;
5485 } ) ;
5586 } )
5687 . then ( row => {
88+ // Audit log
5789 data . meta = _ . assign ( { } , data . meta || { } , row . meta ) ;
5890
5991 // Add to audit log
@@ -78,6 +110,12 @@ const internalProxyHost = {
78110 * @return {Promise }
79111 */
80112 update : ( access , data ) => {
113+ let create_certificate = data . certificate_id === 'new' ;
114+
115+ if ( create_certificate ) {
116+ delete data . certificate_id ;
117+ }
118+
81119 return access . can ( 'proxy_hosts:update' , data . id )
82120 . then ( access_data => {
83121 // Get a list of the domain names and check each of them against existing records
@@ -107,13 +145,28 @@ const internalProxyHost = {
107145 throw new error . InternalValidationError ( 'Proxy Host could not be updated, IDs do not match: ' + row . id + ' !== ' + data . id ) ;
108146 }
109147
148+ if ( create_certificate ) {
149+ return internalCertificate . createQuickCertificate ( access , {
150+ domain_names : data . domain_names || row . domain_names ,
151+ meta : _ . assign ( { } , row . meta , data . meta )
152+ } )
153+ . then ( cert => {
154+ // update host with cert id
155+ data . certificate_id = cert . id ;
156+ } )
157+ . then ( ( ) => {
158+ return row ;
159+ } ) ;
160+ } else {
161+ return row ;
162+ }
163+ } )
164+ . then ( row => {
110165 return proxyHostModel
111166 . query ( )
112- . omit ( omissions ( ) )
113- . patchAndFetchById ( row . id , data )
167+ . where ( { id : data . id } )
168+ . patch ( data )
114169 . then ( saved_row => {
115- saved_row . meta = internalHost . cleanMeta ( saved_row . meta ) ;
116-
117170 // Add to audit log
118171 return internalAuditLog . add ( access , {
119172 action : 'updated' ,
@@ -125,6 +178,19 @@ const internalProxyHost = {
125178 return _ . omit ( saved_row , omissions ( ) ) ;
126179 } ) ;
127180 } ) ;
181+ } )
182+ . then ( ( ) => {
183+ return internalProxyHost . get ( access , {
184+ id : data . id ,
185+ expand : [ 'owner' , 'certificate' ]
186+ } )
187+ . then ( row => {
188+ // Configure nginx
189+ return internalNginx . configure ( proxyHostModel , 'proxy_host' , row )
190+ . then ( ( ) => {
191+ return _ . omit ( row , omissions ( ) ) ;
192+ } ) ;
193+ } )
128194 } ) ;
129195 } ,
130196
@@ -167,7 +233,6 @@ const internalProxyHost = {
167233 } )
168234 . then ( row => {
169235 if ( row ) {
170- row . meta = internalHost . cleanMeta ( row . meta ) ;
171236 return _ . omit ( row , omissions ( ) ) ;
172237 } else {
173238 throw new error . ItemNotFoundError ( data . id ) ;
@@ -207,8 +272,6 @@ const internalProxyHost = {
207272 } )
208273 . then ( ( ) => {
209274 // Add to audit log
210- row . meta = internalHost . cleanMeta ( row . meta ) ;
211-
212275 return internalAuditLog . add ( access , {
213276 action : 'deleted' ,
214277 object_type : 'proxy-host' ,
@@ -257,13 +320,6 @@ const internalProxyHost = {
257320 }
258321
259322 return query ;
260- } )
261- . then ( rows => {
262- rows . map ( row => {
263- row . meta = internalHost . cleanMeta ( row . meta ) ;
264- } ) ;
265-
266- return rows ;
267323 } ) ;
268324 } ,
269325
0 commit comments