@@ -27,7 +27,10 @@ import { MinioService } from 'omniboxd/minio/minio.service';
2727import { WizardTaskService } from 'omniboxd/tasks/wizard-task.service' ;
2828import { PermissionsService } from 'omniboxd/permissions/permissions.service' ;
2929import { PrivateSearchResourceDto } from 'omniboxd/wizard/dto/agent-request.dto' ;
30- import { ResourcePermission } from 'omniboxd/permissions/resource-permission.enum' ;
30+ import {
31+ comparePermission ,
32+ ResourcePermission ,
33+ } from 'omniboxd/permissions/resource-permission.enum' ;
3134import { Response } from 'express' ;
3235import { ResourceDto , SpaceType } from './dto/resource.dto' ;
3336import { Namespace } from 'omniboxd/namespaces/entities/namespace.entity' ;
@@ -401,40 +404,49 @@ export class NamespaceResourcesService {
401404 namespaceId ,
402405 resourceId ,
403406 ) ;
404- return await this . getSubResourcesByParents ( namespaceId , parents , userId ) ;
407+ const subResources = await this . resourcesService . getSubResources (
408+ namespaceId ,
409+ [ resourceId ] ,
410+ ) ;
411+ const permissionMap = await this . permissionsService . getCurrentPermissions (
412+ userId ,
413+ namespaceId ,
414+ [ ...parents , ...subResources ] ,
415+ ) ;
416+ return subResources . filter ( ( res ) => {
417+ const permission = permissionMap . get ( res . id ) ;
418+ return (
419+ permission &&
420+ comparePermission ( permission , ResourcePermission . CAN_VIEW ) >= 0
421+ ) ;
422+ } ) ;
405423 }
406424
407- async getSubResourcesByParents (
408- namespaceId : string ,
409- parents : ResourceMetaDto [ ] ,
425+ async getAllSubResourcesByUser (
410426 userId : string ,
427+ namespaceId : string ,
428+ resourceId : string ,
411429 ) : Promise < ResourceMetaDto [ ] > {
412- if ( ! parents ) {
413- return [ ] ;
414- }
415- const permission = await this . permissionsService . getCurrentPermission (
430+ const parents = await this . resourcesService . getParentResourcesOrFail (
416431 namespaceId ,
417- parents ,
432+ resourceId ,
433+ ) ;
434+ const allSubResources = await this . resourcesService . getAllSubResources (
435+ namespaceId ,
436+ [ resourceId ] ,
437+ ) ;
438+ const permissionMap = await this . permissionsService . getCurrentPermissions (
418439 userId ,
440+ namespaceId ,
441+ [ ...parents , ...allSubResources ] ,
419442 ) ;
420- if ( permission === ResourcePermission . NO_ACCESS ) {
421- return [ ] ;
422- }
423- const children = await this . resourcesService . getSubResources ( namespaceId , [
424- parents [ 0 ] . id ,
425- ] ) ;
426- const filteredChildren : ResourceMetaDto [ ] = [ ] ;
427- for ( const child of children ) {
428- const permission = await this . permissionsService . getCurrentPermission (
429- namespaceId ,
430- [ child , ...parents ] ,
431- userId ,
443+ return allSubResources . filter ( ( res ) => {
444+ const permission = permissionMap . get ( res . id ) ;
445+ return (
446+ permission &&
447+ comparePermission ( permission , ResourcePermission . CAN_VIEW ) >= 0
432448 ) ;
433- if ( permission !== ResourcePermission . NO_ACCESS ) {
434- filteredChildren . push ( child ) ;
435- }
436- }
437- return filteredChildren ;
449+ } ) ;
438450 }
439451
440452 async listChildren (
@@ -446,42 +458,45 @@ export class NamespaceResourcesService {
446458 namespaceId ,
447459 resourceId ,
448460 ) ;
449- const children = await this . resourcesService . getSubResources ( namespaceId , [
461+ let children = await this . resourcesService . getSubResources ( namespaceId , [
450462 resourceId ,
451463 ] ) ;
452- const subChildren = await this . resourcesService . getSubResources (
464+ let subChildren = await this . resourcesService . getSubResources (
453465 namespaceId ,
454466 children . map ( ( child ) => child . id ) ,
455467 ) ;
456- const resources = [ ...parents , ...children , ...subChildren ] ;
457468 const permissionMap = await this . permissionsService . getCurrentPermissions (
458469 userId ,
459470 namespaceId ,
460- resources ,
471+ [ ... parents , ... children , ... subChildren ] ,
461472 ) ;
462473
474+ children = children . filter ( ( res ) => {
475+ const permission = permissionMap . get ( res . id ) ;
476+ return (
477+ permission &&
478+ comparePermission ( permission , ResourcePermission . CAN_VIEW ) >= 0
479+ ) ;
480+ } ) ;
481+
482+ subChildren = subChildren . filter ( ( res ) => {
483+ const permission = permissionMap . get ( res . id ) ;
484+ return (
485+ permission &&
486+ comparePermission ( permission , ResourcePermission . CAN_VIEW ) >= 0
487+ ) ;
488+ } ) ;
489+
463490 const hasChildrenMap = new Map < string , boolean > ( ) ;
464- for ( const subChild of subChildren ) {
465- if ( ! subChild . parentId ) {
466- continue ;
467- }
468- const permission = permissionMap . get ( subChild . id ) ;
469- if ( ! permission || permission === ResourcePermission . NO_ACCESS ) {
470- continue ;
491+ for ( const resource of subChildren ) {
492+ if ( resource . parentId ) {
493+ hasChildrenMap . set ( resource . parentId , true ) ;
471494 }
472- hasChildrenMap . set ( subChild . parentId , true ) ;
473495 }
474496
475- const childrenDtos : ChildrenMetaDto [ ] = [ ] ;
476- for ( const child of children ) {
477- const permission = permissionMap . get ( child . id ) ;
478- if ( ! permission || permission === ResourcePermission . NO_ACCESS ) {
479- continue ;
480- }
481- const hasChildren = hasChildrenMap . get ( child . id ) || false ;
482- childrenDtos . push ( new ChildrenMetaDto ( child , hasChildren ) ) ;
483- }
484- return childrenDtos ;
497+ return children . map (
498+ ( res ) => new ChildrenMetaDto ( res , ! ! hasChildrenMap . get ( res . id ) ) ,
499+ ) ;
485500 }
486501
487502 async getSpaceType (
0 commit comments