src/WmsBundle/Security/RoleRoutingVoter.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Notive.
  4.  * Project: wms.p
  5.  * User: Henny Krijnen
  6.  * Date: 07/03/17
  7.  * Time: 11:16
  8.  */
  9. namespace WmsBundle\Security;
  10. use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  16. use WmsBundle\Exceptions\DeprecatedException;
  17. use WmsBundle\Services\AuthorizationService;
  18. /**
  19.  * Class RoleProvider
  20.  * @package WmsBundle\Security
  21.  */
  22. class RoleRoutingVoter implements VoterInterface
  23. {
  24.     /** @var AuthorizationService */
  25.     private $authorizationService;
  26.     /** @var bool */
  27.     private $wmsRolesEnabled;
  28.     /**
  29.      * RoleVoter constructor.
  30.      * @param AuthorizationService $authorizationService
  31.      * @param bool $wmsRolesEnabled
  32.      */
  33.     public function __construct(AuthorizationService $authorizationService$wmsRolesEnabled true)
  34.     {
  35.         $this->authorizationService $authorizationService;
  36.         $this->wmsRolesEnabled $wmsRolesEnabled;
  37.     }
  38.     /**
  39.      * @param TokenInterface $token
  40.      * @param null|object $object
  41.      * @param array $attributes
  42.      * @return int
  43.      * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
  44.      * @throws UnauthorizedHttpException
  45.      */
  46.     public function vote(TokenInterface $token$object, array $attributes)
  47.     {
  48.         if ($this->wmsRolesEnabled &&
  49.             $token instanceof OAuthToken &&
  50.             !$this->authorizationService->isAuthorized($token$object$attributes)
  51.         ) {
  52.             throw new AccessDeniedHttpException('Sorry, you do not have permission to do this.');
  53.         }
  54.         return Voter::ACCESS_ABSTAIN;
  55.     }
  56.     /**
  57.      * Checks if the voter supports the given attribute.
  58.      *
  59.      * @param mixed $attribute An attribute (usually the attribute name string)
  60.      *
  61.      * @return bool true if this Voter supports the attribute, false otherwise
  62.      *
  63.      * @deprecated since version 2.8, to be removed in 3.0.
  64.      */
  65.     public function supportsAttribute($attribute)
  66.     {
  67.         throw new DeprecatedException('supportsAttribute');
  68.     }
  69.     /**
  70.      * Checks if the voter supports the given class.
  71.      *
  72.      * @param string $class A class name
  73.      *
  74.      * @return bool true if this Voter can process the class
  75.      *
  76.      * @deprecated since version 2.8, to be removed in 3.0.
  77.      */
  78.     public function supportsClass($class)
  79.     {
  80.         throw new DeprecatedException('supportsClass');
  81.     }
  82. }