<?php
/**
* Created by Notive.
* Project: wms.p
* User: Henny Krijnen
* Date: 07/03/17
* Time: 11:16
*/
namespace WmsBundle\Security;
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use WmsBundle\Exceptions\DeprecatedException;
use WmsBundle\Services\AuthorizationService;
/**
* Class RoleProvider
* @package WmsBundle\Security
*/
class RoleRoutingVoter implements VoterInterface
{
/** @var AuthorizationService */
private $authorizationService;
/** @var bool */
private $wmsRolesEnabled;
/**
* RoleVoter constructor.
* @param AuthorizationService $authorizationService
* @param bool $wmsRolesEnabled
*/
public function __construct(AuthorizationService $authorizationService, $wmsRolesEnabled = true)
{
$this->authorizationService = $authorizationService;
$this->wmsRolesEnabled = $wmsRolesEnabled;
}
/**
* @param TokenInterface $token
* @param null|object $object
* @param array $attributes
* @return int
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* @throws UnauthorizedHttpException
*/
public function vote(TokenInterface $token, $object, array $attributes)
{
if ($this->wmsRolesEnabled &&
$token instanceof OAuthToken &&
!$this->authorizationService->isAuthorized($token, $object, $attributes)
) {
throw new AccessDeniedHttpException('Sorry, you do not have permission to do this.');
}
return Voter::ACCESS_ABSTAIN;
}
/**
* Checks if the voter supports the given attribute.
*
* @param mixed $attribute An attribute (usually the attribute name string)
*
* @return bool true if this Voter supports the attribute, false otherwise
*
* @deprecated since version 2.8, to be removed in 3.0.
*/
public function supportsAttribute($attribute)
{
throw new DeprecatedException('supportsAttribute');
}
/**
* Checks if the voter supports the given class.
*
* @param string $class A class name
*
* @return bool true if this Voter can process the class
*
* @deprecated since version 2.8, to be removed in 3.0.
*/
public function supportsClass($class)
{
throw new DeprecatedException('supportsClass');
}
}