<?php
namespace App\Security\Voter;
use App\Entity\Magasin;
use App\Entity\UserAccess;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class MagasinVoter extends AbstractVoter
{
public const VIEW = 'Magasin:View';
public const EDIT = 'Magasin:Edit';
private ?Magasin $magasin = null;
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::EDIT, self::VIEW])
&& $subject instanceof Magasin;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$this->user = $token->getUser();
$this->magasin = $subject;
return match ($attribute) {
self::VIEW => $this->canViewMagasin(),
self::EDIT => $this->canEditMagasin(),
default => false,
};
}
private function canViewMagasin(): bool
{
// Read-only superadmins can view all magasins
if ($this->isReadOnlySuperAdmin()) {
return true;
}
if ($this->isSuperAdmin()) {
return true;
}
if ($this->user->canAccessResource($this->magasin->getGroupement(), [UserAccess::ROLE_ADMIN])) {
return true;
}
return $this->isAdmin()
&& $this->user->canAccessResource($this->magasin, [UserAccess::ROLE_ADMIN]);
}
private function canEditMagasin(): bool
{
// Only full superadmins can edit, not read-only
return parent::canEdit();
}
}