<?php
namespace App\Security\Voter;
use Menke\UserBundle\Entity\User;
use Menke\UserBundle\Entity\Client;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
/**
* Security voter to grant frontend access by client.
*
* @package App\Security\Voter
*/
class OAuthClientVoter extends Voter
{
/**
* @var AccessDecisionManagerInterface
*/
protected $decisionManager;
/**
* ClientVoter constructor.
* @param AccessDecisionManagerInterface $decisionManager
*/
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
/**
* @return bool
*/
protected function supports($attribute, $subject)
{
return $attribute === 'has_client';
}
/**
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
if ($token instanceof UsernamePasswordToken) {
return false;
}
$user = $token->getUser();
$client = null;
if ($user instanceof User) {
$client = $user->getClient();
} else {
$client = $user->getApplicationClient();
}
if (empty($client)) {
return false;
}
if ($client instanceof Client) {
return true;
}
return false;
}
}