src/Security/Voter/GuestbookEntryVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\GuestbookEntry;
  4. use App\Security\ApiUser;
  5. use App\Service\User\MemberOperatorService;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class GuestbookEntryVoter extends Voter
  9. {
  10.     public const COMMENT 'comment';
  11.     public const MANAGE 'manage';
  12.     public const DELETE 'delete';
  13.     protected function supports(string $attribute$subject): bool
  14.     {
  15.         if (!$subject instanceof GuestbookEntry) {
  16.             return false;
  17.         }
  18.         return in_array($attribute, [
  19.             self::COMMENT,
  20.             self::MANAGE,
  21.             self::DELETE,
  22.         ], true);
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof ApiUser) {
  28.             return false;
  29.         }
  30.         // admin should be able to do anything
  31.         if ($user->getIsAdmin()) {
  32.             return true;
  33.         }
  34.         /*
  35.          * @var $subject GuestbookEntry
  36.          */
  37.         if ($user->getIsOperator()) {
  38.             return MemberOperatorService::isMemberOperatedByUser($subject->getRecipient(), $user);
  39.         }
  40.         return $user->getMember()->getId() == $subject->getRecipient()->getId();
  41.     }
  42. }