88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Security\Voter;
|
|
|
|
use App\BusProNet\DataProvider\HotelDataProvider;
|
|
use App\BusProNet\Model\Hotel;
|
|
use App\Entity\Disposition;
|
|
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 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($token) || $this->assertTeamerAccess($token, $disposition),
|
|
static::DELETE => $this->assertAdministrativeAccess($token),
|
|
static::FEEDBACK => $this->assertHouseManagerAccess($token, $disposition),
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
private function assertAdministrativeAccess(TokenInterface $token): bool
|
|
{
|
|
return in_array('ROLE_ADMINISTRATIVE', $token->getRoleNames());
|
|
}
|
|
|
|
private function assertHouseManagerAccess(TokenInterface $token, Disposition $disposition): bool
|
|
{
|
|
if (false === in_array('ROLE_HOUSE_MANAGER', $token->getRoleNames())) {
|
|
return false;
|
|
}
|
|
|
|
$hotels = $this
|
|
->hotelDataProvider
|
|
->findByCode($token->getUser()->getHotelCode())
|
|
;
|
|
$hotelBusProIds = array_map(function (Hotel $hotel) {
|
|
return $hotel->getBusProId();
|
|
}, $hotels);
|
|
$destination = $disposition
|
|
->getAssignment()
|
|
->getDestination()
|
|
;
|
|
|
|
return in_array($destination->getHotelBusProId(), $hotelBusProIds)
|
|
&& $destination->getDateTo() < new \DateTimeImmutable();
|
|
}
|
|
|
|
private function assertTeamerAccess(TokenInterface $token, Disposition $disposition): bool
|
|
{
|
|
if (false === in_array('ROLE_TEAMER', $token->getRoleNames())) {
|
|
return false;
|
|
}
|
|
|
|
return $token->getUser()->getTeamer() === $disposition->getTeamer();
|
|
}
|
|
} |