<?php
namespace App\Security\Voter;
use App\Entity\Profile\Profile;
use App\Entity\Song\Song;
use App\Entity\User\User;
use App\Repository\Profile\ProfileRepository;
use App\Service\AuthenticationService;
use App\Service\ProfileService;
use App\Service\SongService;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class SongVoter extends Voter
{
private const SONG_ACCESS = 'SONG_ACCESS';
private const SONG_EDIT = 'SONG_EDIT';
/**
* @var SongService
*/
private $songService;
/** @var ProfileService */
private $profileService;
/** @var AuthenticationService */
private $authenticationService;
/** @var ProfileRepository */
private $profileRepository;
public function __construct(
SongService $songService,
ProfileService $profileService,
AuthenticationService $authenticationService,
ProfileRepository $profileRepository
)
{
$this->songService = $songService;
$this->profileService = $profileService;
$this->authenticationService = $authenticationService;
$this->profileRepository = $profileRepository;
}
protected function supports($attribute, $subject)
{
return in_array($attribute, [self::SONG_ACCESS, self::SONG_EDIT], true)
&& $subject instanceof Song;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var User $user */
$user = $token->getUser();
if($_ENV['CWR_ENVIROMENT'] !== 'local'){
/** @var Profile $profile */
$profile = $this->profileService->findLastAccessedProfileForUser($this->authenticationService->getUser());
}else{
/** @var Profile $profile */
$profile = $this->profileRepository->findOneBy(['urlName' => $_ENV['LOCAL_PUBLISHER_ADMINISTRATOR_PROFILE_URL']]);
}
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
/*if($this->authenticationService->userIsMmpzServiceAdmin($user)){
return true;
}*/
switch ($attribute) {
case self::SONG_ACCESS:
$profileHasInactiveSubscription = $this->profileService->profielHasInactivePacakgeSubcription($profile);
if($profileHasInactiveSubscription){
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.");
}
$hasActivePackage = $this->profileService->profielHasActivePacakgeSubcription($profile);
if(!$hasActivePackage){
throw new BadRequestHttpException("This profile doesn't have a package or subscription active");
}
$canAccess = $this->songService->userCanAccessSong($user, $subject);
return ($canAccess && $hasActivePackage && !$profileHasInactiveSubscription);
case self::SONG_EDIT:
$profileHasInactiveSubscription = $this->profileService->profielHasInactivePacakgeSubcription($profile);
if($profileHasInactiveSubscription){
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.");
}
$hasActivePackage = $this->profileService->profielHasActivePacakgeSubcription($profile);
if(!$hasActivePackage){
throw new BadRequestHttpException("This profile doesn't have a package or subscription active");
}
$canEdit = $this->songService->userCanEditSong($user, $subject);
return ($canEdit && $hasActivePackage && !$profileHasInactiveSubscription);
}
return false;
}
}