<?php
namespace Bidcoz\Bundle\CoreBundle\Security\Authorization\Voter;
use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
use Bidcoz\Bundle\CoreBundle\Services\OrganizationManager;
use Bidcoz\Bundle\CoreBundle\Services\PermissionManager;
use RS\DiExtraBundle\Annotation as DI;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @DI\Service
* @DI\Tag("security.voter")
*/
class HiddenCampaignVoter extends Voter
{
public const FRONT_END = 'FRONT_END';
protected OrganizationManager $organizationManager;
protected PermissionManager $permissionManager;
private RequestStack $requestStack;
/**
* @DI\InjectParams({
* "organizationManager" = @DI\Inject("organization_manager"),
* "permissionManager" = @DI\Inject("permission_manager"),
* "requestStack" = @DI\Inject("request_stack")
* })
*/
public function __construct(
OrganizationManager $organizationManager,
PermissionManager $permissionManager,
RequestStack $requestStack
)
{
$this->organizationManager = $organizationManager;
$this->permissionManager = $permissionManager;
$this->requestStack = $requestStack;
}
protected function supports($attribute, $subject)
{
return $subject instanceof Campaign && self::FRONT_END === $attribute;
}
protected function voteOnAttribute($attribute, $campaign, TokenInterface $token)
{
/** @var $campaign Campaign */
if (!$campaign->isHidden()) {
return true;
}
$route = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->get('_route') : null;
if ('campaign_register_user' === $route) {
return true;
}
$user = $token->getUser();
// If the user isn't logged in, denied
if (!$user instanceof UserInterface) {
return false;
}
if ($user->hasRole('ROLE_SUPER_ADMIN')) {
return true;
}
if ($this->organizationManager->isOrganizationAdmin($campaign->getOrganization(), $user)) {
return true;
}
$memberships = $this
->permissionManager
->getUserGroupMembershipForCampaign($campaign, $user);
// allow access to any user that has any permissions
return count($memberships) > 0;
}
}