src/Security/Voter/ReclamationCustomerVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. class ReclamationCustomerVoter extends Voter
  7. {
  8.     public const EDIT = 'POST_EDIT';
  9.     public const VIEW = 'POST_VIEW';
  10.     protected function supports(string $attribute, $subject): bool
  11.     {
  12.         // replace with your own logic
  13.         // https://symfony.com/doc/current/security/voters.html
  14.         return in_array($attribute, [self::EDIT, self::VIEW])
  15.             && $subject instanceof \App\Entity\ReclamationCustomer;
  16.     }
  17.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  18.     {
  19.         $user = $token->getUser();
  20.         // if the user is anonymous, do not grant access
  21.         if (!$user instanceof UserInterface) {
  22.             return false;
  23.         }
  24.         // ... (check conditions and return true to grant permission) ...
  25.         switch ($attribute) {
  26.             case self::EDIT:
  27.                 if ($subject->getStatus() == "non traité" && $subject->getUser() == $user) {
  28.                     return true;
  29.                 } else {
  30.                     return false;
  31.                 }
  32.                 break;
  33.             case self::VIEW:
  34.                 // logic to determine if the user can VIEW
  35.                 // return true or false
  36.                 break;
  37.         }
  38.         return false;
  39.     }
  40. }