<?php
namespace App\Voter;
use App\BundleExtensions\Oauth2\OAuthStorage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SystemVoter extends Voter
{
private $OAuthStorage;
private $attributes = ["ROLE_SYSTEM"];
public function __construct(OAuthStorage $OAuthStorage)
{
$this->OAuthStorage = $OAuthStorage;
}
protected function supports(string $attribute, $subject)
{
return in_array($attribute, $this->attributes);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
/** @var \FOS\OAuthServerBundle\Model\AccessToken $accessToken */
$accessToken = $this->OAuthStorage->getAccessToken($token->getToken());
$client = $accessToken->getClient();
/*
* There is no user with "system" role, so if we identify a "system" client,
* we give it the "system" role to be able to execute some api routes
*/
$allowedRoles = explode(" ", $client->getAllowedRoles());
$role = strtolower(str_replace("ROLE_", "", $attribute));
return in_array($role, $allowedRoles);
}
}