1- const _ = require ( 'lodash' ) ;
2- const fs = require ( 'fs' ) ;
3- const batchflow = require ( 'batchflow' ) ;
4- const logger = require ( '../logger' ) . access ;
5- const error = require ( '../lib/error' ) ;
6- const accessListModel = require ( '../models/access_list' ) ;
7- const accessListAuthModel = require ( '../models/access_list_auth' ) ;
8- const proxyHostModel = require ( '../models/proxy_host' ) ;
9- const internalAuditLog = require ( './audit-log' ) ;
10- const internalNginx = require ( './nginx' ) ;
11- const utils = require ( '../lib/utils' ) ;
1+ const _ = require ( 'lodash' ) ;
2+ const fs = require ( 'fs' ) ;
3+ const batchflow = require ( 'batchflow' ) ;
4+ const logger = require ( '../logger' ) . access ;
5+ const error = require ( '../lib/error' ) ;
6+ const accessListModel = require ( '../models/access_list' ) ;
7+ const accessListAuthModel = require ( '../models/access_list_auth' ) ;
8+ const accessListClientModel = require ( '../models/access_list_client' ) ;
9+ const proxyHostModel = require ( '../models/proxy_host' ) ;
10+ const internalAuditLog = require ( './audit-log' ) ;
11+ const internalNginx = require ( './nginx' ) ;
12+ const utils = require ( '../lib/utils' ) ;
1213
1314function omissions ( ) {
1415 return [ 'is_deleted' ] ;
@@ -29,14 +30,16 @@ const internalAccessList = {
2930 . omit ( omissions ( ) )
3031 . insertAndFetch ( {
3132 name : data . name ,
33+ satify_any : data . satify_any ,
3234 owner_user_id : access . token . getUserId ( 1 )
3335 } ) ;
3436 } )
3537 . then ( ( row ) => {
3638 data . id = row . id ;
3739
38- // Now add the items
3940 let promises = [ ] ;
41+
42+ // Now add the items
4043 data . items . map ( ( item ) => {
4144 promises . push ( accessListAuthModel
4245 . query ( )
@@ -48,13 +51,27 @@ const internalAccessList = {
4851 ) ;
4952 } ) ;
5053
54+ // Now add the clients
55+ if ( typeof data . clients !== 'undefined' && data . clients ) {
56+ data . clients . map ( ( client ) => {
57+ promises . push ( accessListClientModel
58+ . query ( )
59+ . insert ( {
60+ access_list_id : row . id ,
61+ address : client . address ,
62+ directive : client . directive
63+ } )
64+ ) ;
65+ } ) ;
66+ }
67+
5168 return Promise . all ( promises ) ;
5269 } )
5370 . then ( ( ) => {
5471 // re-fetch with expansions
5572 return internalAccessList . get ( access , {
5673 id : data . id ,
57- expand : [ 'owner' , 'items' ]
74+ expand : [ 'owner' , 'items' , 'clients' , 'proxy_hosts.access_list.clients' ]
5875 } , true /* <- skip masking */ ) ;
5976 } )
6077 . then ( ( row ) => {
@@ -64,7 +81,7 @@ const internalAccessList = {
6481 return internalAccessList . build ( row )
6582 . then ( ( ) => {
6683 if ( row . proxy_host_count ) {
67- return internalNginx . reload ( ) ;
84+ return internalNginx . bulkGenerateConfigs ( 'proxy_host' , row . proxy_hosts ) ;
6885 }
6986 } )
7087 . then ( ( ) => {
@@ -109,7 +126,8 @@ const internalAccessList = {
109126 . query ( )
110127 . where ( { id : data . id } )
111128 . patch ( {
112- name : data . name
129+ name : data . name ,
130+ satify_any : data . satify_any ,
113131 } ) ;
114132 }
115133 } )
@@ -153,6 +171,38 @@ const internalAccessList = {
153171 } ) ;
154172 }
155173 } )
174+ . then ( ( ) => {
175+ // Check for clients and add/update/remove them
176+ if ( typeof data . clients !== 'undefined' && data . clients ) {
177+ let promises = [ ] ;
178+
179+ data . clients . map ( function ( client ) {
180+ if ( client . address ) {
181+ promises . push ( accessListClientModel
182+ . query ( )
183+ . insert ( {
184+ access_list_id : data . id ,
185+ address : client . address ,
186+ directive : client . directive
187+ } )
188+ ) ;
189+ }
190+ } ) ;
191+
192+ let query = accessListClientModel
193+ . query ( )
194+ . delete ( )
195+ . where ( 'access_list_id' , data . id ) ;
196+
197+ return query
198+ . then ( ( ) => {
199+ // Add new items
200+ if ( promises . length ) {
201+ return Promise . all ( promises ) ;
202+ }
203+ } ) ;
204+ }
205+ } )
156206 . then ( ( ) => {
157207 // Add to audit log
158208 return internalAuditLog . add ( access , {
@@ -166,14 +216,14 @@ const internalAccessList = {
166216 // re-fetch with expansions
167217 return internalAccessList . get ( access , {
168218 id : data . id ,
169- expand : [ 'owner' , 'items' ]
219+ expand : [ 'owner' , 'items' , 'clients' , 'proxy_hosts.access_list.clients' ]
170220 } , true /* <- skip masking */ ) ;
171221 } )
172222 . then ( ( row ) => {
173223 return internalAccessList . build ( row )
174224 . then ( ( ) => {
175225 if ( row . proxy_host_count ) {
176- return internalNginx . reload ( ) ;
226+ return internalNginx . bulkGenerateConfigs ( 'proxy_host' , row . proxy_hosts ) ;
177227 }
178228 } )
179229 . then ( ( ) => {
@@ -204,7 +254,7 @@ const internalAccessList = {
204254 . joinRaw ( 'LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0' )
205255 . where ( 'access_list.is_deleted' , 0 )
206256 . andWhere ( 'access_list.id' , data . id )
207- . allowEager ( '[owner,items,proxy_hosts]' )
257+ . allowEager ( '[owner,items,clients, proxy_hosts,proxy_hosts.access_list.clients ]' )
208258 . omit ( [ 'access_list.is_deleted' ] )
209259 . first ( ) ;
210260
@@ -246,7 +296,7 @@ const internalAccessList = {
246296 delete : ( access , data ) => {
247297 return access . can ( 'access_lists:delete' , data . id )
248298 . then ( ( ) => {
249- return internalAccessList . get ( access , { id : data . id , expand : [ 'proxy_hosts' , 'items' ] } ) ;
299+ return internalAccessList . get ( access , { id : data . id , expand : [ 'proxy_hosts' , 'items' , 'clients' ] } ) ;
250300 } )
251301 . then ( ( row ) => {
252302 if ( ! row ) {
@@ -330,7 +380,7 @@ const internalAccessList = {
330380 . where ( 'access_list.is_deleted' , 0 )
331381 . groupBy ( 'access_list.id' )
332382 . omit ( [ 'access_list.is_deleted' ] )
333- . allowEager ( '[owner,items]' )
383+ . allowEager ( '[owner,items,clients ]' )
334384 . orderBy ( 'access_list.name' , 'ASC' ) ;
335385
336386 if ( access_data . permission_visibility !== 'all' ) {
0 commit comments