+ return $this->view($person, Response::HTTP_CREATED,
+ ['Location' => $this->generateUrl('show_person',
+ ['id' => $person->getId(),
+ UrlGeneratorInterface::ABSOLUTE_URL])]);
+ }
+
+ /**
+ * @Rest\Post(
+ * path = "/api/person/authenticate",
+ * name = "authenticate_person"
+ * )
+ * @Rest\View(StatusCode = Response::HTTP_ACCEPTED)
+ */
+ public function authenticatePersonAction(Request $request)
+ {
+ $em = $this->getDoctrine()->getManager();
+ $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+
+ if (empty($person)) {
+ return $this->PersonNotFound();
+ }
+
+ if ($request->get('password') != $person->getPassword()) {
+ return $this->PersonWrongPassword();
+ } else {
+ return $this->view($person, Response::HTTP_ACCEPTED,
+ ['Location' => $this->generateUrl('show_person',
+ ['id' => $person->getId(),
+ UrlGeneratorInterface::ABSOLUTE_URL])]);
+ }
+ }
+
+ /**
+ * @Rest\Get("/api/person/{id}/localisations")
+ * @Rest\View()
+ */
+ public function getLocalisationsAction(Request $request)
+ {
+ //TODO: Check that the authenticated user is allowed to see the localisation
+ $em = $this->getDoctrine()->getManager();
+ $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
+
+ if (empty($localisations)) {
+ return $this->PersonLocalisationsNotFound();
+ }
+
+ return $localisations;
+ }
+
+ /**
+ * @Rest\Get(
+ * path = "/api/person/{id}/localisations/fuzzy/{distance}",
+ * name = "person_localisations_fuzzy",
+ * requirements = {"id"="\d+", "distance"="\d+"}
+ * )
+ * @Rest\View()
+ */
+ public function getLocalisationsFuzzyAction(Request $request)
+ {
+ //TODO: Check that the authenticated user is allowed to see the localisation
+ $em = $this->getDoctrine()->getManager();
+ $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
+
+ if (empty($localisations)) {
+ return $this->PersonLocalisationsNotFound();
+ }
+
+ if (!$this->chk_distance($request->get('distance'), 200, 500)) {
+ return $this->PersonLocalisationFuzzyWrongDistance();
+ }
+
+ $fuzzy_localisations = array_map(function($item) use ($request) { return $this->randomizeLocation($item, $request->get('distance'), 200, 500); }, $localisations);
+
+ return $fuzzy_localisations;
+ }
+
+ private function getLastLocalisation($em, $id) {
+ $query = $em->createQuery("SELECT l1 FROM App\Entity\Localisation l1
+ WHERE l1.person = :person and l1.timestamp =
+ (SELECT MAX(l2.timestamp) FROM App\Entity\Localisation l2
+ WHERE l2.person = l1.person)");
+ $query->setParameter('person', $id);
+ $result = $query->getResult();
+ if (!empty($result)) {
+ return $result[0];
+ }