1+ import { MissionGuardService } from '@/endpoints/auth/mission-guard.service' ;
12import {
23 ActionTriggerDto ,
34 CreateActionTriggerDto ,
@@ -12,13 +13,16 @@ import {
1213import { redis } from '@kleinkram/backend-common/consts' ;
1314import { ActionDispatcherService } from '@kleinkram/backend-common/modules/action-dispatcher/action-dispatcher.service' ;
1415import {
16+ AccessGroupRights ,
1517 ActionTriggerSource ,
1618 isValidCron ,
1719 TriggerEvent ,
1820 TriggerType ,
21+ UserRole ,
1922} from '@kleinkram/shared' ;
2023import {
2124 BadRequestException ,
25+ ForbiddenException ,
2226 Injectable ,
2327 NotFoundException ,
2428 OnModuleInit ,
@@ -43,13 +47,28 @@ export class TriggerService implements OnModuleInit {
4347 @InjectRepository ( MissionEntity )
4448 private missionRepository : Repository < MissionEntity > ,
4549 private readonly actionDispatcher : ActionDispatcherService ,
50+ private readonly missionGuardService : MissionGuardService ,
4651 ) { }
4752
4853 onModuleInit ( ) : void {
4954 this . triggerQueue = new Queue ( 'trigger-queue' , { redis } ) ;
5055 }
5156
52- async findAll ( missionUuid ?: string ) : Promise < ActionTriggerDto [ ] > {
57+ async findAll (
58+ user : UserEntity ,
59+ missionUuid ?: string ,
60+ ) : Promise < ActionTriggerDto [ ] > {
61+ if ( missionUuid && user . role !== UserRole . ADMIN ) {
62+ const hasAccess = await this . missionGuardService . canAccessMission (
63+ user ,
64+ missionUuid ,
65+ AccessGroupRights . READ ,
66+ ) ;
67+ if ( ! hasAccess ) {
68+ throw new ForbiddenException ( 'Forbidden resource' ) ;
69+ }
70+ }
71+
5372 const query = this . triggerRepository
5473 . createQueryBuilder ( 'trigger' )
5574 . leftJoinAndSelect ( 'trigger.template' , 'template' )
@@ -59,10 +78,31 @@ export class TriggerService implements OnModuleInit {
5978 query . where ( 'trigger.missionUuid = :missionUuid' , { missionUuid } ) ;
6079 }
6180 const entities = await query . getMany ( ) ;
81+
82+ if ( ! missionUuid && user . role !== UserRole . ADMIN ) {
83+ const allowedEntities : ActionTriggerEntity [ ] = [ ] ;
84+ for ( const entity of entities ) {
85+ if ( entity . creatorUuid === user . uuid ) {
86+ allowedEntities . push ( entity ) ;
87+ } else {
88+ const hasAccess =
89+ await this . missionGuardService . canAccessMission (
90+ user ,
91+ entity . missionUuid ,
92+ AccessGroupRights . READ ,
93+ ) ;
94+ if ( hasAccess ) {
95+ allowedEntities . push ( entity ) ;
96+ }
97+ }
98+ }
99+ return allowedEntities . map ( ( entity ) => this . toDto ( entity ) ) ;
100+ }
101+
62102 return entities . map ( ( entity ) => this . toDto ( entity ) ) ;
63103 }
64104
65- async findOne ( uuid : string ) : Promise < ActionTriggerDto > {
105+ async findOne ( uuid : string , user : UserEntity ) : Promise < ActionTriggerDto > {
66106 const trigger = await this . triggerRepository . findOne ( {
67107 where : { uuid } ,
68108 relations : { template : true , mission : true , creator : true } ,
@@ -72,6 +112,17 @@ export class TriggerService implements OnModuleInit {
72112 throw new NotFoundException ( 'Trigger not found' ) ;
73113 }
74114
115+ if ( user . role !== UserRole . ADMIN && trigger . creatorUuid !== user . uuid ) {
116+ const hasAccess = await this . missionGuardService . canAccessMission (
117+ user ,
118+ trigger . missionUuid ,
119+ AccessGroupRights . READ ,
120+ ) ;
121+ if ( ! hasAccess ) {
122+ throw new ForbiddenException ( 'Forbidden resource' ) ;
123+ }
124+ }
125+
75126 return this . toDto ( trigger ) ;
76127 }
77128
0 commit comments