<?php
namespace App\Security\Voter;
use App\Entity\BackUser;
use App\Utils\VoterHelper;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class BackUserVoter extends Voter
{
private const EDIT = 'edit';
private const CREATE = 'create';
private const DELETE = 'delete';
private const RESET_PASSWORD = 'reset-password';
private const SWITCH_STATUS = 'switch-status';
private const IMPERSONATE = 'impersonate';
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof BackUser && in_array($attribute, [self::EDIT, self::CREATE, self::DELETE, self::RESET_PASSWORD, self::SWITCH_STATUS, self::IMPERSONATE], true);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::IMPERSONATE:
if (!$subject->getEnabled()) {
return false;
}
break;
case self::SWITCH_STATUS:
if ($subject->getId() == $token->getUser()->getId()) {
return false;
}
break;
}
return VoterHelper::voteOnAttribute($attribute, $subject, $token, 'back_user');
}
}