src/Security/Voter/UserVoter.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Controller\Api\V1\Song\SearchFromMmdz;
  4. use App\Entity\Profile\Profile;
  5. use App\Enums\Constants;
  6. use App\Repository\Profile\ProfileRepository;
  7. use App\Service\AuthenticationService;
  8. use App\Service\GeneralService;
  9. use App\Service\ProfileService;
  10. use FontLib\Table\Type\nameRecord;
  11. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. class UserVoter extends Voter
  16. {
  17.     private const SERVICE_ADMIN_ACCESS 'SERVICE_ADMIN_ACCESS';
  18.     private const SONGWRITER_USER_CAN_CREATE_SONG 'SONGWRITER_USER_CAN_CREATE_SONG';
  19.     /** @var GeneralService */
  20.     private $generalService;
  21.     /**
  22.      * @var AuthenticationService
  23.      */
  24.     private $authenticationService;
  25.     /**
  26.      * @var ProfileService
  27.      */
  28.     private $profileService;
  29.     /** @var ProfileRepository */
  30.     private $profileRepository;
  31.     public function __construct(
  32.         GeneralService $generalService,
  33.         AuthenticationService $authenticationService,
  34.         ProfileService $profileService,
  35.         ProfileRepository $profileRepository
  36.     )
  37.     {
  38.         $this->generalService $generalService;
  39.         $this->authenticationService $authenticationService;
  40.         $this->profileService $profileService;
  41.         $this->profileRepository $profileRepository;
  42.     }
  43.     protected function supports($attribute$subject)
  44.     {
  45.         return in_array($attribute, [self::SERVICE_ADMIN_ACCESSself::SONGWRITER_USER_CAN_CREATE_SONG]);
  46.     }
  47.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  48.     {
  49.         $user $token->getUser();
  50.         // if the user is anonymous, do not grant access
  51.         if (!$user instanceof UserInterface) {
  52.             return false;
  53.         }
  54.         // ... (check conditions and return true to grant permission) ...
  55.         switch ($attribute) {
  56.             case self::SERVICE_ADMIN_ACCESS:
  57.                 return $this->authenticationService->userIsMmpzServiceAdmin();
  58.             case self::SONGWRITER_USER_CAN_CREATE_SONG:
  59.                 /** @var Profile $profile */
  60.                 $profile $this->generalService->findProfileForProcess(
  61.                     $this->generalService->findProfileTypeByName(Constants::PROFILE_TYPE_SONGWRITER),
  62.                     $this->generalService->findTheCurrentUser()
  63.                 );
  64.                 $profileHasInactiveSubscription = !($_ENV['CWR_ENVIROMENT'] === 'local') && $this->profileService->profielHasInactivePacakgeSubcription($profile);
  65.                 if($profileHasInactiveSubscription){
  66.                     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.");
  67.                 }
  68.                 $canSongwriterAddOtherComposition = ($_ENV['CWR_ENVIROMENT'] === 'local') || $this->profileService->canSongwriterAddOtherComposition();
  69.                 if(!$canSongwriterAddOtherComposition){
  70.                     throw new BadRequestHttpException("Please check the package or Subscription composition limit");
  71.                 }
  72.                 $canCreateSong = ($_ENV['CWR_ENVIROMENT'] === 'local') || $this->profileService->userIsSongwriterCanCreateSong();
  73.                 return ($canSongwriterAddOtherComposition && $canCreateSong && !$profileHasInactiveSubscription);
  74.         }
  75.         return false;
  76.     }
  77. }