src/Security/Voter/Messenger/MessengerVoter.php line 16

  1. <?php
  2. namespace App\Security\Voter\Messenger;
  3. use App\Entity\Messenger\Message;
  4. use App\Dto\Messenger\OutboxDto;
  5. use App\Dto\Messenger\OutboxPostDto;
  6. use App\Entity\Admin\CustomerUser;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Bundle\SecurityBundle\Security;
  12. class MessengerVoter extends Voter
  13. {
  14.     final public const EDIT 'MSG_ENTITY_EDIT';
  15.     final public const VIEW 'MSG_ENTITY_VIEW';
  16.     final public const DELETE 'MSG_ENTITY_DELETE';
  17.     
  18.     public function __construct(
  19.             private readonly Security $security)
  20.     {
  21.     }
  22.     
  23.     protected function supports(string $attributemixed $subject): bool
  24.     {
  25.         if(!in_array($attribute, [self::EDITself::VIEWself::DELETE])){
  26.             return false;
  27.         }
  28.         if ($subject == null){
  29.             return true;
  30.         }
  31.         
  32.         if (!$subject instanceof Message and !$subject instanceof OutboxDto and !$subject instanceof OutboxPostDto) {
  33.             return false;
  34.         }
  35.       
  36.         return true;
  37.     }
  38.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  39.     {
  40.         $user $token->getUser();
  41.         // if the user is anonymous, do not grant access
  42.         if (!$user instanceof UserInterface) {
  43.             return false;
  44.         }
  45.         if (!$this->security->isGranted('ROLE_STAFF')){
  46.             return false;
  47.         }
  48.      
  49.         if($subject instanceof OutboxDto){
  50.             return true;
  51.         }
  52.      
  53.         // ... (check conditions and return true to grant permission) ...
  54.        return match($attribute) {
  55.             self::VIEW => $this->canView($subject$user),
  56.             self::EDIT => $this->canEdit($subject$user),
  57.             self::DELETE => $this->canDelete($subject$user),
  58.             default => throw new \LogicException('This code should not be reached!')
  59.         };
  60.     }
  61.     
  62.     private function canView(Message $entityCustomerUser $user): bool
  63.     {
  64.         if($entity->isDeleted()){
  65.             return false;
  66.         }
  67.        
  68.         if ($entity->getStaff()->getId() == $user->getStaff()) {
  69.             return true;
  70.         }
  71.  
  72.         return false;
  73.     }
  74.     private function canEdit(?Message $entityCustomerUser $user): bool
  75.     {
  76.         if ($entity == null) {
  77.             return true;
  78.         }
  79.        
  80.         if ($entity->getStaff() == null) {
  81.             return true;
  82.         }
  83.         
  84.         if ($entity->getStaff()->getId() == $user->getStaff()) {
  85.             return true;
  86.         }
  87.      ;  
  88.         return false;
  89.     }
  90.     
  91.     private function canDelete(Message $entityCustomerUser $user): bool
  92.     {
  93.         if ($entity->getStaff()->getId() == $user->getStaff()) {
  94.             return true;
  95.         }
  96.         return false;
  97.     }
  98. }