src/Security/Voter/DisclaimerVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class DisclaimerVoter extends Voter
  8. {
  9.     protected function supports($attribute$subject)
  10.     {
  11.         return in_array($attribute, [ 'DISCLAIMER_AGREED']);
  12.     }
  13.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  14.     {
  15.         /** @var User $user */
  16.         $user $token->getUser();
  17.         // if the user is anonymous, do not grant access
  18.         if (!$user instanceof UserInterface) {
  19.             return false;
  20.         }
  21.         // ... (check conditions and return true to grant permission) ...
  22.         switch ($attribute) {
  23.             case 'DISCLAIMER_AGREED':
  24.                 if($user->getIsDisclaimer() == true) {
  25.                     return true;
  26.                 }
  27.                 break;
  28.         }
  29.         return false;
  30.     }
  31. }