<?php
namespace App\Profile\Security\Voter;
use App\Profile\Model\Profile;
use App\Profile\Model\ProfileRoles;
use Pimcore\Model\DataObject\Objectbrick\Data\AuctionhouseProfile;
use Pimcore\Model\DataObject\Objectbrick\Data\RacetrackProfile;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class SubprofileVoter extends Voter
{
public const LIST = 'profile.subprofile.list';
public function __construct(protected Security $security)
{
}
protected function supports(string $attribute, $subject): bool
{
if (!in_array($attribute, [self::LIST])) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Profile) {
return false;
}
$subprofiles = $subject;
switch ($attribute) {
case self::LIST:
return $this->canList($user, $subprofiles);
}
throw new \LogicException(sprintf('Attribute `%s` not supported by object', $attribute));
}
protected function canList(Profile $profile, array $subprofiles): bool
{
if (
in_array(AuctionhouseProfile::class, $subprofiles) ||
in_array(RacetrackProfile::class, $subprofiles)) {
return
$this->security->isGranted(ProfileRoles::ROLE_CLUB_HORSE) ||
$this->security->isGranted(ProfileRoles::ROLE_CLUB_JOCKEY);
}
return true;
}
}