58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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;
|
|
|
|
/**
|
|
* Determines access permissions for booking operations.
|
|
*
|
|
* Enforces ownership check via personId matching. VIEW access requires ownership,
|
|
* EDIT access additionally requires the booking to be in an editable state
|
|
* (determined by booking.isEditable()).
|
|
*
|
|
* @extends Voter<string, mixed>
|
|
*/
|
|
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|null $user */
|
|
$user = $token->getUser();
|
|
|
|
if (null === $user) {
|
|
return false;
|
|
}
|
|
|
|
/** @var Booking $booking */
|
|
$booking = $subject;
|
|
|
|
if ($booking->applicant->addressId !== $user->getAddressId()) {
|
|
return false;
|
|
}
|
|
|
|
if (static::VIEW === $attribute) {
|
|
return true;
|
|
}
|
|
|
|
return $booking->isEditable();
|
|
}
|
|
}
|