<?php
namespace App\Security\Voter;
use App\Entity\CompanyDemand;
use App\Entity\CompanyFile;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CompanyFileVoter extends Voter
{
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['COMPANY_FILE_REMOVE'], true)
&& $subject instanceof CompanyFile;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof User) {
return false;
}
/** @var CompanyDemand $companyFile */
$companyFile = $subject;
$company = $companyFile
->getCompany()
;
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'COMPANY_FILE_REMOVE':
// return true or false
return
$company->getUser() === $user ||
in_array('ROLE_ADMIN', $user->getRoles(), true);
}
return false;
}
}