handle modifier token

This commit is contained in:
Baptiste Lemoine 2020-02-04 12:38:07 +01:00
parent 158a795446
commit c2995e9799
3 changed files with 168 additions and 120 deletions

View File

@ -8,6 +8,7 @@ use App\Entity\Poll;
use App\Entity\StackOfVotes; use App\Entity\StackOfVotes;
use App\Entity\Vote; use App\Entity\Vote;
use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Route; use FOS\RestBundle\Controller\Annotations\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -22,7 +23,7 @@ use Symfony\Component\HttpFoundation\Request;
class VoteController extends AbstractController { class VoteController extends AbstractController {
/** /**
* add a vote on a poll * add a vote stack on a poll
* @Post( * @Post(
* path = "/poll/{id}/vote", * path = "/poll/{id}/vote",
* name = "new_vote_stack", * name = "new_vote_stack",
@ -128,13 +129,55 @@ class VoteController extends AbstractController {
'choices' => $choices, 'choices' => $choices,
'choices_count' => $poll->computeAnswers(), 'choices_count' => $poll->computeAnswers(),
'vote_count' => count( $poll->getStacksOfVotes() ), 'vote_count' => count( $poll->getStacksOfVotes() ),
'owner_modifier_token' => $foundOwner->getModifierToken(), 'owner' => $stack->getOwner(),
'owner_modifier_token' => $stack->getOwner()->getModifierToken(),
'admin_key' => $poll->getAdminKey(), 'admin_key' => $poll->getAdminKey(),
'json_you_sent' => $data, 'json_you_sent' => $data,
], ],
201 ); 201 );
} }
/**
* update vote stack
* @Patch(
* path = "/vote-stack/{id}/token/{modifierToken}",
* name = "update_vote_stack",
* requirements = { "id"="\d+"}
* )
*/
public
function updateVoteStackAction(
StackOfVotes $id,
$modifierToken,
Request $request
) {
$voteStack = $id;
if ( ! $voteStack ) {
return $this->json( [ 'message' => 'vote stack not found' ], 404 );
}
$poll = $voteStack->getPoll();
// if only self users are allowed to modify a vote, check it
if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) {
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
403 );
}
// everything is ok, we can update all the votes of the vote stack
//TODO
// match votes and choices
// update answers
// save evrything
return $this->json( [
'message' => 'ok',
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
'vote_stack' => $voteStack->display(),
],
200 );
}
/** /**
* @Delete( * @Delete(
* path = "/poll/{id}/votes/{accessToken}", * path = "/poll/{id}/votes/{accessToken}",

View File

@ -46,6 +46,7 @@ class StackOfVotes {
public function display() { public function display() {
$tab = [ $tab = [
'id' => $this->getId(), 'id' => $this->getId(),
'modifier_token' => $this->getOwner()->getModifierToken(),
'pseudo' => '', 'pseudo' => '',
'creation_date' => '', 'creation_date' => '',
'votes' => [], 'votes' => [],
@ -58,13 +59,8 @@ class StackOfVotes {
} }
foreach ( $this->getVotes() as $vote ) { foreach ( $this->getVotes() as $vote ) {
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = [ $tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
'id' => $this->getId(), $tab[ 'votes' ][ $vote->getChoice()->getId() ][ 'stack_id' ] = $this->getId();
'vote_id' => $vote->getId(),
'value' => $vote->getValue(),
'choice_id' => $vote->getChoice()->getId(),
'text' => $vote->getChoice()->getName(),
];
$tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); $tab[ 'pseudo' ] = $this->getOwner()->getPseudo();
$tab[ 'creation_date' ] = $vote->getCreationDate(); $tab[ 'creation_date' ] = $vote->getCreationDate();
} }

View File

@ -1,15 +1,15 @@
<?php <?php
namespace App\Entity; namespace App\Entity;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer; use JMS\Serializer\Annotation as Serializer;
/** /**
* @ORM\Entity(repositoryClass="App\Repository\VoteRepository") * @ORM\Entity(repositoryClass="App\Repository\VoteRepository")
*/ */
class Vote { class Vote {
/** /**
* for a text kind of choice: could be "yes" "no" "maybe" and emptu. * for a text kind of choice: could be "yes" "no" "maybe" and emptu.
* for a date kind, the choice linked is equivalent to the value selected * for a date kind, the choice linked is equivalent to the value selected
@ -52,6 +52,15 @@ class Vote {
*/ */
private $stacksOfVotes; private $stacksOfVotes;
public function display() {
return [
'id' => $this->getId(),
'value' => $this->getValue(),
'choice_id' => $this->getChoice()->getId(),
'text' => $this->getChoice()->getName(),
];
}
public function __construct() { public function __construct() {
$this->setCreationDate( new \DateTime() ); $this->setCreationDate( new \DateTime() );
} }
@ -112,4 +121,4 @@ class Vote {
return $this; return $this;
} }
} }