<?php
namespace App\Security\Voter;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class DomainAuthenticationVoter extends Voter {
const DOMAIN_AUTH_COMPLETE = 'domain-auth-complete';
private $validator;
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
protected function supports($attribute, $subject): bool
{
if(!in_array($attribute, [self::DOMAIN_AUTH_COMPLETE])){
return false;
}
if(!$subject instanceof User){
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if(!$user instanceof User){
return false;
}
$subject;
switch ($attribute) {
case self::DOMAIN_AUTH_COMPLETE:
return $this->isDomainAuthComplete($user);
}
throw new \LogicException('This code should not be reached!');
}
private function isDomainAuthComplete(User $user): bool
{
$errors = $this->validator->validate($user);
if ($user->getDomains()->count() == 0) {
return false;
}
return true;
}
}