<?php
namespace App\Security\Voter;
use App\Entity\User;
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(string $attribute, mixed $subject): bool
{
return in_array($attribute, ['USER_EDIT', 'USER_VIEW', 'USER_POST', 'USER_DELETE'])
&& $subject instanceof \App\Entity\User;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
//return true;
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'USER_EDIT':
case 'USER_POST':
case 'USER_DELETE':
if ($user->getProfile() === 0) {
return true;
}
break;
case 'USER_VIEW':
if ($user->getProfile() <= 1) {
return true;
}
break;
}
return false;
}
}