Files
myep/src/Security/Voter/BookingVoter.php
T

47 lines
1.1 KiB
PHP

<?php
namespace App\Security\Voter;
use App\BusProNet\Model\Booking;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class BookingVoter extends Voter
{
public const VIEW = 'VIEW';
public const EDIT = 'EDIT';
protected function supports(string $attribute, mixed $subject): bool
{
if (false === in_array($attribute, [self::VIEW, self::EDIT])) {
return false;
}
return $subject instanceof Booking;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
if (null === $user) {
return false;
}
/** @var Booking $booking */
$booking = $subject;
if ($booking->applicant->personId !== $user->getPersonId()) {
return false;
}
if (static::VIEW === $attribute) {
return true;
}
return $booking->isEditable();
}
}