src/Security/UserAnnotationVoter.php line 12

Open in your IDE?
  1. <?php 
  2. namespace App\Security;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use App\Entity\User;
  9. class UserAnnotationVoter extends Voter
  10. {
  11.     const EDIT 'edit';
  12.     const CONFIGURATION 'configuration';
  13.     /**
  14.      * @var AccessDecisionManagerInterface
  15.      */
  16.     private $decisionManager;
  17.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  18.     {
  19.         $this->decisionManager $decisionManager;
  20.     }
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         if (!in_array($attribute, array(self::EDITself::CONFIGURATION))) {
  24.             return false;
  25.         }
  26.         if (!$subject[0] instanceof User) {
  27.             return false;
  28.         }
  29.         if (!$subject[1] instanceof Request) {
  30.             return false;
  31.         }
  32.         return true;
  33.     }
  34.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  35.     {
  36.         $user $token->getUser();
  37.         $actualUser $subject[0];
  38.         $actualAgencyId $subject[1]->getSession()->get('agencyId');
  39.         if(!$user instanceof UserInterface){
  40.             return false;
  41.         }
  42.         switch($attribute){
  43.             case self::EDIT:
  44.                 if($actualUser->canEditAgency($actualAgencyId))
  45.                     return true;
  46.                 break;
  47.             case self::CONFIGURATION:
  48.                 if($actualUser->canConfigureAgency($actualAgencyId))
  49.                     return true;
  50.                 break;
  51.         }
  52.         return false;
  53.     }
  54. }