src/Security/Voter/UserVoter.php line 11
<?phpnamespace App\Security\Voter;use App\Entity\User;use App\Enum\Admin\RolesEnum;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authorization\Voter\Voter;use Symfony\Component\Security\Core\User\UserInterface;class UserVoter extends Voter{public const EDIT = 'USER_EDIT';public const VIEW = 'USER_VIEW';/*** @param string $attribute* @param mixed $subject* @return bool*/protected function supports(string $attribute, mixed $subject): bool{return in_array($attribute, [self::EDIT, self::VIEW])&& $subject instanceof User;}/*** @param string $attribute* @param mixed $subject* @param TokenInterface $token* @return bool*/protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool{$user = $token->getUser();// if the user is anonymous, do not grant accessif (!$user instanceof UserInterface) {return false;}if (!$subject instanceof User) {return false;}switch ($attribute) {case self::EDIT:if (in_array(RolesEnum::ROLE_SUPER_ADMIN->name, $user->getRoles(), true)) {return true;}if (in_array(RolesEnum::ROLE_ADMIN->name, $user->getRoles(), true)&& !in_array(RolesEnum::ROLE_SUPER_ADMIN->name, $subject->getRoles(), true)) {return true;}break;case self::VIEW:return in_array(RolesEnum::ROLE_SUPER_ADMIN->name, $user->getRoles(), true)|| in_array(RolesEnum::ROLE_ADMIN->name, $user->getRoles(), true);}return false;}}