src/Security/Voter/TransactionVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Enum\RoleEnum;
  4. use App\Entity\RoleManagement;
  5. use App\Entity\Transaction;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class TransactionVoter extends Voter
  10. {
  11.     public function __construct(private Security $security)
  12.     {}
  13.     protected function supports($attribute$subject): bool
  14.     {
  15.         $supportsAttribute in_array($attribute, ['TRANSACTION_CREATE''TRANSACTION_READ''TRANSACTION_EDIT''TRANSACTION_DELETE']);
  16.         $supportsSubject $subject instanceof Transaction;
  17.         return $supportsAttribute && $supportsSubject;
  18.     }
  19.     /**
  20.      * @param string $attribute
  21.      * @param Transaction $subject
  22.      * @param TokenInterface $token
  23.      * @return bool
  24.      */
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  26.     {
  27.         if($this->security->isGranted(RoleEnum::SUPER_ADMIN->value)) return true;
  28.         // TODO belongs to user
  29.         // refactor api
  30.         return false;
  31.     }
  32. }