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' ] ;
@@ -35,8 +36,9 @@ const internalAccessList = {
3536 . then ( ( row ) => {
3637 data . id = row . id ;
3738
38- // Now add the items
3939 let promises = [ ] ;
40+
41+ // Now add the items
4042 data . items . map ( ( item ) => {
4143 promises . push ( accessListAuthModel
4244 . query ( )
@@ -48,13 +50,27 @@ const internalAccessList = {
4850 ) ;
4951 } ) ;
5052
53+ // Now add the clients
54+ if ( typeof data . clients !== 'undefined' && data . clients ) {
55+ data . clients . map ( ( client ) => {
56+ promises . push ( accessListClientModel
57+ . query ( )
58+ . insert ( {
59+ access_list_id : row . id ,
60+ address : client . address ,
61+ directive : client . directive
62+ } )
63+ ) ;
64+ } ) ;
65+ }
66+
5167 return Promise . all ( promises ) ;
5268 } )
5369 . then ( ( ) => {
5470 // re-fetch with expansions
5571 return internalAccessList . get ( access , {
5672 id : data . id ,
57- expand : [ 'owner' , 'items' ]
73+ expand : [ 'owner' , 'items' , 'clients' ]
5874 } , true /* <- skip masking */ ) ;
5975 } )
6076 . then ( ( row ) => {
@@ -152,6 +168,37 @@ const internalAccessList = {
152168 }
153169 } ) ;
154170 }
171+
172+ // Check for clients and add/update/remove them
173+ if ( typeof data . clients !== 'undefined' && data . clients ) {
174+ let promises = [ ] ;
175+
176+ data . clients . map ( function ( client ) {
177+ if ( client . address ) {
178+ promises . push ( accessListAuthModel
179+ . query ( )
180+ . insert ( {
181+ access_list_id : data . id ,
182+ address : client . address ,
183+ directive : client . directive
184+ } )
185+ ) ;
186+ }
187+ } ) ;
188+
189+ let query = accessListClientModel
190+ . query ( )
191+ . delete ( )
192+ . where ( 'access_list_id' , data . id ) ;
193+
194+ return query
195+ . then ( ( ) => {
196+ // Add new items
197+ if ( promises . length ) {
198+ return Promise . all ( promises ) ;
199+ }
200+ } ) ;
201+ }
155202 } )
156203 . then ( ( ) => {
157204 // Add to audit log
@@ -166,7 +213,7 @@ const internalAccessList = {
166213 // re-fetch with expansions
167214 return internalAccessList . get ( access , {
168215 id : data . id ,
169- expand : [ 'owner' , 'items' ]
216+ expand : [ 'owner' , 'items' , 'clients' ]
170217 } , true /* <- skip masking */ ) ;
171218 } )
172219 . then ( ( row ) => {
@@ -204,7 +251,7 @@ const internalAccessList = {
204251 . joinRaw ( 'LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0' )
205252 . where ( 'access_list.is_deleted' , 0 )
206253 . andWhere ( 'access_list.id' , data . id )
207- . allowEager ( '[owner,items,proxy_hosts]' )
254+ . allowEager ( '[owner,items,clients, proxy_hosts]' )
208255 . omit ( [ 'access_list.is_deleted' ] )
209256 . first ( ) ;
210257
@@ -246,7 +293,7 @@ const internalAccessList = {
246293 delete : ( access , data ) => {
247294 return access . can ( 'access_lists:delete' , data . id )
248295 . then ( ( ) => {
249- return internalAccessList . get ( access , { id : data . id , expand : [ 'proxy_hosts' , 'items' ] } ) ;
296+ return internalAccessList . get ( access , { id : data . id , expand : [ 'proxy_hosts' , 'items' , 'clients' ] } ) ;
250297 } )
251298 . then ( ( row ) => {
252299 if ( ! row ) {
@@ -330,7 +377,7 @@ const internalAccessList = {
330377 . where ( 'access_list.is_deleted' , 0 )
331378 . groupBy ( 'access_list.id' )
332379 . omit ( [ 'access_list.is_deleted' ] )
333- . allowEager ( '[owner,items]' )
380+ . allowEager ( '[owner,items,clients ]' )
334381 . orderBy ( 'access_list.name' , 'ASC' ) ;
335382
336383 if ( access_data . permission_visibility !== 'all' ) {
0 commit comments