src/Security/Voter/Messenger/ChatTemplateVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Messenger;
  3. use App\Entity\ChatTemplate;
  4. use App\Entity\Member;
  5. use App\Security\ApiUser;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ChatTemplateVoter extends Voter
  9. {
  10.     public const DELETE 'delete';
  11.     public const EDIT 'edit';
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         if (!$subject instanceof ChatTemplate) {
  15.             return false;
  16.         }
  17.         return in_array($attribute, [self::DELETEself::EDIT], true);
  18.     }
  19.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  20.     {
  21.         $user $token->getUser();
  22.         if (!$user instanceof ApiUser) {
  23.             return false;
  24.         }
  25.         switch ($attribute) {
  26.             case self::EDIT:
  27.                 return $this->canEdit($subject$user->getMember());
  28.             case self::DELETE:
  29.                 return $this->canDelete($subject$user->getMember());
  30.         }
  31.         return false;
  32.     }
  33.     protected function canEdit(ChatTemplate $templateMember $user): bool
  34.     {
  35.         return $template->getMember()->getId() === $user->getId();
  36.     }
  37.     protected function canDelete(ChatTemplate $templateMember $user): bool
  38.     {
  39.         return $this->canEdit($template$user);
  40.     }
  41. }