src/Bidcoz/Bundle/CoreBundle/Security/Authorization/Voter/RequireCcVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\CoreBundle\Security\Authorization\Voter;
  3. use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
  4. use Bidcoz\Bundle\CoreBundle\Entity\PaymentGateway\Account\Account;
  5. use Bidcoz\Bundle\CoreBundle\Entity\User;
  6. use Bidcoz\Bundle\CoreBundle\Services\CreditCardManager;
  7. use RS\DiExtraBundle\Annotation as DI;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. /**
  11.  * @DI\Service
  12.  * @DI\Tag("security.voter")
  13.  */
  14. class RequireCcVoter extends Voter
  15. {
  16.     const WITH_CC 'WITH_CC';
  17.     protected $ccManager;
  18.     /**
  19.      * @DI\InjectParams({
  20.      *      "ccManager" = @DI\Inject("credit_card_manager"),
  21.      * })
  22.      */
  23.     public function __construct(CreditCardManager $ccManager)
  24.     {
  25.         $this->ccManager $ccManager;
  26.     }
  27.     protected function supports($attribute$subject)
  28.     {
  29.         // if the attribute isn't one we support, return false
  30.         if (self::WITH_CC !== $attribute) {
  31.             return false;
  32.         }
  33.         // only vote on Campaign objects inside this voter
  34.         if (!$subject instanceof Campaign) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     /**
  40.      * @param string   $attribute
  41.      * @param Campaign $campaign
  42.      *
  43.      * @return bool
  44.      */
  45.     protected function voteOnAttribute($attribute$campaignTokenInterface $token)
  46.     {
  47.         $organization $campaign->getOrganization();
  48.         //Stripe not enabled
  49.         if (!$organization->isStripeAllowed() || !$organization->hasPaymentGatewayAccountType(Account::STRIPE)) {
  50.             return true;
  51.         }
  52.         //Advance CC is not required
  53.         if (!$campaign->getCollectCcInfo()) {
  54.             return true;
  55.         }
  56.         $user $token->getUser();
  57.         if (!$user instanceof User) {
  58.             // the user must be logged in; if not, deny access
  59.             return false;
  60.         }
  61.         return (bool) $this->ccManager->getCC($campaign$user);
  62.     }
  63. }