src/Security/Voter/DomainAuthenticationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Validator\Validator\ValidatorInterface;
  7. class DomainAuthenticationVoter extends Voter {
  8. const DOMAIN_AUTH_COMPLETE = 'domain-auth-complete';
  9. private $validator;
  10. public function __construct(ValidatorInterface $validator)
  11. {
  12. $this->validator = $validator;
  13. }
  14. protected function supports($attribute, $subject): bool
  15. {
  16. if(!in_array($attribute, [self::DOMAIN_AUTH_COMPLETE])){
  17. return false;
  18. }
  19. if(!$subject instanceof User){
  20. return false;
  21. }
  22. return true;
  23. }
  24. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  25. {
  26. $user = $token->getUser();
  27. if(!$user instanceof User){
  28. return false;
  29. }
  30. $subject;
  31. switch ($attribute) {
  32. case self::DOMAIN_AUTH_COMPLETE:
  33. return $this->isDomainAuthComplete($user);
  34. }
  35. throw new \LogicException('This code should not be reached!');
  36. }
  37. private function isDomainAuthComplete(User $user): bool
  38. {
  39. $errors = $this->validator->validate($user);
  40. if ($user->getDomains()->count() == 0) {
  41. return false;
  42. }
  43. return true;
  44. }
  45. }