<?php
namespace App\Security;
use App\Entity\Vacancy;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ApplicationVoter extends Voter
{
public const AUTHORIZED_APPLY = 'authorized_apply';
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function supports($attribute, $subject)
{
if (!\in_array($attribute, [self::AUTHORIZED_APPLY], true)) {
return false;
}
return Vacancy::class === \get_class($subject);
}
/**
* @param string $attribute
* @param mixed $subject
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var Vacancy $vacancy */
$vacancy = $subject;
$domain = $vacancy->getDomain();
if (!$domain) {
return true;
}
switch ($attribute) {
case self::AUTHORIZED_APPLY:
if ($domain->getPublic()) {
return true;
}
if ($domain->getPrivate() && \in_array('ROLE_SITE_MOBILITY_USER', $token->getRoleNames(), true)) {
return true;
}
return false;
default:
return false;
}
}
}