<?php
declare(strict_types=1);
/*
* Copyright (C) Office National de Publication et de Communication - All Rights Reserved
*
* Unauthorized copying of this file, via any medium is strictly prohibited by law
* This file is proprietary and confidential
*/
namespace App\Security\Voter;
use App\Entity\OffreEmploi;
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;
final class OffreEmploiVoter extends Voter
{
public const EDIT = 'edit';
public const DELETE = 'delete';
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
return $subject instanceof OffreEmploi && \in_array($attribute, [
self::EDIT,
self::DELETE,
], true);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
// Utilisateur doit être authentifié
if (!$token->getUser() instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::EDIT:
case self::DELETE:
return $this->canManipulate($subject, $token);
}
return false;
}
private function canManipulate(OffreEmploi $offre, TokenInterface $token): bool
{
// Actualités distantes ne peuvent pas être modifié dans le portail
if ($offre->getIdDistant()) {
return false;
}
if (!$this->security->isGranted('ROLE_RESPONSABLE_ETABLISSEMENT')) {
return false;
}
$etab = $offre->getEtablissement();
if (!$etab) {
return false;
}
return $token->getUser()->getEtablissementsResponsable()->contains($etab);
}
}