<?php
namespace App\Voters;
use App\Entity\Clinic\Stock\TreatmentClinic;
use App\Entity\Person\Admin;
use App\Entity\Person\SuperClinicAdmin;
use App\Entity\Person\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class TreatmentVoter extends OrganisationVoter
{
protected function supports($attribute, $subject)
{
return $subject instanceof TreatmentClinic && $attribute == self::EDIT;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var User $user */
$user = $token->getUser();
if ($user instanceof Admin) {
return true;
}
if ($user instanceof SuperClinicAdmin) {
/* @var TreatmentClinic $subject */
return parent::voteOnAttribute(self::VIEW, $subject->getClinic()->getOrganisation(), $token);
} else {
return $user->getClinic() === $subject->getClinic();
}
}
}