src/Security/Voter/CompositionVoter.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Composition\Composition;
  4. use App\Entity\Package\PackageSongWriter;
  5. use App\Entity\Profile\Profile;
  6. use App\Entity\User\User;
  7. use App\Enums\Constants;
  8. use App\Enums\PackageSongwriterStatus;
  9. use App\Repository\Package\PackageSongwriterRepository;
  10. use App\Repository\Profile\ProfileRepository;
  11. use App\Service\AuthenticationService;
  12. use App\Service\CompositionService;
  13. use App\Service\ProfileService;
  14. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. class CompositionVoter extends Voter
  19. {
  20.     private const COMPOSITION_ACCESS 'COMPOSITION_ACCESS';
  21.     private const COMPOSITION_EDIT 'COMPOSITION_EDIT';
  22.     private const COMPOSITION_DELETE 'COMPOSITION_DELETE';
  23.     /**
  24.      * @var CompositionService
  25.      */
  26.     private $compositionService;
  27.     /** @var ProfileService */
  28.     private $profileService;
  29.     /** @var AuthenticationService */
  30.     private $authenticationService;
  31.     /** @var ProfileRepository */
  32.     private $profileRepository;
  33.     public function __construct(
  34.         CompositionService $compositionService,
  35.         ProfileService $profileService,
  36.         AuthenticationService $authenticationService,
  37.         ProfileRepository $profileRepository
  38.     )
  39.     {
  40.         $this->compositionService $compositionService;
  41.         $this->profileService $profileService;
  42.         $this->authenticationService $authenticationService;
  43.         $this->profileRepository $profileRepository;
  44.     }
  45.     protected function supports($attribute$subject)
  46.     {
  47.         return in_array($attribute, [
  48.             self::COMPOSITION_ACCESS,
  49.             self::COMPOSITION_EDIT,
  50.             self::COMPOSITION_DELETE
  51.         ], true) && $subject instanceof Composition;
  52.     }
  53.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  54.     {
  55.         /** @var User $user */
  56.         $user $token->getUser();
  57.         if($_ENV['CWR_ENVIROMENT'] !== 'local'){
  58.             /** @var Profile $profile */
  59.             $profile $this->profileService->findLastAccessedProfileForUser($this->authenticationService->getUser());
  60.         }else{
  61.             /** @var Profile $profile */
  62.             $profile $this->profileRepository->findOneBy(['urlName' => "colita-telerin"]);
  63.         }
  64.         // if the user is anonymous, do not grant access
  65.         if (!$user instanceof UserInterface) {
  66.             return false;
  67.         }
  68.         /*if($this->authenticationService->userIsMmpzServiceAdmin($user)){
  69.             return true;
  70.         }*/
  71.         switch ($attribute) {
  72.             case self::COMPOSITION_ACCESS:
  73.                 $profileHasInactiveSubscription $this->profileService->profielHasInactivePacakgeSubcription($profile);
  74.                 if($profileHasInactiveSubscription){
  75.                     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.");
  76.                 }
  77.                 $hasActivePackage $this->profileService->profielHasActivePacakgeSubcription($profile);
  78.                 if(!$hasActivePackage){
  79.                     throw new BadRequestHttpException("This profile doesn't have a package or subscription active");
  80.                 }
  81.                 $canAccess $this->compositionService->userCanAccessComposition($user$subject);
  82.                 return ($canAccess && $hasActivePackage && !$profileHasInactiveSubscription);
  83.             case self::COMPOSITION_EDIT:
  84.                 if(($_ENV['CWR_ENVIROMENT'] === 'local')){
  85.                     return true;
  86.                 }
  87.                 $profileHasInactiveSubscription $this->profileService->profielHasInactivePacakgeSubcription($profile);
  88.                 if($profileHasInactiveSubscription){
  89.                     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.");
  90.                 }
  91.                 $hasActivePackage $this->profileService->profielHasActivePacakgeSubcription($profile);
  92.                 if(!$hasActivePackage){
  93.                     throw new BadRequestHttpException("This profile doesn't have a package or subscription active");
  94.                 }
  95.                 $canEdit $this->compositionService->userCanEditComposition($user$subject);
  96.                 return ($canEdit && $hasActivePackage && !$profileHasInactiveSubscription);
  97.             case self::COMPOSITION_DELETE:
  98.                 return $this->compositionService->userCanDeleteComposition($user$subject);
  99.         }
  100.         return false;
  101.     }
  102. }