src/Security/Voter/FrontendClientVoter.php line 14

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\Authorization\AccessDecisionManagerInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * Security voter to grant backend access by client.
  10.  */
  11. class FrontendClientVoter extends Voter
  12. {
  13.     /** @var AccessDecisionManagerInterface */
  14.     protected $decisionManager;
  15.     /**
  16.      * ClientVoter constructor.
  17.      */
  18.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  19.     {
  20.         $this->decisionManager $decisionManager;
  21.     }
  22.     /**
  23.      * @return bool
  24.      */
  25.     protected function supports($attribute$subject)
  26.     {
  27.         return 'frontend_client_allowed' === $attribute;
  28.     }
  29.     /**
  30.      * @return bool
  31.      */
  32.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  33.     {
  34.         $user $token->getUser();
  35.         if (!$subject instanceof Client) {
  36.             return false;
  37.         }
  38.         $client null;
  39.         if ($user instanceof User) {
  40.             $client $user->getClient();
  41.         } else {
  42.             $client $user->getApplicationClient();
  43.         }
  44.         if (!$client) {
  45.             return false;
  46.         }
  47.         if ($this->decisionManager->decide($token, ['ROLE_SUPER_USER'])) {
  48.             return true;
  49.         }
  50.         if (!$subject->getClient() instanceof Client) {
  51.             return false;
  52.         }
  53.         if ($client->getId() === $subject->getClient()->getId()) {
  54.             return true;
  55.         }
  56.         return false;
  57.     }
  58. }