src/Security/Voter/AddressVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\Security;
  6. class AddressVoter extends Voter
  7. {
  8.     public const EDIT 'POST_EDIT';
  9.     public const VIEW 'POST_VIEW';
  10.     public const ADD 'POST_ADD';
  11.     public const DELETE 'POST_DELETE';
  12.     public function __construct( private readonly Security $security ) {}
  13.     protected function supports(string $attribute$subject): bool
  14.     {
  15.         return in_array($attribute, [self::EDITself::VIEWself::ADDself::DELETE])
  16.             && $subject instanceof \App\Entity\Address;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         /* DEBUG: por ahora asi
  21.          * hay que fijarse si el ROLE_MANAGER puede editar este address,
  22.          * fijarse si su organizacion le pertenece u otro modelo de datos
  23.          * mejor */
  24.         // $isMyOrganization = $this->security->getUser()->getOrganization() == $subject->getOrganization();
  25.         // dd( $isMyOrganization );
  26.         //
  27.         $isMyOrganization true;
  28.         if ($this->security->isGranted('ROLE_ADMIN'))
  29.             return true;
  30.         if ($this->security->isGranted('ROLE_MANAGER')  ) {
  31.             if ( self::ADD )
  32.                 return true;
  33.             else if ( $isMyOrganization )
  34.                 return true;
  35.             else return false;
  36.         }
  37.         return false;
  38.     }
  39. }