src/Security/Voter/OAuthClientVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\User\Entity\Client;
  4. use App\User\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  7. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. /**
  10.  * Security voter to grant frontend access by client.
  11.  */
  12. class OAuthClientVoter extends Voter
  13. {
  14.     /**
  15.      * @var AccessDecisionManagerInterface
  16.      */
  17.     protected $decisionManager;
  18.     /**
  19.      * ClientVoter constructor.
  20.      */
  21.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  22.     {
  23.         $this->decisionManager $decisionManager;
  24.     }
  25.     /**
  26.      * @return bool
  27.      */
  28.     protected function supports($attribute$subject)
  29.     {
  30.         return 'has_client' === $attribute;
  31.     }
  32.     /**
  33.      * @return bool
  34.      */
  35.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  36.     {
  37.         if ($token instanceof UsernamePasswordToken) {
  38.             return false;
  39.         }
  40.         $user $token->getUser();
  41.         $client null;
  42.         if ($user instanceof User) {
  43.             $client $user->getClient();
  44.         } else {
  45.             $client $user->getApplicationClient();
  46.         }
  47.         if (empty($client)) {
  48.             return false;
  49.         }
  50.         if ($client instanceof Client) {
  51.             return true;
  52.         }
  53.         return false;
  54.     }
  55. }