96 lines
2.9 KiB
PHP
96 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Security\Voter;
|
|
|
|
use App\BusProNet\DataProvider\HotelDataProvider;
|
|
use App\BusProNet\Model\Hotel;
|
|
use App\Entity\Disposition;
|
|
use App\Entity\User;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
|
|
class DispositionVoter extends Voter
|
|
{
|
|
public const VIEW = 'VIEW';
|
|
public const EDIT = 'EDIT';
|
|
public const DELETE = 'DELETE';
|
|
public const CONTRACT = 'CONTRACT';
|
|
public const INVOICE = 'INVOICE';
|
|
public const FEEDBACK = 'FEEDBACK';
|
|
|
|
public function __construct(
|
|
private readonly Security $security,
|
|
private readonly HotelDataProvider $hotelDataProvider
|
|
) {
|
|
}
|
|
|
|
protected function supports(string $attribute, mixed $subject): bool
|
|
{
|
|
if (!$subject instanceof Disposition) {
|
|
return false;
|
|
}
|
|
|
|
return in_array($attribute, [
|
|
static::VIEW,
|
|
static::EDIT,
|
|
static::DELETE,
|
|
static::CONTRACT,
|
|
static::INVOICE,
|
|
static::FEEDBACK,
|
|
]);
|
|
}
|
|
|
|
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
|
|
{
|
|
/** @var Disposition $disposition */
|
|
$disposition = $subject;
|
|
|
|
return match ($attribute) {
|
|
static::VIEW, static::EDIT, static::CONTRACT, static::INVOICE => $this->assertAdministrativeAccess() || $this->assertTeamerAccess($disposition),
|
|
static::DELETE => $this->assertAdminAccess(),
|
|
static::FEEDBACK => $this->assertHouseManagerAccess($token, $disposition),
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
private function assertAdminAccess(): bool
|
|
{
|
|
return $this->security->isGranted('ROLE_ADMIN');
|
|
}
|
|
|
|
private function assertAdministrativeAccess(): bool
|
|
{
|
|
return $this->security->isGranted('ROLE_ADMINISTRATIVE');
|
|
}
|
|
|
|
private function assertHouseManagerAccess(TokenInterface $token, Disposition $disposition): bool
|
|
{
|
|
if (false === $this->security->isGranted('ROLE_HOUSE_MANAGER')) {
|
|
return false;
|
|
}
|
|
|
|
$userHotelCode = $token->getUser()->getHotelCode();
|
|
|
|
$destination = $disposition
|
|
->getAssignment()
|
|
->getDestination()
|
|
;
|
|
|
|
$isMatchingHotel = str_starts_with($destination->getHotelCode(), $userHotelCode)
|
|
|| str_ends_with($destination->getHotelCode(), $userHotelCode);
|
|
$isPast = $destination->getDateTo() < new \DateTimeImmutable();
|
|
|
|
return $isMatchingHotel && $isPast;
|
|
}
|
|
|
|
private function assertTeamerAccess(Disposition $disposition): bool
|
|
{
|
|
if (false === $this->security->isGranted('ROLE_TEAMER')) {
|
|
return false;
|
|
}
|
|
|
|
return $this->security->getUser()->getTeamer() === $disposition->getTeamer();
|
|
}
|
|
}
|