<?php
namespace App\Security\Voter;
use App\Entity\Authentication\DeveloperApp;
use App\Entity\Authentication\Person;
use App\Entity\Vacancy\Vacancy;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class VacancyVoter extends Voter
{
public const EDIT = 'edit';
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [self::EDIT])
&& $subject instanceof Vacancy;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Person || !$user->getDeveloperApp() instanceof DeveloperApp) {
return false;
}
/** @var Vacancy $vacancy */
$vacancy = $subject;
switch ($attribute) {
case self::EDIT:
return $this->canEdit($vacancy, $user->getDeveloperApp());
}
return false;
}
private function canEdit(Vacancy $vacancy, DeveloperApp $provider)
{
return $provider === $vacancy->getProvider();
}
}