src/Security/Voter/User/OperatorVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\User;
  3. use App\Entity\Member;
  4. use App\Security\ApiUser;
  5. use App\Service\Messenger\MessengerPropertyService;
  6. use App\Service\Property\MemberPropertyService;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class OperatorVoter extends Voter
  10. {
  11.     public const READ 'operator_read';
  12.     public const GUESTBOOK 'operator_guestbook';
  13.     public const STATS 'operator_stats';
  14.     protected MemberPropertyService $propertyService;
  15.     public function __construct(MemberPropertyService $propertyService)
  16.     {
  17.         $this->propertyService $propertyService;
  18.     }
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         if (!$subject instanceof Member) {
  22.             return false;
  23.         }
  24.         return in_array($attribute, [
  25.             self::READ,
  26.             self::GUESTBOOK,
  27.             self::STATS,
  28.         ], true);
  29.     }
  30.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  31.     {
  32.         $user $token->getUser();
  33.         if (!$user instanceof ApiUser || !$user->getIsOperator()) {
  34.             return false;
  35.         }
  36.         /*
  37.          * @var $subject Member
  38.          */
  39.         switch ($attribute) {
  40.             case self::STATS:
  41.                 $prop MessengerPropertyService::PROPERTY_MESSENGER_OPERATOR_AMATEUR_STATS_ENABLED;
  42.                 if (!(bool) $this->propertyService->getValueForMember($user->getMember(), $prop)) {
  43.                     return false;
  44.                 }
  45.                 // no break
  46.             case self::GUESTBOOK:
  47.                 $prop MessengerPropertyService::PROPERTY_MESSENGER_OPERATOR_AMATEUR_GUESTBOOK_ENABLED;
  48.                 if (!(bool) $this->propertyService->getValueForMember($user->getMember(), $prop)) {
  49.                     return false;
  50.                 }
  51.         }
  52.         return $user->getMember()->getId() === $subject->getId();
  53.     }
  54. }