88 lines
2.6 KiB
PHP
88 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Security\Voter;
|
|
|
|
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)
|
|
{
|
|
}
|
|
|
|
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->security->isGranted('ROLE_ADMINISTRATIVE')
|
|
|| $this->assertTeamerAccess($token, $disposition),
|
|
static::DELETE => $this->security->isGranted('ROLE_ADMIN'),
|
|
static::FEEDBACK => $this->security->isGranted('ROLE_ADMINISTRATIVE')
|
|
|| $this->assertHouseManagerAccess($token, $disposition),
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
private function assertHouseManagerAccess(TokenInterface $token, Disposition $disposition): bool
|
|
{
|
|
if (false === $this->security->isGranted('ROLE_HOUSE_MANAGER')) {
|
|
return false;
|
|
}
|
|
|
|
/** @var User $user */
|
|
$user = $token->getUser();
|
|
|
|
$destination = $disposition
|
|
->getAssignment()
|
|
->getDestination()
|
|
;
|
|
|
|
$isMatchingHotel = $user->hasHotelCodeMatch($destination->getHotelCode());
|
|
$isPast = $destination->getDateTo() < new \DateTimeImmutable();
|
|
|
|
return $isMatchingHotel && $isPast;
|
|
}
|
|
|
|
private function assertTeamerAccess(TokenInterface $token, Disposition $disposition): bool
|
|
{
|
|
if (false === $this->security->isGranted('ROLE_TEAMER')) {
|
|
return false;
|
|
}
|
|
|
|
/** @var User $user */
|
|
$user = $token->getUser();
|
|
|
|
return $user->getTeamer() === $disposition->getTeamer();
|
|
}
|
|
}
|