src/Security/Voter/User/AccountVoter.php line 10

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