1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Comment; |
4
|
|
|
|
5
|
|
|
use App\Entity\Comment; |
6
|
|
|
use App\Event\Comment\CommentEditedEvent; |
7
|
|
|
use App\Exception\RedirectException; |
8
|
|
|
use App\Form\Type\CommentType; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class EditCommentController |
17
|
|
|
* @package App\Controller\Comment |
18
|
|
|
* @IsGranted("ROLE_USER") |
19
|
|
|
*/ |
20
|
|
|
class EditCommentController extends AbstractController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var EventDispatcherInterface |
24
|
|
|
*/ |
25
|
|
|
private $dispatcher; |
26
|
|
|
|
27
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
28
|
|
|
{ |
29
|
|
|
$this->dispatcher = $dispatcher; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @Route("/comment/edit/{id}", name="comment.edit", methods={"GET"}) |
34
|
|
|
*/ |
35
|
|
|
public function editComment(Comment $comment) |
36
|
|
|
{ |
37
|
|
|
$this->checkSecurity($comment); |
38
|
|
|
$commentForm = $this->createForm(CommentType::class, $comment, [ |
39
|
|
|
'save_button_label' => 'Update', |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
return $this->render('comment/edit.html.twig', [ |
43
|
|
|
'comment' => $comment, |
44
|
|
|
'commentForm' => $commentForm->createView(), |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Route("/comment/edit/{id}", name="comment.submit", methods={"POST"}) |
51
|
|
|
*/ |
52
|
|
|
public function editCommentSubmit(Comment $comment, Request $request) |
53
|
|
|
{ |
54
|
|
|
$this->checkSecurity($comment); |
55
|
|
|
|
56
|
|
|
$form = $this->createForm(CommentType::class, $comment, [ |
57
|
|
|
'save_button_label' => 'Update', |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$form->handleRequest($request); |
61
|
|
|
|
62
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
63
|
|
|
|
64
|
|
|
$event = new CommentEditedEvent($comment); |
65
|
|
|
$this->dispatcher->dispatch(CommentEditedEvent::NAME, $event); |
66
|
|
|
|
67
|
|
|
return $this->redirectToRoute('trick.show', [ |
68
|
|
|
'id' => $comment->getTrick()->getId(), |
69
|
|
|
'slug' => $comment->getTrick()->getSlug(), |
70
|
|
|
'_fragment' => 'comment-'.$comment->getId(), |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
//This should never be called unless we have some strange error |
75
|
|
|
return $this->render('comment/edit.html.twig', [ |
76
|
|
|
'comment' => $comment, |
77
|
|
|
'commentForm' => $form->createView(), |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Comment $comment |
84
|
|
|
* Checks if the user is admin or author of the comment. |
85
|
|
|
* Thows a redirect to the trick show page |
86
|
|
|
*/ |
87
|
|
|
private function checkSecurity(Comment $comment){ |
88
|
|
|
if(!($this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() === $comment->getUser()->getId())) |
89
|
|
|
{ |
90
|
|
|
Throw new RedirectException($this->generateUrl('trick.show', ['id'=> $comment->getTrick()->getId(), 'slug'=> $comment->getTrick()->getSlug()]),"You are not allowed to edit this comment"); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
} |