src/Security/Voter/SongWriterVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Composition\SongWriter;
  4. use App\Entity\User\User;
  5. use App\Service\CompositionService;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class SongWriterVoter extends Voter
  10. {
  11.     private const SONG_WRITER_EDIT 'SONG_WRITER_EDIT';
  12.     /**
  13.      * @var CompositionService
  14.      */
  15.     private $compositionService;
  16.     public function __construct(CompositionService $compositionService)
  17.     {
  18.         $this->compositionService $compositionService;
  19.     }
  20.     protected function supports($attribute$subject)
  21.     {
  22.         return in_array($attribute, [self::SONG_WRITER_EDIT], true)
  23.             && $subject instanceof SongWriter;
  24.     }
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  26.     {
  27.         /** @var User $user */
  28.         $user $token->getUser();
  29.         // if the user is anonymous, do not grant access
  30.         if (!$user instanceof UserInterface) {
  31.             return false;
  32.         }
  33.         switch ($attribute) {
  34.             // If user can edit a composition, it can also edit a songwriter
  35.             case self::SONG_WRITER_EDIT:
  36.                 return $this->compositionService->userCanEditComposition($user$subject->getComposition());
  37.         }
  38.         return false;
  39.     }
  40. }