src/Security/Voter/Customer/ScoreVoter.php line 14
<?php
namespace App\Security\Voter\Customer;
use App\Entity\Customer\Score;
use App\Entity\Customer\ScoreInvite;
use App\Entity\Admin\CustomerUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\SecurityBundle\Security;
class ScoreVoter extends Voter
{
final public const EDIT = 'SCORE_ENTITY_EDIT';
final public const VIEW = 'SCORE_ENTITY_VIEW';
final public const DELETE = 'SCORE_ENTITY_DELETE';
public function __construct(
private readonly Security $security
) {
}
protected function supports(string $attribute, mixed $subject): bool
{
if (!in_array($attribute, [self::EDIT, self::VIEW, self::DELETE])) {
return false;
}
if (!$subject instanceof Score) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if (!$this->security->isGranted('ROLE_STAFF')) {
return false;
}
// ... (check conditions and return true to grant permission) ...
return match ($attribute) {
self::VIEW => $this->canView($subject, $user),
self::EDIT => $this->canEdit($subject),
self::DELETE => $this->canDelete(),
default => throw new \LogicException('This code should not be reached!')
};
}
private function canView(Score $entity, CustomerUser $user): bool
{
if ($entity->getStaff()->getId() == $user->getStaff()) {
return true;
}
return false;
}
private function canEdit(?Score $entity): bool
{
if ($entity == null) {
return true;
}
if ($entity->getStaff() == null) {
return true;
}
return false;
}
private function canDelete(): bool
{
return false;
}
}