src/Security/Voter/SwitchToUserVoter.php line 9
<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class SwitchToUserVoter extends Voter
{
protected function supports($attribute, $subject): bool
{
return in_array($attribute, ['CAN_SWITCH_USER'])
&& $subject instanceof UserInterface;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface || !$subject instanceof UserInterface) {
return false;
}
if ($subject->getCreator() != $user) {
$checkUserAncestor = function($userRecord) use (&$checkUserAncestor) {
if ($userRecord->getCreator()) {
return $checkUserAncestor($userRecord->getCreator());
} else {
return $userRecord;
}
};
if ($checkUserAncestor($subject) == $user) {
return true;
}
return $user->getControlling()->includes($subject);
}
return true;
}
}