<?php
namespace App\Security\Voter;
use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class UserVoter extends Voter
{
protected function supports($attribute, $subject)
{
// dump([
// "supports" => $attribute,
// 'subject' => $subject,
// ]);
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [Permission::EA_ACCESS_ENTITY])
&& $subject->getInstance() instanceof User;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case Permission::EA_ACCESS_ENTITY:
// dump([
// "voteOnAttribute" => $attribute,
// 'subject' => $subject,
// 'token' => $token,
// ]);
switch ($subject->getFqcn()) {
case User::class:
// User can access his own record
return $subject->getInstance() == $user;
}
break;
}
throw new \LogicException('This code should not be reached!');
}
}