src/Voter/SystemVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Voter;
  3. use App\BundleExtensions\OAuth2\OAuthStorage;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class SystemVoter extends Voter
  7. {
  8.     private $OAuthStorage;
  9.     private $attributes = ["ROLE_SYSTEM"];
  10.     public function __construct(OAuthStorage $OAuthStorage)
  11.     {
  12.         $this->OAuthStorage = $OAuthStorage;
  13.     }
  14.     protected function supports(string $attribute, $subject)
  15.     {
  16.         return in_array($attribute, $this->attributes);
  17.     }
  18.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
  19.     {
  20.         /** @var \FOS\OAuthServerBundle\Model\AccessToken $accessToken */
  21.         $accessToken = $this->OAuthStorage->getAccessToken($token->getToken());
  22.         $client = $accessToken->getClient();
  23.         $allowedRoles = explode(" ", $client->getAllowedRoles());
  24.         $role = strtolower(str_replace("ROLE_", "", $attribute));
  25.         return in_array($role, $allowedRoles);
  26.     }
  27. }