mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-16 05:14:35 +02:00
Added button to user admin to impersonate a certain user
This commit is contained in:
parent
d20153c569
commit
cc1595e048
6 changed files with 145 additions and 25 deletions
|
@ -47,7 +47,9 @@ use App\Entity\LogSystem\AbstractLogEntry;
|
|||
use App\Repository\LogEntryRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
@ -59,7 +61,9 @@ final class UserExtension extends AbstractExtension
|
|||
{
|
||||
private readonly LogEntryRepository $repo;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, private readonly Security $security)
|
||||
public function __construct(EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly UrlGeneratorInterface $urlGenerator)
|
||||
{
|
||||
$this->repo = $em->getRepository(AbstractLogEntry::class);
|
||||
}
|
||||
|
@ -80,6 +84,7 @@ final class UserExtension extends AbstractExtension
|
|||
new TwigFunction('creating_user', fn(AbstractDBElement $element): ?User => $this->repo->getCreatingUser($element)),
|
||||
new TwigFunction('impersonator_user', $this->getImpersonatorUser(...)),
|
||||
new TwigFunction('impersonation_active', $this->isImpersonationActive(...)),
|
||||
new TwigFunction('impersonation_path', $this->getImpersonationPath(...)),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -103,6 +108,15 @@ final class UserExtension extends AbstractExtension
|
|||
return $this->security->isGranted('IS_IMPERSONATOR');
|
||||
}
|
||||
|
||||
public function getImpersonationPath(User $user, string $route_name = 'homepage'): string
|
||||
{
|
||||
if (! $this->security->isGranted('CAN_SWITCH_USER', $user)) {
|
||||
throw new AccessDeniedException('You are not allowed to impersonate this user!');
|
||||
}
|
||||
|
||||
return $this->urlGenerator->generate($route_name, ['_switch_user' => $user->getUsername()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function/filter generates a path.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue