src/App/Security/Voter/BelongUserVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Security\Model\BelongUser;
  5. use App\Traits\UserTrait;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. class BelongUserVoter extends AbstractVoter {
  8.     use UserTrait;
  9.     
  10.     public const BELONG 'BELONG';
  11.     
  12.     protected function getAttributes(): array {
  13.         return [
  14.             self::BELONG
  15.         ];
  16.     }
  17.     
  18.     protected function getEntityClass(): string {
  19.         return BelongUser::class;
  20.     }
  21.     
  22.     /**
  23.      * @param string $attribute
  24.      * @param BelongUser $subject
  25.      * @param TokenInterface $token
  26.      * @return bool
  27.      */
  28.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) {
  29.         
  30.         $user $token->getUser();
  31.         if (
  32.             $user instanceof User && 
  33.             $subject->getUser()->getId() == $user->getId()
  34.         ) {
  35.             return self::ACCESS_GRANTED;
  36.         }
  37.         return self::ACCESS_ABSTAIN;
  38.     }
  39. }