Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/api/comment/content-types/comment/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ module.exports = {
event.params.data.author = ctx.state.user.username;
},

beforeUpdate(event) {
// Only content fields (e.g. text) are editable — prevent the owner
// from reassigning the comment via the update payload.
delete event.params.data.user;
delete event.params.data.author;
delete event.params.data.authorId;
delete event.params.data.idea_card;
},

async afterCreate(event) {
const { id: commentId, text: commentText } = event.result;
const comment = await strapi.entityService.findOne(
Expand Down
19 changes: 19 additions & 0 deletions src/api/comment/policies/comment-owner-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = async (ctx) => {
const user = ctx.state.user;

if (!user || !ctx.params?.id) {
return false;
}

const comment = await strapi.entityService.findOne(
"api::comment.comment",
ctx.params.id,
{ populate: ["user"] }
);

if (!comment || !comment.user) {
return false;
}

return comment.user.id === user.id;
};
11 changes: 10 additions & 1 deletion src/api/comment/routes/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@

const { createCoreRouter } = require('@strapi/strapi').factories;

module.exports = createCoreRouter('api::comment.comment');
module.exports = createCoreRouter('api::comment.comment', {
config: {
update: {
policies: ['api::comment.comment-owner-filter'],
},
delete: {
policies: ['api::comment.comment-owner-filter'],
},
},
});
Loading