From 0f8d981c944d2115f7d8903a43aed8cddd349862 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Mon, 7 Jun 2021 12:13:00 +0200 Subject: [PATCH] send modifier token after submitting a vote stack --- config/packages/framework.yaml | 2 +- src/Controller/api/v1/VoteController.php | 51 ++++++++++++++++++------ src/Entity/StackOfVotes.php | 14 +++++-- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index 0db6bbb..1d3bbda 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -1,6 +1,6 @@ # see https://symfony.com/doc/current/reference/configuration/framework.html framework: - trusted_hosts: ['localhost:4200', 'localhost', 'tktest.lan', '127.0.0.1', '127.0.0.1:8000', 'framadate-api.cipherbliss.com'] + trusted_hosts: ['localhost:4200', 'localhost', 'tktest.lan', '127.0.0.1', '127.0.0.1:4200', '127.0.0.1:8000', 'framadate-api.cipherbliss.com'] secret: '%env(APP_SECRET)%' #csrf_protection: true #http_method_override: true diff --git a/src/Controller/api/v1/VoteController.php b/src/Controller/api/v1/VoteController.php index f5e2903..21b80ef 100644 --- a/src/Controller/api/v1/VoteController.php +++ b/src/Controller/api/v1/VoteController.php @@ -21,50 +21,50 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Class DefaultController * @package App\Controller - * @Route("/api/v1/vote",name="api_") + * @Route("/api/v1/vote-stack",name="api_") */ class VoteController extends EmailsController { /** * add a vote stack on a poll * @Route( - * path = "/vote-stack", + * path = "/", * name = "new_vote_stack", * methods={"POST","OPTIONS"} * ) * * @param SerializerInterface $serializer - * @param string $custom_url * @param Request $request * * @return JsonResponse|Response */ public function newVoteStackAction( SerializerInterface $serializer, - string $custom_url, Request $request, ChoiceRepository $choice_repository ) { + $data = $request->getContent(); + $data = json_decode( $data, true ); + $poll_custom_url = $data['poll_custom_url']; /*** * checks before persisting */ $em = $this->getDoctrine()->getManager(); $emPol = $em->getRepository( Poll::class ); - $poll = $emPol->findOneByCustomUrl( $custom_url ); + $poll = $emPol->findOneByCustomUrl( $poll_custom_url ); // check : existence of poll if ( ! $poll ) { - return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 ); + return $this->json( [ 'message' => 'poll "' . $poll_custom_url . '" not found' ], 404 ); } // check : limit of number of participation max if ( count( $poll->getStacksOfVotes() ) == $poll->getVotesMax() ) { - return $this->json( [ 'message' => 'poll "' . $custom_url . '" not allowed to have more stack of votes than ' . $poll->getVotesMax() ], + return $this->json( [ 'message' => 'poll "' . $poll_custom_url . '" not allowed to have more stack of votes than ' . $poll->getVotesMax() ], 403 ); } - $data = $request->getContent(); - $data = json_decode( $data, true ); + // var_dump($data); // die(); @@ -125,16 +125,15 @@ class VoteController extends EmailsController { $this->sendVoteNotificationAction( $newStack->getOwner(), $newStack ); } - return $this->json( $newStack->display() ); + return $this->json( $newStack->displayForAdmin() ); } /** * update vote stack * @Route( - * path = "/vote-stack/{id}/token/{modifierToken}", + * path = "/{id}/token/{modifierToken}", * name = "update_vote_stack", - * requirements = { "id"="\d+"} * methods={"PATCH","OPTIONS"} * ) * @param SerializerInterface $serializer @@ -185,6 +184,34 @@ class VoteController extends EmailsController { } + /** + * @Route( + * path = "/{id}/token/{modifierToken}", + * name = "delete_vote_stack", + * requirements = { "id"="\d+","modifierToken"="\w+"}, + * methods={"DELETE"} + * ) + * @param StackOfVotes $stack_of_votes + */ + public function deleteVoteStackAction(StackOfVotes $stack_of_votes,$modifierToken){ + if ( $modifierToken == $stack_of_votes->getOwner()->getModifierToken() ) { + $em = $this->getDoctrine()->getManager(); + $id = $stack_of_votes->getId() ; + $em->remove( $stack_of_votes ); + $em->flush(); + + return $this->json( [ + 'message' => 'boom! la stack de vote ' . $id . ' a été supprimée', + ], + 200 ); + } else { + return $this->json( [ + 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier cet ensemble de réponses', + ], + 403 ); + } + } + /** * @Delete( * path = "/poll/{id}/votes/{accessToken}", diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 4c5fa00..5783b75 100755 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -58,18 +58,24 @@ class StackOfVotes { } public function display() { - $votes = $this->getVotes(); $tab = [ - // 'id' => $this->getId(), - // 'modifier_token' => $this->getOwner()->getModifierToken(), + 'id' => $this->getId(), 'pseudo' => $this->getPseudo(), 'created_at' => $this->getCreatedAtAsString(), 'votes' => [], ]; foreach ( $this->getVotes() as $vote ) { - $tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display(); + $tab[ 'votes' ][ ] = $vote->display(); } + $tab[ 'owner' ] = $this->getOwner()->display(); + + return $tab; + } + + public function displayForAdmin() { + $tab = $this->display(); + $tab[ 'owner' ] = $this->getOwner()->displayForAdmin(); return $tab; }