<?php
namespace App\Voter\__Booking;
use App\Entity\Booking\Booking;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class BookingContractVoter extends Voter
{
const DETAILS = "BOOKING_CONTRACT_DETAILS";
const GET_CONTRACT = "BOOKING_CONTRACT_GET_CONTRACT";
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
self::DETAILS,
self::GET_CONTRACT,
]) && ($subject === null || $subject instanceof Booking);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
if ($subject === null) {
return false;
}
switch ($attribute) {
case self::DETAILS:
return $this->voteOnDetails($subject, $loggedUser);
case self::GET_CONTRACT:
return $this->voteOnGetContract($subject, $loggedUser);
}
return false;
}
private function voteOnDetails(?Booking $booking, UserInterface $loggedUser): bool
{
return in_array(self::DETAILS, $loggedUser->getRoles());
}
private function voteOnGetContract(?Booking $booking, UserInterface $loggedUser): bool
{
return in_array(self::GET_CONTRACT, $loggedUser->getRoles());
}
}