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' ) ;
12
13
13
14
function omissions ( ) {
14
15
return [ 'is_deleted' ] ;
@@ -29,14 +30,16 @@ const internalAccessList = {
29
30
. omit ( omissions ( ) )
30
31
. insertAndFetch ( {
31
32
name : data . name ,
33
+ satify_any : data . satify_any ,
32
34
owner_user_id : access . token . getUserId ( 1 )
33
35
} ) ;
34
36
} )
35
37
. then ( ( row ) => {
36
38
data . id = row . id ;
37
39
38
- // Now add the items
39
40
let promises = [ ] ;
41
+
42
+ // Now add the items
40
43
data . items . map ( ( item ) => {
41
44
promises . push ( accessListAuthModel
42
45
. query ( )
@@ -48,13 +51,27 @@ const internalAccessList = {
48
51
) ;
49
52
} ) ;
50
53
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
+
51
68
return Promise . all ( promises ) ;
52
69
} )
53
70
. then ( ( ) => {
54
71
// re-fetch with expansions
55
72
return internalAccessList . get ( access , {
56
73
id : data . id ,
57
- expand : [ 'owner' , 'items' ]
74
+ expand : [ 'owner' , 'items' , 'clients' , 'proxy_hosts.access_list.clients' ]
58
75
} , true /* <- skip masking */ ) ;
59
76
} )
60
77
. then ( ( row ) => {
@@ -64,7 +81,7 @@ const internalAccessList = {
64
81
return internalAccessList . build ( row )
65
82
. then ( ( ) => {
66
83
if ( row . proxy_host_count ) {
67
- return internalNginx . reload ( ) ;
84
+ return internalNginx . bulkGenerateConfigs ( 'proxy_host' , row . proxy_hosts ) ;
68
85
}
69
86
} )
70
87
. then ( ( ) => {
@@ -109,7 +126,8 @@ const internalAccessList = {
109
126
. query ( )
110
127
. where ( { id : data . id } )
111
128
. patch ( {
112
- name : data . name
129
+ name : data . name ,
130
+ satify_any : data . satify_any ,
113
131
} ) ;
114
132
}
115
133
} )
@@ -153,6 +171,38 @@ const internalAccessList = {
153
171
} ) ;
154
172
}
155
173
} )
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
+ } )
156
206
. then ( ( ) => {
157
207
// Add to audit log
158
208
return internalAuditLog . add ( access , {
@@ -166,14 +216,14 @@ const internalAccessList = {
166
216
// re-fetch with expansions
167
217
return internalAccessList . get ( access , {
168
218
id : data . id ,
169
- expand : [ 'owner' , 'items' ]
219
+ expand : [ 'owner' , 'items' , 'clients' , 'proxy_hosts.access_list.clients' ]
170
220
} , true /* <- skip masking */ ) ;
171
221
} )
172
222
. then ( ( row ) => {
173
223
return internalAccessList . build ( row )
174
224
. then ( ( ) => {
175
225
if ( row . proxy_host_count ) {
176
- return internalNginx . reload ( ) ;
226
+ return internalNginx . bulkGenerateConfigs ( 'proxy_host' , row . proxy_hosts ) ;
177
227
}
178
228
} )
179
229
. then ( ( ) => {
@@ -204,7 +254,7 @@ const internalAccessList = {
204
254
. joinRaw ( 'LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0' )
205
255
. where ( 'access_list.is_deleted' , 0 )
206
256
. andWhere ( 'access_list.id' , data . id )
207
- . allowEager ( '[owner,items,proxy_hosts]' )
257
+ . allowEager ( '[owner,items,clients, proxy_hosts,proxy_hosts.access_list.clients ]' )
208
258
. omit ( [ 'access_list.is_deleted' ] )
209
259
. first ( ) ;
210
260
@@ -246,7 +296,7 @@ const internalAccessList = {
246
296
delete : ( access , data ) => {
247
297
return access . can ( 'access_lists:delete' , data . id )
248
298
. 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' ] } ) ;
250
300
} )
251
301
. then ( ( row ) => {
252
302
if ( ! row ) {
@@ -330,7 +380,7 @@ const internalAccessList = {
330
380
. where ( 'access_list.is_deleted' , 0 )
331
381
. groupBy ( 'access_list.id' )
332
382
. omit ( [ 'access_list.is_deleted' ] )
333
- . allowEager ( '[owner,items]' )
383
+ . allowEager ( '[owner,items,clients ]' )
334
384
. orderBy ( 'access_list.name' , 'ASC' ) ;
335
385
336
386
if ( access_data . permission_visibility !== 'all' ) {
0 commit comments