<?php
namespace App\Security\Voter;
use ADAS\eBudzetBundle\Entity\BuStatusy;
use ADAS\eBudzetBundle\Entity\BuWnioski;
use ADAS\LogowanieBundle\Entity\SysUsers;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class WniosekVoter extends Voter
{
public const ZATWIERDZ = 'zatwierdz';
public const EDYCJA = 'edycja';
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute,[self::ZATWIERDZ,self::EDYCJA]) && $subject instanceof BuWnioski;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof SysUsers) {
return false;
}
/** @var BuWnioski $wniosek */
$wniosek = $subject;
return match($attribute) {
self::ZATWIERDZ => $this->canZatwierdz($wniosek),
self::EDYCJA => $this->canEdit($wniosek),
default => throw new LogicException('This code should not be reached!')
};
}
private function canZatwierdz(BuWnioski $wniosek): bool
{
return $wniosek->getRStatus()?->getNrid() === BuStatusy::STATUS_WYSLANY
&& $wniosek->getUzytkownikAkceptujacy() === null;
}
private function canEdit(BuWnioski $wniosek): bool
{
return in_array($wniosek->getRStatus()?->getNrid(),BuStatusy::getStatusyPozwalajaceNaModyfikacjePozycji());
}
}