<?php
namespace App\Profile\Security\Voter;
use App\Profile\Model\Profile;
use App\Profile\Model\ProfileRoles;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class NetworkVoter extends Voter
{
public const ADD = 'profile.network.add';
public function __construct(protected Security $security)
{
}
protected function supports(string $attribute, $subject): bool
{
if (!in_array($attribute, [self::ADD])) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Profile) {
return false;
}
/** @var Profile $profileToAdd */
$profileToAdd = $subject;
switch ($attribute) {
case self::ADD:
return $this->canAdd($user, $profileToAdd);
}
throw new \LogicException(sprintf('Attribute `%s` not supported by object', $attribute));
}
protected function canAdd(Profile $profile, Profile $profileToAdd): bool
{
if ($profileToAdd->getProfiles()->getAuctionhouseProfile() || $profileToAdd->getProfiles()->getRacetrackProfile()) {
return $this->security->isGranted([ProfileRoles::ROLE_CLUB_JOCKEY, ProfileRoles::ROLE_CLUB_HORSE]);
}
return true;
}
}