From 84fd6c7fa765874e478ad7f92e356e88b3ff962c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 28 Jun 2018 13:52:46 +0200 Subject: [PATCH] Only expose relevant attributes. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Controller/PersonController.php | 34 +++++++++++++++++++++-------- src/Entity/Friendship.php | 4 ++++ src/Entity/Localisation.php | 5 +++++ src/Entity/Person.php | 7 ++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index d4bd7ff..734f29c 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -3,7 +3,6 @@ namespace App\Controller; use App\Entity\Person; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -#use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\View\ViewHandler; use FOS\RestBundle\View\View; @@ -16,7 +15,7 @@ class PersonController extends Controller { /** * @Rest\Post( - * path = "/person/inscription", + * path = "/api/person/inscription", * name = "create_person" * ) * @Rest\View(StatusCode = Response::HTTP_CREATED) @@ -75,23 +74,32 @@ class PersonController extends Controller } /** - * @Rest\Get("/api/person/{id}/friends/localisation") + * @Rest\Get("/api/person/{id}/localisations") + * @Rest\View() */ - public function getFriendsLocalisationAction(Request $request) + public function getLocalisationsAction(Request $request) { + $em = $this->getDoctrine()->getManager(); + $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); + if (empty($localisations)) { + return $this->PersonLocalisationsNotFound(); + } + + return $localisations; } /** - * @Rest\Get("/api/person/{id}/localisations") + * @Rest\Get("/api/person/{id}/localisation") + * @Rest\View() */ - public function getLocalisationsAction(Request $request) + public function getLocalisationAction(Request $request) { $em = $this->getDoctrine()->getManager(); - $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]); + $localisations = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]); if (empty($localisations)) { - return $this->PersonLocalisationsNotFound(); + return $this->PersonLocalisationNotFound(); } return $localisations; @@ -188,7 +196,7 @@ class PersonController extends Controller $persons = $em->getRepository('App:Person')->findAll(); if (empty($persons)) { - return $this->PersonNotFound(); + return $this->PersonsNotFound(); } return $persons; @@ -218,6 +226,14 @@ class PersonController extends Controller return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } + private function PersonsNotFound() { + return View::create(['message' => 'Persons not found'], Response::HTTP_NOT_FOUND); + } + + private function PersonLocalisationNotFound() { + return View::create(['message' => 'Person localisation not found'], Response::HTTP_NOT_FOUND); + } + private function PersonLocalisationsNotFound() { return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND); } diff --git a/src/Entity/Friendship.php b/src/Entity/Friendship.php index a069bce..444523f 100644 --- a/src/Entity/Friendship.php +++ b/src/Entity/Friendship.php @@ -2,10 +2,12 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation as Serializer; /** * @ORM\Entity() * @ORM\Table(name="Friendship"); + * @Serializer\ExclusionPolicy("all") */ class Friendship { @@ -18,11 +20,13 @@ class Friendship /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Person", inversedBy="friends_with_me", cascade={"all"}) + * @Serializer\Expose */ protected $friend; /** * @ORM\Column(type="boolean", options={"default":false}) + * @Serializer\Expose */ protected $is_valid; diff --git a/src/Entity/Localisation.php b/src/Entity/Localisation.php index 69431b9..b85a705 100644 --- a/src/Entity/Localisation.php +++ b/src/Entity/Localisation.php @@ -2,10 +2,12 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation as Serializer; /** * @ORM\Entity() * @ORM\Table(name="Localisation") + * @Serializer\ExclusionPolicy("all") */ class Localisation { @@ -23,16 +25,19 @@ class Localisation /** * @ORM\Column(type="datetime") + * @Serializer\Expose */ protected $timestamp; /** * @ORM\Column(type="float") + * @Serializer\Expose */ protected $latitude; /** * @ORM\Column(type="float") + * @Serializer\Expose */ protected $longitude; diff --git a/src/Entity/Person.php b/src/Entity/Person.php index d401cc3..d06f575 100644 --- a/src/Entity/Person.php +++ b/src/Entity/Person.php @@ -3,11 +3,13 @@ namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation as Serializer; /** * @ORM\Entity() * @ORM\Table(name="Person", indexes={@ORM\Index(name="authentification_idx", columns={"email", "password"}), * @ORM\Index(name="search_idx", columns={"firstname", "lastname", "email"})}); + * @Serializer\ExclusionPolicy("all") */ class Person { @@ -15,21 +17,25 @@ class Person * @ORM\Id * @ORM\Column(type="bigint") * @ORM\GeneratedValue(strategy="AUTO") + * @Serializer\Expose */ protected $id; /** * @ORM\Column(type="string") + * @Serializer\Expose */ protected $firstname; /** * @ORM\Column(type="string") + * @Serializer\Expose */ protected $lastname; /** * @ORM\Column(type="string", unique=true) + * @Serializer\Expose */ protected $email; @@ -40,6 +46,7 @@ class Person /** * @ORM\Column(type="boolean", options={"default":false}) + * @Serializer\Expose */ protected $online; -- 2.34.1