@@ -33,43 +33,44 @@ const controller = {
3333 */
3434 async syncBucketRecursive ( req , res , next ) {
3535 try {
36- // Wrap all sql operations in a single transaction
37- const response = await utils . trxWrapper ( async ( trx ) => {
36+ // current userId
37+ const userId = await userService . getCurrentUserId (
38+ getCurrentIdentity ( req . currentUser , SYSTEM_USER ) ,
39+ SYSTEM_USER
40+ ) ;
41+ // parent bucket
42+ const bucketId = addDashesToUuid ( req . params . bucketId ) ;
43+ const parentBucket = await bucketService . read ( bucketId ) ;
3844
39- // curren userId
40- const userId = await userService . getCurrentUserId (
41- getCurrentIdentity ( req . currentUser , SYSTEM_USER ) ,
42- SYSTEM_USER
43- ) ;
44- // parent bucket
45- const bucketId = addDashesToUuid ( req . params . bucketId ) ;
46- const parentBucket = await bucketService . read ( bucketId ) ;
45+ // current user's permissions on parent bucket (folder)
46+ const currentUserParentBucketPerms = userId !== SYSTEM_USER ? ( await bucketPermissionService . searchPermissions ( {
47+ bucketId : parentBucket . bucketId ,
48+ userId : userId
49+ } ) ) . map ( p => p . permCode ) : [ ] ;
4750
48- // current user's permissions on parent bucket (folder)
49- const currentUserParentBucketPerms = userId !== SYSTEM_USER ? ( await bucketPermissionService . searchPermissions ( {
50- bucketId : parentBucket . bucketId ,
51- userId : userId
52- } ) ) . map ( p => p . permCode ) : [ ] ;
51+ /**
52+ * sync (ie create or delete) bucket records in COMS db to match 'folders' (S3 key prefixes) that exist in S3
53+ */
54+ // parent + child bucket records already in COMS db
55+ const dbChildBuckets = await bucketService . searchChildBuckets ( parentBucket , false , userId ) ;
56+ let dbBuckets = [ parentBucket ] . concat ( dbChildBuckets ) ;
57+ // 'folders' that exist below (and including) the parent 'folder' in S3
58+ const s3Response = await storageService . listAllObjectVersions ( { bucketId : bucketId , precisePath : false } ) ;
59+ const s3Keys = [ ...new Set ( [
60+ ...s3Response . DeleteMarkers . map ( object => formatS3KeyForCompare ( object . Key ) ) ,
61+ ...s3Response . Versions . map ( object => formatS3KeyForCompare ( object . Key ) ) ,
62+ ] ) ] ;
5363
54- /**
55- * sync (ie create or delete) bucket records in COMS db to match 'folders' (S3 key prefixes) that exist in S3
56- */
57- // parent + child bucket records already in COMS db
58- const dbChildBuckets = await bucketService . searchChildBuckets ( parentBucket ) ;
59- let dbBuckets = [ parentBucket ] . concat ( dbChildBuckets ) ;
60- // 'folders' that exist below (and including) the parent 'folder' in S3
61- const s3Response = await storageService . listAllObjectVersions ( { bucketId : bucketId , precisePath : false } ) ;
62- const s3Keys = [ ...new Set ( [
63- ...s3Response . DeleteMarkers . map ( object => formatS3KeyForCompare ( object . Key ) ) ,
64- ...s3Response . Versions . map ( object => formatS3KeyForCompare ( object . Key ) ) ,
65- ] ) ] ;
64+ // Wrap sync sql operations in a single transaction
65+ const response = await utils . trxWrapper ( async ( trx ) => {
6666
6767 const syncedBuckets = await this . syncBucketRecords (
6868 dbBuckets ,
6969 s3Keys ,
7070 parentBucket ,
7171 // assign current user's permissions on parent bucket to new sub-folders (buckets)
7272 currentUserParentBucketPerms ,
73+ userId ,
7374 trx
7475 ) ;
7576
@@ -115,14 +116,16 @@ const controller = {
115116 /**
116117 * @function syncBucketRecords
117118 * Synchronizes (creates / prunes) COMS db bucket records for each 'directry' found in S3
119+ * Adds current user's permissions to all buckets
118120 * @param {object[] } Array of Bucket models - bucket records already in COMS db before syncing
119121 * @param {string[] } s3Keys Array of key prefixes from S3 representing 'directories'
120122 * @param {object } Bucket model for the COMS db bucket record of parent bucket
121123 * @param {string[] } currentUserParentBucketPerms Array of PermCodes to add to NEW buckets
122- * @param {object } [trx] An Objection Transaction object
124+ * @param {string } userId the guid of current user
125+ * @param {object } [trx] An Objection Transaction object
123126 * @returns {string[] } And array of bucketId's for bucket records in COMS db
124127 */
125- async syncBucketRecords ( dbBuckets , s3Keys , parentBucket , currentUserParentBucketPerms , trx ) {
128+ async syncBucketRecords ( dbBuckets , s3Keys , parentBucket , currentUserParentBucketPerms , userId , trx ) {
126129 try {
127130 // delete buckets not found in S3 from COMS db
128131 const oldDbBuckets = dbBuckets . filter ( b => ! s3Keys . includes ( b . key ) ) ;
@@ -134,6 +137,17 @@ const controller = {
134137 } )
135138 )
136139 ) ;
140+ // add current user's permissions to all buckets
141+ await Promise . all (
142+ dbBuckets . map ( bucket => {
143+ return bucketPermissionService . addPermissions (
144+ bucket . bucketId ,
145+ currentUserParentBucketPerms . map ( permCode => ( { userId, permCode } ) ) ,
146+ undefined ,
147+ trx
148+ ) ;
149+ } )
150+ ) ;
137151
138152 // Create buckets only found in S3 in COMS db
139153 const newS3Keys = s3Keys . filter ( k => ! dbBuckets . map ( b => b . key ) . includes ( k ) ) ;
@@ -149,8 +163,6 @@ const controller = {
149163 region : parentBucket . region ?? undefined ,
150164 active : parentBucket . active ,
151165 userId : parentBucket . createdBy ?? SYSTEM_USER ,
152- // current user has MANAGE perm on parent folder (see route.hasPermission)
153- // ..so copy all their perms to NEW subfolders
154166 permCodes : currentUserParentBucketPerms
155167 } ;
156168 return bucketService . create ( data , trx )
@@ -159,7 +171,6 @@ const controller = {
159171 } ) ;
160172 } )
161173 ) ;
162-
163174 return dbBuckets ;
164175 }
165176 catch ( err ) {
0 commit comments