@@ -19,7 +19,6 @@ import { Authentication } from '../external/Authentication';
19
19
import { hasPermissions } from '../utils/hasPermission' ;
20
20
import type { ProductionUnitFilters } from '../interfaces/ProductionUnitFilters' ;
21
21
import { StringSearchType } from '../enums/StringSearchType' ;
22
- import { UnauthorizedError } from '../errors/UnauthorizedError' ;
23
22
24
23
@Controller ( '/producers' )
25
24
@Injectable ( )
@@ -67,7 +66,16 @@ export class ProducersController {
67
66
pageSize : Joi . number ( ) . integer ( ) . min ( 1 ) ,
68
67
includeAll : Joi . boolean ( ) . optional ( )
69
68
} )
70
- } )
69
+ } ) ,
70
+ async ( req , res , next ) => {
71
+ if ( req . query . includeAll ) {
72
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
73
+ await authenticationMiddleware ( req , res , ( ) => { } ) ;
74
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
75
+ await authorizationMiddleware ( { permissions : Permission . READ_OTHER_PRODUCER } ) ( req , res , ( ) => { } ) ;
76
+ }
77
+ next ( ) ;
78
+ }
71
79
] )
72
80
public async getProducers ( @Response ( ) res : Express . Response , @Request ( ) req : Express . Request ) {
73
81
const options : PaginatedOptions = {
@@ -76,9 +84,6 @@ export class ProducersController {
76
84
} ;
77
85
let producers ;
78
86
if ( req . query . includeAll ) {
79
- if ( ! req . authUser ) throw new UnauthorizedError ( 'User is not authenticated' ) ;
80
- const user = await container . userGateway . findByAuthId ( req . authUser . uid ) ;
81
- if ( ! hasPermissions ( user ! , Permission . READ_OTHER_PRODUCER ) ) throw new ForbiddenError ( 'User may not include all' ) ;
82
87
producers = await container . producerGateway . findAllWithDeletedAt ( options ) ;
83
88
}
84
89
@@ -196,14 +201,20 @@ export class ProducersController {
196
201
query : Joi . object ( {
197
202
includeAll : Joi . boolean ( ) . optional ( )
198
203
} )
199
- } )
204
+ } ) ,
205
+ async ( req , res , next ) => {
206
+ if ( req . query . includeAll ) {
207
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
208
+ await authenticationMiddleware ( req , res , ( ) => { } ) ;
209
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
210
+ await authorizationMiddleware ( { permissions : Permission . READ_OTHER_PRODUCER } ) ( req , res , ( ) => { } ) ;
211
+ }
212
+ next ( ) ;
213
+ }
200
214
] )
201
215
public async getProducer ( @Response ( ) res : Express . Response , @Params ( 'producerId' ) producerId : number , @Request ( ) req : Express . Request ) {
202
216
let producer ;
203
217
if ( req . query . includeAll ) {
204
- if ( ! req . authUser ) throw new UnauthorizedError ( 'User is not authenticated' ) ;
205
- const user = await container . userGateway . findByAuthId ( req . authUser . uid ) ;
206
- if ( ! hasPermissions ( user ! , Permission . READ_OTHER_PRODUCER ) ) throw new ForbiddenError ( 'User may not include all' ) ;
207
218
producer = await container . producerGateway . findByIdWithDeletedAt ( producerId ) ;
208
219
}
209
220
0 commit comments