<?php
namespace App\Security;
use App\Entity\User;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class DomainVoter extends Voter
{
private const DOMAIN_TYPES = [
'public',
'private',
];
public const OVERVIEW = 'view';
public function supports($attribute, $subject)
{
if (!\is_array($subject)) {
return false;
}
if (\count(array_diff($subject, self::DOMAIN_TYPES)) > 0) {
return false;
}
if (!\in_array($attribute, [self::OVERVIEW], true)) {
return false;
}
return true;
}
public function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
switch ($attribute) {
case self::OVERVIEW:
return $this->elegibleForOverview($subject, $token);
}
throw new LogicException('This line should never be reached');
}
/**
* @param $subject
*/
public function elegibleForOverview($subject, TokenInterface $token): bool
{
if (!\in_array('private', $subject, true)) {
return true;
}
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if (empty($user->getRoles()) || !\in_array('ROLE_SITE_MOBILITY_USER', $user->getRoles(), true)) {
return false;
}
return true;
}
}