mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-08-03 01:34:34 +02:00
Merge branch 'user_impersonator'
This commit is contained in:
commit
2362835275
14 changed files with 381 additions and 45 deletions
|
@ -64,6 +64,7 @@ class SecurityEventLogEntry extends AbstractLogEntry
|
|||
6 => SecurityEvents::GOOGLE_DISABLED,
|
||||
7 => SecurityEvents::TRUSTED_DEVICE_RESET,
|
||||
8 => SecurityEvents::TFA_ADMIN_RESET,
|
||||
9 => SecurityEvents::USER_IMPERSONATED,
|
||||
];
|
||||
|
||||
public function __construct(string $type, string $ip_address, bool $anonymize = true)
|
||||
|
|
|
@ -48,6 +48,7 @@ use App\Events\SecurityEvents;
|
|||
use App\Services\LogSystem\EventLogger;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
|
||||
|
||||
/**
|
||||
* This subscriber writes entries to log if a security related event happens (e.g. the user changes its password).
|
||||
|
@ -70,6 +71,7 @@ final class SecurityEventLoggerSubscriber implements EventSubscriberInterface
|
|||
SecurityEvents::GOOGLE_DISABLED => 'google_disabled',
|
||||
SecurityEvents::GOOGLE_ENABLED => 'google_enabled',
|
||||
SecurityEvents::TFA_ADMIN_RESET => 'tfa_admin_reset',
|
||||
SecurityEvents::USER_IMPERSONATED => 'user_impersonated',
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -103,6 +105,11 @@ final class SecurityEventLoggerSubscriber implements EventSubscriberInterface
|
|||
$this->addLog(SecurityEvents::U2F_REMOVED, $event);
|
||||
}
|
||||
|
||||
public function user_impersonated(SecurityEvent $event): void
|
||||
{
|
||||
$this->addLog(SecurityEvents::USER_IMPERSONATED, $event);
|
||||
}
|
||||
|
||||
public function u2f_added(SecurityEvent $event): void
|
||||
{
|
||||
$this->addLog(SecurityEvents::U2F_ADDED, $event);
|
||||
|
|
64
src/EventSubscriber/SwitchUserEventSubscriber.php
Normal file
64
src/EventSubscriber/SwitchUserEventSubscriber.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Events\SecurityEvent;
|
||||
use App\Events\SecurityEvents;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
|
||||
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class SwitchUserEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
|
||||
public function __construct(private readonly EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'security.switch_user' => 'onSwitchUser',
|
||||
];
|
||||
}
|
||||
|
||||
public function onSwitchUser(SwitchUserEvent $event): void
|
||||
{
|
||||
$target_user = $event->getTargetUser();
|
||||
// We can only handle User objects
|
||||
if (!$target_user instanceof User) {
|
||||
return;
|
||||
}
|
||||
|
||||
//We are only interested in impersonation (not unimpersonation)
|
||||
if (!$event->getToken() instanceof SwitchUserToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
$security_event = new SecurityEvent($target_user);
|
||||
$this->eventDispatcher->dispatch($security_event, SecurityEvents::USER_IMPERSONATED);
|
||||
}
|
||||
}
|
|
@ -78,6 +78,11 @@ final class PasswordChangeNeededSubscriber implements EventSubscriberInterface
|
|||
return;
|
||||
}
|
||||
|
||||
//If the user is impersonated, we don't need to redirect him
|
||||
if ($this->security->isGranted('IS_IMPERSONATOR')) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Abort if we dont need to redirect the user.
|
||||
if (!$user->isNeedPwChange() && !static::TFARedirectNeeded($user)) {
|
||||
return;
|
||||
|
|
|
@ -52,4 +52,6 @@ class SecurityEvents
|
|||
final public const GOOGLE_DISABLED = 'security.google_disabled';
|
||||
final public const TRUSTED_DEVICE_RESET = 'security.trusted_device_reset';
|
||||
final public const TFA_ADMIN_RESET = 'security.2fa_admin_reset';
|
||||
|
||||
final public const USER_IMPERSONATED = 'security.user_impersonated';
|
||||
}
|
||||
|
|
60
src/Security/Voter/ImpersonateUserVoter.php
Normal file
60
src/Security/Voter/ImpersonateUserVoter.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Security\Voter;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Services\UserSystem\PermissionManager;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
class ImpersonateUserVoter extends Voter
|
||||
{
|
||||
|
||||
public function __construct(private PermissionManager $permissionManager)
|
||||
{
|
||||
}
|
||||
|
||||
protected function supports(string $attribute, mixed $subject): bool
|
||||
{
|
||||
return $attribute == 'CAN_SWITCH_USER'
|
||||
&& $subject instanceof UserInterface;
|
||||
}
|
||||
|
||||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
|
||||
{
|
||||
$user = $token->getUser();
|
||||
|
||||
if (!$user instanceof User || !$subject instanceof UserInterface) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//An disabled user is not allowed to do anything...
|
||||
if ($user->isDisabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->permissionManager->inherit($user, 'users', 'impersonate') ?? false;
|
||||
}
|
||||
}
|
|
@ -46,6 +46,10 @@ use App\Entity\UserSystem\User;
|
|||
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;
|
||||
|
@ -57,7 +61,9 @@ final class UserExtension extends AbstractExtension
|
|||
{
|
||||
private readonly LogEntryRepository $repo;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
public function __construct(EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly UrlGeneratorInterface $urlGenerator)
|
||||
{
|
||||
$this->repo = $em->getRepository(AbstractLogEntry::class);
|
||||
}
|
||||
|
@ -76,9 +82,41 @@ final class UserExtension extends AbstractExtension
|
|||
new TwigFunction('last_editing_user', fn(AbstractDBElement $element): ?User => $this->repo->getLastEditingUser($element)),
|
||||
/* Returns the user which has created the given entity. */
|
||||
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(...)),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the user which has impersonated the current user.
|
||||
* If the current user is not impersonated, null is returned.
|
||||
* @return User|null
|
||||
*/
|
||||
public function getImpersonatorUser(): ?User
|
||||
{
|
||||
$token = $this->security->getToken();
|
||||
if ($token instanceof SwitchUserToken) {
|
||||
return $token->getOriginalToken()->getUser();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function isImpersonationActive(): bool
|
||||
{
|
||||
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