<?php
namespace App\Profile\Subprofiles\Controller\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;
class SubprofileAdminVoter extends Voter {
const VIEW = 'view';
protected function supports(string $attribute, mixed $subject): bool
{
if (!in_array($attribute, [self::VIEW])) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
/** @var Profile */
$user = $token->getUser();
switch ($attribute) {
case self::VIEW:
return $this->canView($subject, $user);
}
throw new \LogicException('This code should not be reached');
}
private function canView($subject, $user) {
$roles = $user->getRoles();
if (in_array(ProfileRoles::ROLE_CLUB_HORSE, $roles)) {
return true;
}
$subprofiles = $user->getActiveSubprofileTypes();
if (in_array($subject, $subprofiles)) {
return true;
}
return false;
}
}