@@ -299,83 +299,54 @@ upgradeFuncs.push({
299
299
300
300
upgradeFuncs . push ( {
301
301
description : 'Create default roles with permissions and update user groups' ,
302
- func ( ) {
303
- return new Promise ( async ( resolve , reject ) => {
304
- try {
305
- // Fetch channels and get the role names with their associated channels
306
- const channels = await ChannelModel . find ( )
307
- const existingRoles = JSON . parse ( JSON . stringify ( roles ) ) // Deep clone the roles object
308
-
309
- channels . forEach ( channel => {
310
- if ( Array . isArray ( channel . allow ) ) {
311
- if ( channel . txViewAcl && channel . txViewAcl . length > 0 ) {
312
- channel . txViewAcl . forEach ( role => {
313
- if ( ! existingRoles [ role ] ) {
314
- existingRoles [ role ] = { permissions : { } }
315
- }
316
- if ( ! existingRoles [ role ] . permissions [ 'transaction-view-specified' ] ) {
317
- existingRoles [ role ] . permissions [ 'transaction-view-specified' ] = [ ]
318
- }
319
- existingRoles [ role ] . permissions [ 'transaction-view-specified' ] . push ( channel . name )
320
- if ( ! existingRoles [ role ] . permissions [ 'channel-view-specified' ] ) {
321
- existingRoles [ role ] . permissions [ 'channel-view-specified' ] = [ ]
322
- }
323
- existingRoles [ role ] . permissions [ 'channel-view-specified' ] . push ( channel . name )
324
- existingRoles [ role ] . permissions [ 'client-view-all' ] = true
325
- } )
326
- }
327
- if ( channel . txRerunAcl && channel . txRerunAcl . length > 0 ) {
328
- channel . txRerunAcl . forEach ( role => {
329
- if ( ! existingRoles [ role ] ) {
330
- existingRoles [ role ] = { permissions : { } }
331
- }
332
- if ( ! existingRoles [ role ] . permissions [ 'transaction-rerun-specified' ] ) {
333
- existingRoles [ role ] . permissions [ 'transaction-rerun-specified' ] = [ ]
334
- }
335
- existingRoles [ role ] . permissions [ 'transaction-rerun-specified' ] . push ( channel . name )
336
- if ( ! existingRoles [ role ] . permissions [ 'channel-view-specified' ] ) {
337
- existingRoles [ role ] . permissions [ 'channel-view-specified' ] = [ ]
338
- }
339
- existingRoles [ role ] . permissions [ 'channel-view-specified' ] . push ( channel . name )
340
- existingRoles [ role ] . permissions [ 'client-view-all' ] = true
341
- } )
342
- }
343
- if ( channel . txViewFullAcl && channel . txViewFullAcl . length > 0 ) {
344
- channel . txViewFullAcl . forEach ( role => {
345
- if ( ! existingRoles [ role ] ) {
346
- existingRoles [ role ] = { permissions : { } }
347
- }
348
- if ( ! existingRoles [ role ] . permissions [ 'transaction-view-body-specified' ] ) {
349
- existingRoles [ role ] . permissions [ 'transaction-view-body-specified' ] = [ ]
350
- }
351
- existingRoles [ role ] . permissions [ 'transaction-view-body-specified' ] . push ( channel . name )
352
- if ( ! existingRoles [ role ] . permissions [ 'channel-view-specified' ] ) {
353
- existingRoles [ role ] . permissions [ 'channel-view-specified' ] = [ ]
354
- }
355
- existingRoles [ role ] . permissions [ 'channel-view-specified' ] . push ( channel . name )
356
- existingRoles [ role ] . permissions [ 'client-view-all' ] = true
357
- } )
358
- }
359
- }
360
- } )
302
+ func : async ( ) => {
303
+ try {
304
+ const channels = await ChannelModel . find ( )
305
+ const existingRoles = JSON . parse ( JSON . stringify ( roles ) ) // Deep clone the roles object
306
+
307
+ const updateRolePermissions = ( role , permissionType , channelName ) => {
308
+ if ( ! existingRoles [ role ] ) {
309
+ existingRoles [ role ] = { permissions : { } }
310
+ }
311
+ if ( ! existingRoles [ role ] . permissions [ permissionType ] ) {
312
+ existingRoles [ role ] . permissions [ permissionType ] = [ ]
313
+ }
314
+ existingRoles [ role ] . permissions [ permissionType ] . push ( channelName )
315
+ existingRoles [ role ] . permissions [ 'channel-view-specified' ] = existingRoles [ role ] . permissions [ 'channel-view-specified' ] || [ ]
316
+ existingRoles [ role ] . permissions [ 'channel-view-specified' ] . push ( channelName )
317
+ existingRoles [ role ] . permissions [ 'client-view-all' ] = true
318
+ }
361
319
362
- // Create or update roles
363
- for ( const [ roleName , roleData ] of Object . entries ( existingRoles ) ) {
364
- await RoleModel . findOneAndUpdate (
365
- { name : roleName } ,
366
- { name : roleName , permissions : roleData . permissions } ,
367
- { upsert : true , new : true }
368
- )
369
- logger . info ( `Role ${ roleName } created or updated with permissions` )
320
+ channels . forEach ( channel => {
321
+ if ( Array . isArray ( channel . allow ) ) {
322
+ const aclTypes = [
323
+ { acl : channel . txViewAcl , permissionType : 'transaction-view-specified' } ,
324
+ { acl : channel . txRerunAcl , permissionType : 'transaction-rerun-specified' } ,
325
+ { acl : channel . txViewFullAcl , permissionType : 'transaction-view-body-specified' }
326
+ ]
327
+
328
+ aclTypes . forEach ( ( { acl, permissionType } ) => {
329
+ if ( acl && acl . length > 0 ) {
330
+ acl . forEach ( role => updateRolePermissions ( role , permissionType , channel . name ) )
331
+ }
332
+ } )
370
333
}
334
+ } )
371
335
372
- logger . info ( 'Successfully updated roles' )
373
- resolve ( )
374
- } catch ( err ) {
375
- logger . error ( `Error updating roles: ${ err } ` )
376
- reject ( err )
377
- }
378
- } )
336
+ // Create or update roles
337
+ await Promise . all ( Object . entries ( existingRoles ) . map ( ( [ roleName , roleData ] ) =>
338
+ RoleModel . findOneAndUpdate (
339
+ { name : roleName } ,
340
+ { name : roleName , permissions : roleData . permissions } ,
341
+ { upsert : true , new : true }
342
+ )
343
+ ) )
344
+
345
+ logger . info ( 'Successfully updated roles' )
346
+ } catch ( err ) {
347
+ logger . error ( `Error updating roles: ${ err } ` )
348
+ throw err
349
+ }
379
350
}
380
351
} )
381
352
@@ -389,7 +360,7 @@ async function upgradeDbInternal() {
389
360
const dbVer =
390
361
( await DbVersionModel . findOne ( ) ) ||
391
362
new DbVersionModel ( { version : 0 , lastUpdated : new Date ( ) } )
392
- const upgradeFuncsToRun = upgradeFuncs . slice ( dbVer . version )
363
+ const upgradeFuncsToRun = upgradeFuncs . slice ( dbVer . version )
393
364
for ( const upgradeFunc of upgradeFuncsToRun ) {
394
365
await upgradeFunc . func ( )
395
366
dbVer . version ++
0 commit comments