我创建了一个带有评论系统的博客,我希望作者或管理员删除他的评论。
所以我搜索了互联网,但我发现只有关于 Symfony 2/3 的帖子,我很难理解。
所以我创建了自己的函数
/**
* @Route("/blog/commentDelete/{id}-{articleId}-{articleSlug}", name="comment_delete")
*/
public function commentDelete($id, $articleId, $articleSlug, CommentRepository $commentRepository, AuthorizationCheckerInterface $authChecker){
$em = $this->getDoctrine()->getManager();
$comment = $commentRepository->find($id);
$user = $this->getUser();
if ($user->getId() != $comment->getAuthor()->getId() && $authChecker->isGranted('ROLE_MODERATOR') == false ){
throw exception_for("Cette page n'existe pas");
}
$em->remove($comment);
$em->flush();
$this->addFlash('comment_success', 'Commentaire supprimé avec succès');
return $this->redirectToRoute('blog_show', array('id' => $articleId, 'slug' => $articleSlug));
}
在树枝上,我有这个链接:
<a href="{{ path('comment_delete', {'id': comment.id, 'articleId': article.id, 'articleSlug': article.slug}) }}">Supprimer</a>
我需要该操作的评论 ID,以及文章 ID 和文章 slug 以在删除评论后重定向用户。
我检查删除评论的人是作者还是版主。
但是,我听说这绝对不安全,因为我必须使用表单,但我真的不知道在这种情况下如何使用表单......或者也许用JS隐藏到最终用户的链接?
所以我想知道我的功能是否足够安全,或者是否存在更好的解决方案以及如何实现它?