<?php
declare(strict_types=1);
/**
* @author Mehrez Labidi
*/
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Services\ManagerEntity\ProfilRepreneurManagers;
/**
* Description of ProfilRepreneurVoter
*
* @author mehrez
*/
class ProfilRepreneurVoter extends Voter {
public const VIEW_OWN_PROFIL_REPRENEUR = 'VIEW_OWN_PROFIL_REPRENEUR';
private $requestStack;
public function __construct(RequestStack $requestStack, ProfilRepreneurManagers $prm) {
$this->requestStack = $requestStack;
$this->prm = $prm;
}
protected function supports(string $attribute, $subject): bool {
return $attribute === self::VIEW_OWN_PROFIL_REPRENEUR;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool {
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return false;
}
$id_profil_repreneur = $request->attributes->get('id_profil_repreneur');
if(!$id_profil_repreneur){ // premiere etape sans profil_repreneur // rien à proteger donc !
return true;
}
return (bool) $this->prm->iamTheOwner($id_profil_repreneur);
}
}