src/Security/Voter/SongVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Profile\Profile;
  4. use App\Entity\Song\Song;
  5. use App\Entity\User\User;
  6. use App\Repository\Profile\ProfileRepository;
  7. use App\Service\AuthenticationService;
  8. use App\Service\ProfileService;
  9. use App\Service\SongService;
  10. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. class SongVoter extends Voter
  15. {
  16.     private const SONG_ACCESS 'SONG_ACCESS';
  17.     private const SONG_EDIT 'SONG_EDIT';
  18.     /**
  19.      * @var SongService
  20.      */
  21.     private $songService;
  22.     /** @var ProfileService */
  23.     private $profileService;
  24.     /** @var AuthenticationService */
  25.     private $authenticationService;
  26.     /** @var ProfileRepository */
  27.     private $profileRepository;
  28.     public function __construct(
  29.         SongService $songService,
  30.         ProfileService $profileService,
  31.         AuthenticationService $authenticationService,
  32.         ProfileRepository $profileRepository
  33.     )
  34.     {
  35.         $this->songService $songService;
  36.         $this->profileService $profileService;
  37.         $this->authenticationService $authenticationService;
  38.         $this->profileRepository $profileRepository;
  39.     }
  40.     protected function supports($attribute$subject)
  41.     {
  42.         return in_array($attribute, [self::SONG_ACCESSself::SONG_EDIT], true)
  43.             && $subject instanceof Song;
  44.     }
  45.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  46.     {
  47.         /** @var User $user */
  48.         $user $token->getUser();
  49.         if($_ENV['CWR_ENVIROMENT'] !== 'local'){
  50.             /** @var Profile $profile */
  51.             $profile $this->profileService->findLastAccessedProfileForUser($this->authenticationService->getUser());
  52.         }else{
  53.             /** @var Profile $profile */
  54.             $profile $this->profileRepository->findOneBy(['urlName' => $_ENV['LOCAL_PUBLISHER_ADMINISTRATOR_PROFILE_URL']]);
  55.         }
  56.         // if the user is anonymous, do not grant access
  57.         if (!$user instanceof UserInterface) {
  58.             return false;
  59.         }
  60.         /*if($this->authenticationService->userIsMmpzServiceAdmin($user)){
  61.             return true;
  62.         }*/
  63.         switch ($attribute) {
  64.             case self::SONG_ACCESS:
  65.                 $profileHasInactiveSubscription $this->profileService->profielHasInactivePacakgeSubcription($profile);
  66.                 if($profileHasInactiveSubscription){
  67.                     throw new BadRequestHttpException("The status of your subscription is currently Inactive. This means we have not yet received payment and you will not be able to upload any songs. Please contact publishing@horusmusic.global to discuss this and request an invoice to make a direct payment.");
  68.                 }
  69.                 $hasActivePackage $this->profileService->profielHasActivePacakgeSubcription($profile);
  70.                 if(!$hasActivePackage){
  71.                     throw new BadRequestHttpException("This profile doesn't have a package or subscription active");
  72.                 }
  73.                 $canAccess $this->songService->userCanAccessSong($user$subject);
  74.                 return ($canAccess && $hasActivePackage && !$profileHasInactiveSubscription);
  75.             case self::SONG_EDIT:
  76.                 $profileHasInactiveSubscription $this->profileService->profielHasInactivePacakgeSubcription($profile);
  77.                 if($profileHasInactiveSubscription){
  78.                     throw new BadRequestHttpException("The status of your subscription is currently Inactive. This means we have not yet received payment and you will not be able to upload any songs. Please contact publishing@horusmusic.global to discuss this and request an invoice to make a direct payment.");
  79.                 }
  80.                 $hasActivePackage $this->profileService->profielHasActivePacakgeSubcription($profile);
  81.                 if(!$hasActivePackage){
  82.                     throw new BadRequestHttpException("This profile doesn't have a package or subscription active");
  83.                 }
  84.                 $canEdit $this->songService->userCanEditSong($user$subject);
  85.                 return ($canEdit && $hasActivePackage && !$profileHasInactiveSubscription);
  86.         }
  87.         return false;
  88.     }
  89. }