From 1ad6a958e203540cbd2bd0ac1528297d807a10b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 28 Dec 2021 22:35:15 +0100 Subject: [PATCH] notification: add show action --- .../Controller/NotificationController.php | 21 ++++++++ .../views/Notification/list.html.twig | 44 +++++++++++----- .../views/Notification/show.html.twig | 28 ++++++++++ .../Authorization/NotificationVoter.php | 51 +++++++++++++++++++ .../config/services/security.yaml | 2 + 5 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Notification/show.html.twig create mode 100644 src/Bundle/ChillMainBundle/Security/Authorization/NotificationVoter.php diff --git a/src/Bundle/ChillMainBundle/Controller/NotificationController.php b/src/Bundle/ChillMainBundle/Controller/NotificationController.php index c3ec0e549..df38c15d9 100644 --- a/src/Bundle/ChillMainBundle/Controller/NotificationController.php +++ b/src/Bundle/ChillMainBundle/Controller/NotificationController.php @@ -18,6 +18,7 @@ use Chill\MainBundle\Notification\Exception\NotificationHandlerNotFound; use Chill\MainBundle\Notification\NotificationHandlerManager; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Repository\NotificationRepository; +use Chill\MainBundle\Security\Authorization\NotificationVoter; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -169,6 +170,26 @@ class NotificationController extends AbstractController ]); } + /** + * @Route("/{id}/show", name="chill_main_notification_show") + */ + public function showAction(Notification $notification, Request $request): Response + { + $this->denyAccessUnlessGranted(NotificationVoter::SEE, $notification); + + $response = $this->render('@ChillMain/Notification/show.html.twig', [ + 'notification' => $notification, + 'handler' => $this->notificationHandlerManager->getHandler($notification), + ]); + + if ($this->getUser() instanceof User && !$notification->isReadBy($this->getUser())) { + $notification->markAsReadBy($this->getUser()); + $this->em->flush(); + } + + return $response; + } + private function countUnread(): array { return [ diff --git a/src/Bundle/ChillMainBundle/Resources/views/Notification/list.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Notification/list.html.twig index 4a1f504f4..96074e14d 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Notification/list.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Notification/list.html.twig @@ -12,9 +12,11 @@