From 834f1284fe36910345255e57f6d1a4284f94b255 Mon Sep 17 00:00:00 2001 From: j-weissen Date: Mon, 26 Sep 2022 23:13:50 +0200 Subject: [PATCH] findOne --- .../strapi/src/api/note/controllers/note.js | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/backend/strapi/src/api/note/controllers/note.js b/backend/strapi/src/api/note/controllers/note.js index a69f15d..65e3eb5 100644 --- a/backend/strapi/src/api/note/controllers/note.js +++ b/backend/strapi/src/api/note/controllers/note.js @@ -29,15 +29,22 @@ module.exports = createCoreController('api::note.note', ({strapi}) => ({ return JSON.stringify(entries); }, /** - * Finds the note by id. Only returns the note when the user is assigned. + * Finds the note by id. Exits 403 if the note does not belong to the user making the request * @param ctx - * @returns {Promise} + * @returns {Promise} */ async findOne(ctx) { - console.log("findOne") - } -})); + const noteId = Number(ctx.request.url.split("/").at(-1)); + const userId = ctx.state.user.id; + const entry = await strapi.entityService.findOne('api::note.note', noteId, { + populate: ['owners'], + }); + let allowed = entry.owners.some(owner => owner.id === userId) + if (allowed) { + return JSON.stringify(entry); + } else { + ctx.response.status = 403; + } + }, -/** - * - */ +}));