Skip to content

Commit ee9606c

Browse files
fix: includeAll dos produtores
1 parent 8738cfb commit ee9606c

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/controllers/producers.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { Authentication } from '../external/Authentication';
1919
import { hasPermissions } from '../utils/hasPermission';
2020
import type { ProductionUnitFilters } from '../interfaces/ProductionUnitFilters';
2121
import { StringSearchType } from '../enums/StringSearchType';
22-
import { UnauthorizedError } from '../errors/UnauthorizedError';
2322

2423
@Controller('/producers')
2524
@Injectable()
@@ -67,7 +66,16 @@ export class ProducersController {
6766
pageSize: Joi.number().integer().min(1),
6867
includeAll: Joi.boolean().optional()
6968
})
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+
}
7179
])
7280
public async getProducers(@Response() res: Express.Response, @Request() req: Express.Request) {
7381
const options: PaginatedOptions = {
@@ -76,9 +84,6 @@ export class ProducersController {
7684
};
7785
let producers;
7886
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');
8287
producers = await container.producerGateway.findAllWithDeletedAt(options);
8388
}
8489

@@ -196,14 +201,20 @@ export class ProducersController {
196201
query: Joi.object({
197202
includeAll: Joi.boolean().optional()
198203
})
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+
}
200214
])
201215
public async getProducer(@Response() res: Express.Response, @Params('producerId') producerId: number, @Request() req: Express.Request) {
202216
let producer;
203217
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');
207218
producer = await container.producerGateway.findByIdWithDeletedAt(producerId);
208219
}
209220

src/middlewares/authorization.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export const authorizationMiddleware = ({
2828
const user = await container.userGateway.findByAuthId(req.authUser.uid, { populate: ['role'] });
2929
if (!user) throw new NotFoundError('Authenticated user not found');
3030
req.user = user;
31-
3231
// Bitwise AND operator to check if the user has the required permission(s)
3332
let hasBasePermission = true;
3433
// Default to none if the user has no role

0 commit comments

Comments
 (0)