src/Security/CampaignVoter.php line 26

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by IntelliJ IDEA.
  4.  * User: jochen
  5.  * Date: 12.09.16
  6.  * Time: 15:14
  7.  */
  8. namespace App\Security;
  9. // use Monolog\Logger;
  10. use App\Entity\User;
  11. use App\Entity\Document;
  12. use App\Entity\Project;
  13. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  14. use Symfony\Component\Form\Exception\RuntimeException;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  18. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. use Psr\Log\LoggerInterface;
  22. use Psr\Log\LoggerAwareInterface;
  23. class CampaignVoter implements VoterInterfaceLoggerAwareInterface
  24. {
  25.     const ACCESS   'access';
  26.     private $decisionManager;
  27.     /**
  28.      * @var LoggerInterface $logger
  29.      */
  30.     private $logger;
  31.     protected $jwtManager;
  32.     protected $tokenStorageInterface;
  33.     /**
  34.      * @var RequestStack $requestStack
  35.      */
  36.     protected $requestStack;
  37.     private $user;
  38.     public function __construct(
  39.         AccessDecisionManagerInterface $decisionManager,
  40.         TokenStorageInterface $tokenStorageInterface,
  41.         JWTTokenManagerInterface $jwtManager,
  42.         RequestStack $requestStack)
  43.     {
  44.         $this->decisionManager $decisionManager;
  45.         $this->jwtManager $jwtManager;
  46.         $this->tokenStorageInterface $tokenStorageInterface;
  47.         $this->requestStack $requestStack;
  48.     }
  49.     public function setLogger(LoggerInterface $logger)
  50.     {
  51.         $this->logger $logger;
  52.     }
  53.     public function supportsAttribute($attribute)
  54.     {
  55.         return in_array($attribute, array(
  56.             self::ACCESS,
  57.         ));
  58.     }
  59.     public function supportsClass($object)
  60.     {
  61.         $this->logger->info(sprintf("CampaignVoter class supported"));
  62.         $supportedClasses = array(
  63.             'App\Entity\Campaign',
  64.         );
  65.         foreach($supportedClasses as $supportedClass) {
  66.             if ($object instanceof $supportedClass) {
  67.                 return true;
  68.             }
  69.         }
  70.         return false;
  71.     }
  72.     public function vote(TokenInterface $token$object, array $attributes)
  73.     {
  74.         $decodedJwtToken $this->jwtManager->decode($this->tokenStorageInterface->getToken());
  75.         $handleFromToken $decodedJwtToken["campaign"];
  76.         $handleFromRequest $this->requestStack->getCurrentRequest()->get('campaignHandle');
  77.         if ($handleFromToken !== $handleFromRequest) {
  78.             $this->logger->info(sprintf("CampaignVoter handle mismatch between request %s and toke  %s"$handleFromToken$handleFromRequest));
  79.             return VoterInterface::ACCESS_DENIED;
  80.         }
  81.         if (is_null($object)) {
  82.             return VoterInterface::ACCESS_ABSTAIN;
  83.         }
  84.         $attr $attributes[0];
  85.         if (!$this->supportsClass($object) || !$this->supportsAttribute($attr)) {
  86.             return VoterInterface::ACCESS_ABSTAIN;
  87.         }
  88.         if ($object->getActive()) {
  89.             return VoterInterface::ACCESS_GRANTED;
  90.         }
  91.         else {
  92.             return VoterInterface::ACCESS_ABSTAIN;
  93.         }
  94.     }
  95. }