<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Entity\User;
class MediaVoter extends Voter
{
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['POST_EDIT', 'POST_VIEW'])
&& $subject instanceof \App\Entity\Media;
}
protected function voteOnAttribute(string $attribute, $media, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if($user->getIsActive()===0)
return false;
if (!$user instanceof UserInterface) {
return false;
}
if($media->getMediaCategorie()->getType() == "candidature" && $media->getMediaCategorie()->getName() == "photo" && count($media->getContact()->getSejourSessionEquipiers())>0 )
return true;
if(in_array(User::ROLE_ADMIN,$user->getRoles()))
return true;
if(in_array('ROLE_DIRECTEUR',$user->getRoles())&&$media->getMediaCategorie()->getType()=="candidature")
return true;
if(in_array('ROLE_DIRECTEUR',$user->getRoles())&&$media->getMediaCategorie()->getType()=="App\Entity\Sejour")
return true;
if($media->getContact() !== null && $media->getContact()==$user->getContact())
return true;
if($media->getContact() !== null && $media->getContact()->getParent() == $user->getContact())
return true;
if($user->getContact() !== null && $media->getMediaCategorie()->getName() == "projet pedagogique")
return true;
if($media->getSejourSession()!== null && $media->getSejourSession()->getDirector()==$user->getContact())
return true;
if($media->getInscription() !== null && $media->getInscription()->getParent() == $user->getContact())
return true;
//todo voir si sur sessions
if(in_array('ROLE_DIRECTEUR',$user->getRoles())){
return $media->getMediaCategorie()->getIsEspaceDirecteur();
if($media->getContact() !== null)
{
//mettre plus de sécurite
return $media->getMediaCategorie()->getIsEspaceDirecteur();
}
}
return false;
}
}