mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
check if user has permission to delete comments
This commit is contained in:
parent
3dea2c3544
commit
f67f7a8863
@ -62,6 +62,7 @@ export default class CommentsController extends Controller {
|
||||
fn: async (req, res) => {
|
||||
const result = await deleteComment({
|
||||
comment_id: req.params.comment_id,
|
||||
issuer_id: req.user._id.toString(),
|
||||
}).catch((err) => {
|
||||
res.status(500).json({ message: err.message })
|
||||
|
||||
|
@ -1,18 +1,29 @@
|
||||
import { Comment } from "../../../models"
|
||||
import CheckUserAdmin from "../../../lib/checkUserAdmin"
|
||||
|
||||
export default async (payload) => {
|
||||
const { comment_id } = payload
|
||||
const { issuer_id, comment_id } = payload
|
||||
|
||||
if (!issuer_id) {
|
||||
throw new Error("Missing issuer_id")
|
||||
}
|
||||
|
||||
if (!comment_id) {
|
||||
throw new Error("Missing comment_id")
|
||||
}
|
||||
|
||||
const isAdmin = await CheckUserAdmin(issuer_id)
|
||||
|
||||
const comment = await Comment.findById(comment_id)
|
||||
|
||||
if (!comment) {
|
||||
throw new Error("Comment not found")
|
||||
}
|
||||
|
||||
if (comment.user_id !== issuer_id && !isAdmin) {
|
||||
throw new Error("You can't delete this comment, cause you are not the owner.")
|
||||
}
|
||||
|
||||
await comment.delete()
|
||||
|
||||
global.wsInterface.io.emit(`comment.delete.${comment_id}`)
|
||||
|
Loading…
x
Reference in New Issue
Block a user