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$subjectTokenInterface $token)
  19.     {
  20.         /** @var \FOS\OAuthServerBundle\Model\AccessToken $accessToken */
  21.         $accessToken $this->OAuthStorage->getAccessToken($token->getToken());
  22.         $client $accessToken->getClient();
  23.         /*
  24.          * There is no user with "system" role, so if we identify a "system" client,
  25.          * we give it the "system" role to be able to execute some api routes
  26.          */
  27.         $allowedRoles explode(" "$client->getAllowedRoles());
  28.         $role strtolower(str_replace("ROLE_"""$attribute));
  29.         return in_array($role$allowedRoles);
  30.     }
  31. }