src/Security/Voter/WniosekVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use ADAS\eBudzetBundle\Entity\BuStatusy;
  4. use ADAS\eBudzetBundle\Entity\BuWnioski;
  5. use ADAS\LogowanieBundle\Entity\SysUsers;
  6. use LogicException;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class WniosekVoter extends Voter
  10. {
  11.     public const ZATWIERDZ = 'zatwierdz';
  12.      public const EDYCJA = 'edycja';
  13.     protected function supports(string $attribute, mixed $subject): bool
  14.     {
  15.         return in_array($attribute,[self::ZATWIERDZ,self::EDYCJA]) && $subject instanceof BuWnioski;
  16.     }
  17.     protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  18.     {
  19.         $user = $token->getUser();
  20.         if (!$user instanceof SysUsers) {
  21.             return false;
  22.         }
  23.         /** @var BuWnioski $wniosek */
  24.         $wniosek = $subject;
  25.         return match($attribute) {
  26.             self::ZATWIERDZ => $this->canZatwierdz($wniosek),
  27.             self::EDYCJA => $this->canEdit($wniosek),
  28.             default => throw new LogicException('This code should not be reached!')
  29.         };
  30.     }
  31.     private function canZatwierdz(BuWnioski $wniosek): bool
  32.     {
  33.         return $wniosek->getRStatus()?->getNrid() === BuStatusy::STATUS_WYSLANY
  34.             && $wniosek->getUzytkownikAkceptujacy() === null;
  35.     }
  36.     
  37.     private function canEdit(BuWnioski $wniosek): bool
  38.     {
  39.         return in_array($wniosek->getRStatus()?->getNrid(),BuStatusy::getStatusyPozwalajaceNaModyfikacjePozycji());
  40.     }
  41. }