<?php
namespace App\Security\Voter;
use App\Entity\AppelAProjet;
use DateTime;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use function PHPUnit\Framework\returnArgument;
class AppelAProjetVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, ['CREATE', 'EDIT', 'VIEW', 'DELETE', 'ADD_PROJECT', 'PREEVAL_SEND_EMAIL', 'RAPPORT_SEND_EMAIL', 'RECAP_PROJECT', 'GENERATE_EXPERTS'])
&& ($subject instanceof AppelAProjet || $subject == AppelAProjet::class);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
/** @var $subject AppelAProjet */
switch ($attribute) {
case 'CREATE':
case 'EDIT':
if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
return true;
}
break;
case 'VIEW':
if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
return true;
}
/** @var $subject AppelAProjet */
if ($subject->isPublished()) {
return true;
}
break;
case 'DELETE':
// todo : peut-être rajouter d'autres critères
if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
return true;
}
break;
case 'ADD_PROJECT':
// il faut que le projet soit publié
if (!$subject->isPublished()) {
return false;
}
// seuls les porteurs peuvent soumettre des projets en 2 phases
if ($this->security->isGranted('ROLE_PORTEUR')) {
// et qu'on soit dans la date de dépôt
$now = new DateTime();
if ($subject->getDateOpenLi() && $subject->getDateOpenLi() < $now && $subject->getDateCloseLi() > $now) {
return true;
}
}
// les projets en une phase sont déposés par les drci
if ($this->security->isGranted('ROLE_DRCI')) {
// et qu'on soit dans la date de dépôt
$now = new DateTime();
if (!$subject->getDateOpenLi() && $subject->getDateOpenDc() && $subject->getDateOpenDc() < $now && $subject->getDateCloseDc() > $now) {
return true;
}
}
break;
case 'PREEVAL_SEND_EMAIL':
if (!$this->security->isGranted('ROLE_ADMIN_GIRCI')) {
return false;
}
// en fonction de l'état de l'appel à projet
$status = $subject->getStatus();
if ($status == AppelAProjet::AAP_STATUS_POST_LI || $status == AppelAProjet::AAP_STATUS_PRE_EVAL) {
return true;
}
break;
case 'RAPPORT_SEND_EMAIL':
if (!$this->security->isGranted('ROLE_ADMIN_GIRCI')) {
return false;
}
// en fonction de l'état de l'appel à projet
$status = $subject->getStatus();
if ($status == AppelAProjet::AAP_STATUS_POST_DC || $status == AppelAProjet::AAP_STATUS_RAPPORT || $status == AppelAProjet::AAP_STATUS_POST_EXPERT) {
return true;
}
break;
case 'RECAP_PROJECT':
// pour le moment, juste interdire les anonymes
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
} else {
return true;
}
case 'GENERATE_EXPERTS':
if (!$this->security->isGranted('ROLE_ADMIN_GIRCI')) {
return false;
}
// en fonction de l'état de l'appel à projet
$status = $subject->getStatus();
if ($status == AppelAProjet::AAP_STATUS_POST_DC || $status == AppelAProjet::AAP_STATUS_EXPERT) {
return true;
}
break;
}
return false;
}
}