From 149d6d47d9ba04e42546f56aa34b54bf7b710e45 Mon Sep 17 00:00:00 2001 From: PaulCombal Date: Fri, 1 Mar 2019 16:26:21 +0100 Subject: [PATCH] auto --- NotificationsBundle/Controller/MainController.php | 55 +++++++++++++++++------ composer.lock | 19 ++++++++ 2 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 composer.lock diff --git a/NotificationsBundle/Controller/MainController.php b/NotificationsBundle/Controller/MainController.php index 7692056..8948bfb 100644 --- a/NotificationsBundle/Controller/MainController.php +++ b/NotificationsBundle/Controller/MainController.php @@ -6,37 +6,50 @@ use Bluesquare\NotificationsBundle\Entity\Notification; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; /** - * Oui, je throw des exceptions sans catch, c'est comme ça qu'on utilise ce framework proprement - * https://symfony.com/doc/current/controller.html#managing-errors-and-404-pages - * * Class MainController * @package Bluesquare\NotificationsBundle\Controller */ class MainController extends AbstractController { + /** + * Get all the current user's notifications + * + * @return \Symfony\Component\HttpFoundation\JsonResponse + */ public function getAll() { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + if(!($user = $this->getUser())) + { + return $this->json(["message" => "Please login"], 403); + } $notifssrv = $this->get('bluesquare.notifications_bundle.notifssrv'); - $user = $this->getUser(); - $notifs = $notifssrv->getForUser($user, true); + return $this->json($notifs); } + /** + * Delete a notifiation that belong to the current user. + * + * @param $id + * @return \Symfony\Component\HttpFoundation\JsonResponse + */ public function delete($id) { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + if(!($user = $this->getUser())) + { + return $this->json(["message" => "Please login"], 403); + } + $em = $this->getDoctrine()->getManager(); - $user = $this->getUser(); $notif = $em->getRepository(Notification::class)->find($id); if (!$notif) - throw $this->createNotFoundException('Notification not found'); + return $this->json(["message" => "Notification not found"], 404); if ($notif->getUser() != $user) - throw $this->createAccessDeniedException(); + return $this->json(["message" => "Access denied"], 403); $em->remove($notif); $em->flush(); @@ -44,18 +57,27 @@ class MainController extends AbstractController return $this->json(["message" => 'OK']); } + /** + * Mark a notification of the current user as 'Seen' + * + * @param $id + * @return \Symfony\Component\HttpFoundation\JsonResponse + */ public function markSeen($id) { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + if(!($user = $this->getUser())) + { + return $this->json(["message" => "Please login"], 403); + } + $em = $this->getDoctrine()->getManager(); - $user = $this->getUser(); $notif = $em->getRepository(Notification::class)->find($id); if (!$notif) - throw $this->createNotFoundException('Notification not found'); + return $this->json(["message" => "Notification not found"], 404); if ($notif->getUser() != $user) - throw $this->createAccessDeniedException(); + return $this->json(["message" => "Access denied"], 403); $notif->setSeenAt(new \DateTime()); @@ -66,6 +88,11 @@ class MainController extends AbstractController } + /** + * Test method that adds a dummy notification to the current user. + * + * @return \Symfony\Component\HttpFoundation\JsonResponse + */ public function test() { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..82a25c5 --- /dev/null +++ b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "71470e0f0c2bb3d0849e24978e4ee684", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=7.1" + }, + "platform-dev": [] +}