<?php
namespace Bidcoz\Bundle\CoreBundle\Security\Authorization\Voter;
use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
use Bidcoz\Bundle\CoreBundle\Entity\PaymentGateway\Account\Account;
use Bidcoz\Bundle\CoreBundle\Entity\User;
use Bidcoz\Bundle\CoreBundle\Services\CreditCardManager;
use RS\DiExtraBundle\Annotation as DI;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* @DI\Service
* @DI\Tag("security.voter")
*/
class RequireCcVoter extends Voter
{
const WITH_CC = 'WITH_CC';
protected $ccManager;
/**
* @DI\InjectParams({
* "ccManager" = @DI\Inject("credit_card_manager"),
* })
*/
public function __construct(CreditCardManager $ccManager)
{
$this->ccManager = $ccManager;
}
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (self::WITH_CC !== $attribute) {
return false;
}
// only vote on Campaign objects inside this voter
if (!$subject instanceof Campaign) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param Campaign $campaign
*
* @return bool
*/
protected function voteOnAttribute($attribute, $campaign, TokenInterface $token)
{
$organization = $campaign->getOrganization();
//Stripe not enabled
if (!$organization->isStripeAllowed() || !$organization->hasPaymentGatewayAccountType(Account::STRIPE)) {
return true;
}
//Advance CC is not required
if (!$campaign->getCollectCcInfo()) {
return true;
}
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
return (bool) $this->ccManager->getCC($campaign, $user);
}
}