<?php
namespace Bidcoz\Bundle\CoreBundle\Security\Authorization\Voter;
use Bidcoz\Bundle\CoreBundle\Entity\Organization;
use Bidcoz\Bundle\CoreBundle\Services\PermissionManager;
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 OrganizationPaymentPlanVoter extends Voter
{
protected $permissionManager;
/**
* @DI\InjectParams({
* "permissionManager" = @DI\Inject("permission_manager"),
* })
*/
public function __construct(PermissionManager $permissionManager)
{
$this->permissionManager = $permissionManager;
}
protected function supports($attribute, $subject)
{
$orgVoterAttributes = [
OrganizationVoter::VIEW,
OrganizationVoter::MANAGE,
OrganizationVoter::EMAIL,
OrganizationVoter::CONTACTS,
OrganizationVoter::ADMIN,
];
return $subject instanceof Organization && !in_array($attribute, $orgVoterAttributes);
}
/**
* @param string $attribute
* @param Organization $organization
*/
protected function voteOnAttribute($attribute, $organization, TokenInterface $token): bool
{
$paymentPlan = $organization->getPaymentModel();
if ($this->permissionManager->isAllowedByPaymentPlan($attribute, $paymentPlan)) {
return true;
}
return false;
}
}