Commit aa3500c8 authored by Dominique Merle's avatar Dominique Merle 💬
Browse files

Even the author of a poll can't delete it once it has votes.

parent 32380608
......@@ -29,4 +29,11 @@ class PoetryFeatureContext extends BaseFeatureContext
*/
public function whenActorDoesNotDecideOnThings() {}
/**
* @When /^(?P<actor>.+?) does not like it$/ui
* @When /^(?P<actor>.+?) n'aime pas ça$/ui
*/
public function whenActorDoesNotLikeIt() {}
}
......@@ -2,6 +2,7 @@
namespace App\Repository;
use App\Entity\LimajuPoll;
use App\Entity\LimajuPollOptionVote;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
......@@ -19,6 +20,18 @@ class LimajuPollOptionVoteRepository extends ServiceEntityRepository
parent::__construct($registry, LimajuPollOptionVote::class);
}
public function countVotesOnPoll(LimajuPoll $poll)
{
$count = 0;
foreach ($poll->getOptions() as $option) {
$count += $this->count([
'option' => $option->getId(),
]);
}
return $count;
}
// /**
// * @return LimajuPollOptionVote[] Returns an array of LimajuPollOptionVote objects
// */
......
......@@ -6,6 +6,7 @@ namespace App\Security;
use App\Application;
use App\Entity\LimajuPoll;
use App\Repository\LimajuPollOptionVoteRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
......@@ -26,11 +27,17 @@ class PollBouncer extends Voter
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @var Application
*/
private $app;
/**
* @var LimajuPollOptionVoteRepository
*/
private $voteRepository;
/**
* Bouncer constructor.
......@@ -39,10 +46,12 @@ class PollBouncer extends Voter
*/
public function __construct(
Application $app,
LimajuPollOptionVoteRepository $voteRepository,
AuthorizationCheckerInterface $authorizationChecker
) {
$this->authorizationChecker = $authorizationChecker;
$this->app = $app;
$this->voteRepository = $voteRepository;
}
public function canCurrentUserEditPoll(LimajuPoll $poll, string $password='') : bool
......@@ -103,6 +112,10 @@ class PollBouncer extends Voter
return true;
}
if (0 < $this->voteRepository->countVotesOnPoll($subject)) {
return false;
}
if ($subject->getAuthor() === $this->app->getAuthenticatedUser()) {
return true;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment