src/Security/Voter/ProfilRepreneurVoter.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author      Mehrez Labidi
  5.  */
  6. namespace App\Security\Voter;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use App\Services\ManagerEntity\ProfilRepreneurManagers;
  11. /**
  12.  * Description of ProfilRepreneurVoter
  13.  *
  14.  * @author mehrez
  15.  */
  16. class ProfilRepreneurVoter extends Voter {
  17.     public const VIEW_OWN_PROFIL_REPRENEUR 'VIEW_OWN_PROFIL_REPRENEUR';
  18.     private $requestStack;
  19.     public function __construct(RequestStack $requestStackProfilRepreneurManagers $prm) {
  20.         $this->requestStack $requestStack;
  21.         $this->prm $prm;
  22.     }
  23.     protected function supports(string $attribute$subject): bool {
  24.         return $attribute === self::VIEW_OWN_PROFIL_REPRENEUR;
  25.     }
  26.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool {
  27.         $request $this->requestStack->getCurrentRequest();
  28.         if (!$request) {
  29.             return false;
  30.         }
  31.         $id_profil_repreneur $request->attributes->get('id_profil_repreneur');
  32.         if(!$id_profil_repreneur){ // premiere etape sans  profil_repreneur // rien à proteger donc !
  33.             return true;
  34.         }
  35.         return (bool) $this->prm->iamTheOwner($id_profil_repreneur);
  36.     }
  37. }