<?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\Security;
class UserVoter extends Voter
{
const MANAGE = 'manage';
public function __construct(private readonly Security $security)
{
}
protected function supports(string $attribute, $subject): bool
{
if ($attribute !== self::MANAGE) {
return false;
}
if (!$subject instanceof User) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
if($this->security->isGranted('ROLE_SUPPORT')) {
return true;
}
/** @var User $patient */
$patient = $subject;
if ($this->security->isGranted('ROLE_DOCTOR') || $this->security->isGranted('ROLE_ESTABLISHMENT_ADMIN')) {
return $user->getEstablishment()->getId() === $patient->getEstablishment()->getId();
}
return false;
}
}