<?php
declare(strict_types=1);
namespace App\Security;
use App\Entity\Customer\Customer;
use App\Entity\User\AdminUser;
use LogicException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use function in_array;
class CustomerVoter extends Voter
{
public const SHOW = 'show';
public const UPDATE = 'update';
public const ARCHIVE = 'archive';
public function __construct(
private readonly RequestStack $requestStack,
) {
}
protected function supports(string $attribute, mixed $subject): bool
{
// if the attribute isn't one we support, return false
if (! in_array($attribute, [self::SHOW, self::UPDATE, self::ARCHIVE], true)) {
return false;
}
return $subject instanceof Customer;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$adminUser = $token->getUser();
if (! $adminUser instanceof AdminUser) {
// the user must be logged in; if not, deny access
return false;
}
/** @var Customer $customer */
$customer = $subject;
return match ($attribute) {
self::SHOW => $this->canShow($customer, $adminUser),
self::UPDATE => $this->canUpdate($customer, $adminUser),
self::ARCHIVE => $this->canArchive($customer, $adminUser),
default => throw new LogicException('This code should not be reached!')
};
}
private function canShow(Customer $customer, AdminUser $adminUser): bool
{
return $this->checkAccess($customer, $adminUser, self::SHOW);
}
private function canUpdate(Customer $customer, AdminUser $adminUser): bool
{
return $this->checkAccess($customer, $adminUser, self::UPDATE);
}
private function canArchive(Customer $customer, AdminUser $adminUser): bool
{
$loggedClinicalManager = $adminUser->getCustomer()?->getClinicalManager();
return $loggedClinicalManager === null ||
$customer->getDoctor() === null ||
$customer->getDoctor()->getAttachedClinicalManager()?->getId() === $loggedClinicalManager->getId();
}
public function checkAccess(Customer $customer, AdminUser $adminUser, string $attribute): bool
{
$loggedClinicalManager = $adminUser->getCustomer()?->getClinicalManager();
$request = $this->requestStack->getCurrentRequest();
if ($request === null) {
return false;
}
if ($request->get('_route') === 'app_admin_doctor_' . $attribute && $customer->getDoctor() !== null) {
if ($loggedClinicalManager === null) {
return true;
}
return $customer->getDoctor()->getAttachedClinicalManager()?->getId() === $loggedClinicalManager->getId();
}
return $request->get('_route') === 'app_admin_distributor_' . $attribute && $customer->getDistributor() !== null;
}
}