11'use strict' ;
22
3- const _ = require ( 'lodash' ) ;
4- const error = require ( '../lib/error' ) ;
5- const accessListModel = require ( '../models/access_list' ) ;
3+ const _ = require ( 'lodash' ) ;
4+ const error = require ( '../lib/error' ) ;
5+ const accessListModel = require ( '../models/access_list' ) ;
6+ const accessListAuthModel = require ( '../models/access_list_auth' ) ;
7+ const internalAuditLog = require ( './audit-log' ) ;
68
79function omissions ( ) {
810 return [ 'is_deleted' ] ;
@@ -18,8 +20,51 @@ const internalAccessList = {
1820 create : ( access , data ) => {
1921 return access . can ( 'access_lists:create' , data )
2022 . then ( access_data => {
21- // TODO
22- return { } ;
23+ return accessListModel
24+ . query ( )
25+ . omit ( omissions ( ) )
26+ . insertAndFetch ( {
27+ name : data . name ,
28+ owner_user_id : access . token . get ( 'attrs' ) . id
29+ } ) ;
30+ } )
31+ . then ( row => {
32+ // Now add the items
33+ let promises = [ ] ;
34+ data . items . map ( function ( item ) {
35+ promises . push ( accessListAuthModel
36+ . query ( )
37+ . insert ( {
38+ access_list_id : row . id ,
39+ username : item . username ,
40+ password : item . password
41+ } )
42+ ) ;
43+ } ) ;
44+
45+ return Promise . all ( promises ) ;
46+ } )
47+ . then ( row => {
48+ // re-fetch with cert
49+ return internalAccessList . get ( access , {
50+ id : row . id ,
51+ expand : [ 'owner' , 'items' ]
52+ } ) ;
53+ } )
54+ . then ( row => {
55+ // Audit log
56+ data . meta = _ . assign ( { } , data . meta || { } , row . meta ) ;
57+
58+ // Add to audit log
59+ return internalAuditLog . add ( access , {
60+ action : 'created' ,
61+ object_type : 'access-list' ,
62+ object_id : row . id ,
63+ meta : data
64+ } )
65+ . then ( ( ) => {
66+ return row ;
67+ } ) ;
2368 } ) ;
2469 } ,
2570
@@ -62,7 +107,7 @@ const internalAccessList = {
62107 . query ( )
63108 . where ( 'is_deleted' , 0 )
64109 . andWhere ( 'id' , data . id )
65- . allowEager ( '[owner]' )
110+ . allowEager ( '[owner,items ]' )
66111 . first ( ) ;
67112
68113 if ( access_data . permission_visibility !== 'all' ) {
@@ -82,6 +127,10 @@ const internalAccessList = {
82127 } )
83128 . then ( row => {
84129 if ( row ) {
130+ if ( typeof row . items !== 'undefined' && row . items ) {
131+ row . items = internalAccessList . maskItems ( row . items ) ;
132+ }
133+
85134 return _ . omit ( row , omissions ( ) ) ;
86135 } else {
87136 throw new error . ItemNotFoundError ( data . id ) ;
@@ -134,7 +183,7 @@ const internalAccessList = {
134183 . where ( 'is_deleted' , 0 )
135184 . groupBy ( 'id' )
136185 . omit ( [ 'is_deleted' ] )
137- . allowEager ( '[owner]' )
186+ . allowEager ( '[owner,items ]' )
138187 . orderBy ( 'name' , 'ASC' ) ;
139188
140189 if ( access_data . permission_visibility !== 'all' ) {
@@ -153,6 +202,17 @@ const internalAccessList = {
153202 }
154203
155204 return query ;
205+ } )
206+ . then ( rows => {
207+ if ( rows ) {
208+ rows . map ( function ( row , idx ) {
209+ if ( typeof row . items !== 'undefined' && row . items ) {
210+ rows [ idx ] . items = internalAccessList . maskItems ( row . items ) ;
211+ }
212+ } ) ;
213+ }
214+
215+ return rows ;
156216 } ) ;
157217 } ,
158218
@@ -177,6 +237,21 @@ const internalAccessList = {
177237 . then ( row => {
178238 return parseInt ( row . count , 10 ) ;
179239 } ) ;
240+ } ,
241+
242+ /**
243+ * @param {Object } list
244+ * @returns {Object }
245+ */
246+ maskItems : list => {
247+ if ( list && typeof list . items !== 'undefined' ) {
248+ list . items . map ( function ( val , idx ) {
249+ list . items [ idx ] . hint = val . password . charAt ( 0 ) + ( '*' ) . repeat ( val . password . length - 1 ) ;
250+ list . items [ idx ] . password = '' ;
251+ } ) ;
252+ }
253+
254+ return list ;
180255 }
181256} ;
182257
0 commit comments