src/User/Security/Voter/ClientVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\User\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 ClientVoter extends Voter
  12. {
  13.     /**
  14.      * @var AccessDecisionManagerInterface
  15.      */
  16.     protected $decisionManager;
  17.     /**
  18.      * ClientVoter constructor.
  19.      */
  20.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  21.     {
  22.         $this->decisionManager $decisionManager;
  23.     }
  24.     /**
  25.      * @return bool
  26.      */
  27.     protected function supports($attribute$subject)
  28.     {
  29.         return 'ROLE_SUPER_USER' === $attribute;
  30.     }
  31.     /**
  32.      * @return bool
  33.      */
  34.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  35.     {
  36.         $user $token->getUser();
  37.         if (!$user instanceof User) {
  38.             return false;
  39.         }
  40.         if (!$subject instanceof Client) {
  41.             return false;
  42.         }
  43.         if ($this->decisionManager->decide($token, ['ROLE_SUPER_USER'])) {
  44.             return true;
  45.         }
  46.         if ($subject instanceof User && !$subject->getClient() instanceof Client) {
  47.             return false;
  48.         }
  49.         if ($subject instanceof User && $user->getClient() && $user->getClient()->getId() === $subject->getClient()->getId() && $this->decisionManager->decide($token, ['ROLE_BACKEND'])) {
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54. }