<?php
namespace App\Security\Voter;
use App\Controller\Api\V1\Song\SearchFromMmdz;
use App\Entity\Profile\Profile;
use App\Enums\Constants;
use App\Repository\Profile\ProfileRepository;
use App\Service\AuthenticationService;
use App\Service\GeneralService;
use App\Service\ProfileService;
use FontLib\Table\Type\nameRecord;
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 UserVoter extends Voter
{
private const SERVICE_ADMIN_ACCESS = 'SERVICE_ADMIN_ACCESS';
private const SONGWRITER_USER_CAN_CREATE_SONG = 'SONGWRITER_USER_CAN_CREATE_SONG';
/** @var GeneralService */
private $generalService;
/**
* @var AuthenticationService
*/
private $authenticationService;
/**
* @var ProfileService
*/
private $profileService;
/** @var ProfileRepository */
private $profileRepository;
public function __construct(
GeneralService $generalService,
AuthenticationService $authenticationService,
ProfileService $profileService,
ProfileRepository $profileRepository
)
{
$this->generalService = $generalService;
$this->authenticationService = $authenticationService;
$this->profileService = $profileService;
$this->profileRepository = $profileRepository;
}
protected function supports($attribute, $subject)
{
return in_array($attribute, [self::SERVICE_ADMIN_ACCESS, self::SONGWRITER_USER_CAN_CREATE_SONG]);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case self::SERVICE_ADMIN_ACCESS:
return $this->authenticationService->userIsMmpzServiceAdmin();
case self::SONGWRITER_USER_CAN_CREATE_SONG:
/** @var Profile $profile */
$profile = $this->generalService->findProfileForProcess(
$this->generalService->findProfileTypeByName(Constants::PROFILE_TYPE_SONGWRITER),
$this->generalService->findTheCurrentUser()
);
$profileHasInactiveSubscription = !($_ENV['CWR_ENVIROMENT'] === 'local') && $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.");
}
$canSongwriterAddOtherComposition = ($_ENV['CWR_ENVIROMENT'] === 'local') || $this->profileService->canSongwriterAddOtherComposition();
if(!$canSongwriterAddOtherComposition){
throw new BadRequestHttpException("Please check the package or Subscription composition limit");
}
$canCreateSong = ($_ENV['CWR_ENVIROMENT'] === 'local') || $this->profileService->userIsSongwriterCanCreateSong();
return ($canSongwriterAddOtherComposition && $canCreateSong && !$profileHasInactiveSubscription);
}
return false;
}
}