Files
api.pawol.nu/src/policies/is-document-owner.js
T

36 lines
917 B
JavaScript
Raw Normal View History

'use strict';
2026-07-04 20:00:51 +04:00
const { UnauthorizedError, NotFoundError } = require('@strapi/utils').errors;
module.exports = async (policyContext, config, {strapi}) => {
2026-07-04 20:00:51 +04:00
const {request, params} = policyContext;
if (!request?.header?.authorization) {
2026-07-04 20:00:51 +04:00
throw new UnauthorizedError('Opération non autorisée');
}
2026-07-04 20:00:51 +04:00
let jwtUserId;
try {
2026-07-04 20:00:51 +04:00
({id: jwtUserId} = await strapi.plugins['users-permissions'].services.jwt.getToken(policyContext));
} catch (err) {
2026-07-04 20:00:51 +04:00
throw new UnauthorizedError('Opération non autorisée');
}
2026-07-04 20:00:51 +04:00
const documentId = params?.id ?? request.body?.data?.documentId;
const document = await strapi.db.query(config.uid).findOne({
where: {documentId},
populate: {user: true}
2026-07-04 20:00:51 +04:00
});
if (!document) {
2026-07-04 20:00:51 +04:00
throw new NotFoundError('Ressource introuvable.');
}
if (document.user?.id !== jwtUserId) {
2026-07-04 20:00:51 +04:00
throw new UnauthorizedError('Opération non autorisée');
}
2026-07-04 20:00:51 +04:00
return true;
};