<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class JobAdVoter extends Voter
{
public const MANAGE = 'JOB_MANAGE';
protected function supports(string $attribute, $jobad): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::MANAGE])
&& $jobad instanceof \App\Entity\JobAd;
}
protected function voteOnAttribute(string $attribute, $jobad, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if ($this->canManage($jobad, $user)) {
return true;
}
return false;
}
protected function canManage($jobad, $user) {
return $user === $jobad->getUser();
}
}