src/Security/Voter/UserVoter.php line 13

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Classes\Data\UserRolesData;
  4. use App\Entity\User;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class UserVoter extends Voter {
  11.     public const EDIT 'USER_EDIT';
  12.     public const VIEW 'USER_VIEW';
  13.     public const DELETE 'USER_DELETE';
  14.     public function __construct(private readonly Security $security, private readonly LoggerInterface $logger) {
  15.     }
  16.     protected function supports(string $attributemixed $subject): bool {
  17.         // https://symfony.com/doc/current/security/voters.html
  18.         if (!in_array($attribute, [self::VIEWself::EDITself::DELETE])) {
  19.             return false;
  20.         }
  21.         // only vote on `Post` objects
  22.         if (!$subject instanceof User) {
  23.             return false;
  24.         }
  25.         return true;
  26.     }
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool {
  28.         $user $token->getUser();
  29. //     if the user is anonymous, do not grant access
  30.         if (!$user instanceof UserInterface) {
  31.             return false;
  32.         }
  33.         /*
  34.          * Super admin moze sve i odmah se setuje da ima dozvolu
  35.          */
  36.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  37.             return true;
  38.         }
  39.         if ($this->security->isGranted('ROLE_UPRAVNIK_CENTRALE')) {
  40.             if (!is_null($subject->getGrana())) {
  41.                 return false;
  42.             }
  43.         }
  44.         if ($this->security->isGranted('ROLE_UPRAVNIK_GRANE')) {
  45.             if (is_null($subject->getGrana()) || ($subject->getGrana()->getId() != $user->getGranskiSindikatUpGrana()->getId())) {
  46.                 return false;
  47.             }
  48.         }
  49.         $korisnik $subject;
  50.         return match($attribute) {
  51.             self::VIEW => $this->canView($korisnik$user),
  52.             self::EDIT => $this->canEdit($korisnik$user),
  53.             self::DELETE => $this->canDelete($korisnik$user),
  54.         };
  55.     }
  56.     private function canEdit(User $korisnikUser $user): bool {
  57.         if ($this->security->isGranted('ROLE_UPRAVNIK_CENTRALE'))  {
  58.             if ($korisnik->getUserType() == UserRolesData::ROLE_REG_POVERENIK) {
  59.                 return true;
  60.             }
  61.         }
  62.         if ($this->security->isGranted('ROLE_UPRAVNIK_GRANE')) {
  63.             if ($korisnik->getUserType() == UserRolesData::ROLE_POVERENIK || $korisnik->getUserType() == UserRolesData::ROLE_REG_POVERENIK) {
  64.                 return true;
  65.             }
  66.         }
  67.         return false;
  68.     }
  69.     private function canView(User $korisnikUser $user): bool {
  70.         if ($this->security->isGranted('ROLE_UPRAVNIK_CENTRALE'))  {
  71.             if ($korisnik->getUserType() == UserRolesData::ROLE_REG_POVERENIK) {
  72.                 return true;
  73.             }
  74.         }
  75.         if ($this->security->isGranted('ROLE_UPRAVNIK_GRANE')) {
  76.             if ($korisnik->getUserType() == UserRolesData::ROLE_POVERENIK || $korisnik->getUserType() == UserRolesData::ROLE_REG_POVERENIK) {
  77.                 return true;
  78.             }
  79.         }
  80.         return false;
  81.     }
  82.     private function canDelete(User $korisnikUser $user): bool {
  83.         if ($this->security->isGranted('ROLE_UPRAVNIK_CENTRALE')) {
  84.             if ($korisnik->getUserType() == UserRolesData::ROLE_REG_POVERENIK) {
  85.                 return true;
  86.             }
  87.         }
  88.         if ($this->security->isGranted('ROLE_UPRAVNIK_GRANE')) {
  89.             if ($korisnik->getUserType() == UserRolesData::ROLE_POVERENIK || $korisnik->getUserType() == UserRolesData::ROLE_REG_POVERENIK) {
  90.                 return true;
  91.             }
  92.         }
  93.         return false;
  94.     }
  95. }