2020-02-06 19:22:01 +01:00
|
|
|
<?php
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 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/>.
|
|
|
|
*/
|
2020-03-15 13:56:31 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-06 19:22:01 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-06 19:22:01 +01:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* 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.
|
2020-02-06 19:22:01 +01:00
|
|
|
*
|
|
|
|
* 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
|
2020-02-22 18:14:36 +01:00
|
|
|
* GNU Affero General Public License for more details.
|
2020-02-06 19:22:01 +01:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* 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/>.
|
2020-02-06 19:22:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Twig;
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use App\Entity\UserSystem\User;
|
2020-02-06 19:22:01 +01:00
|
|
|
use App\Entity\LogSystem\AbstractLogEntry;
|
2022-08-14 19:32:53 +02:00
|
|
|
use App\Repository\LogEntryRepository;
|
2020-02-06 19:22:01 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2023-07-04 00:31:13 +02:00
|
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
2023-07-08 22:43:41 +02:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2023-07-04 00:31:13 +02:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
|
2023-07-08 22:43:41 +02:00
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
2020-02-06 19:22:01 +01:00
|
|
|
use Twig\Extension\AbstractExtension;
|
2022-09-18 17:50:25 +02:00
|
|
|
use Twig\TwigFilter;
|
2020-02-06 19:22:01 +01:00
|
|
|
use Twig\TwigFunction;
|
|
|
|
|
2023-06-11 15:02:59 +02:00
|
|
|
/**
|
|
|
|
* @see \App\Tests\Twig\UserExtensionTest
|
|
|
|
*/
|
2022-09-18 17:50:25 +02:00
|
|
|
final class UserExtension extends AbstractExtension
|
2020-02-06 19:22:01 +01:00
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
private readonly LogEntryRepository $repo;
|
2020-02-06 19:22:01 +01:00
|
|
|
|
2023-07-08 22:43:41 +02:00
|
|
|
public function __construct(EntityManagerInterface $em,
|
|
|
|
private readonly Security $security,
|
|
|
|
private readonly UrlGeneratorInterface $urlGenerator)
|
2020-02-06 19:22:01 +01:00
|
|
|
{
|
|
|
|
$this->repo = $em->getRepository(AbstractLogEntry::class);
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
public function getFilters(): array
|
|
|
|
{
|
|
|
|
return [
|
2023-06-11 14:15:46 +02:00
|
|
|
new TwigFilter('remove_locale_from_path', fn(string $path): string => $this->removeLocaleFromPath($path)),
|
2022-09-18 17:50:25 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-14 19:09:07 +02:00
|
|
|
public function getFunctions(): array
|
2020-02-06 19:22:01 +01:00
|
|
|
{
|
|
|
|
return [
|
2022-09-18 16:45:12 +02:00
|
|
|
/* Returns the user which has edited the given entity the last time. */
|
2023-06-11 14:55:06 +02:00
|
|
|
new TwigFunction('last_editing_user', fn(AbstractDBElement $element): ?User => $this->repo->getLastEditingUser($element)),
|
2022-09-18 16:45:12 +02:00
|
|
|
/* Returns the user which has created the given entity. */
|
2023-06-11 14:55:06 +02:00
|
|
|
new TwigFunction('creating_user', fn(AbstractDBElement $element): ?User => $this->repo->getCreatingUser($element)),
|
2023-07-04 00:31:13 +02:00
|
|
|
new TwigFunction('impersonator_user', $this->getImpersonatorUser(...)),
|
|
|
|
new TwigFunction('impersonation_active', $this->isImpersonationActive(...)),
|
2023-07-08 22:43:41 +02:00
|
|
|
new TwigFunction('impersonation_path', $this->getImpersonationPath(...)),
|
2020-02-06 19:22:01 +01:00
|
|
|
];
|
|
|
|
}
|
2022-09-18 17:50:25 +02:00
|
|
|
|
2023-07-04 00:31:13 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2023-07-17 00:43:35 +02:00
|
|
|
$tmp = $token->getOriginalToken()->getUser();
|
|
|
|
|
|
|
|
if ($tmp instanceof User) {
|
|
|
|
return $tmp;
|
|
|
|
}
|
2023-07-04 00:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isImpersonationActive(): bool
|
|
|
|
{
|
|
|
|
return $this->security->isGranted('IS_IMPERSONATOR');
|
|
|
|
}
|
|
|
|
|
2023-07-08 22:43:41 +02:00
|
|
|
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()]);
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* This function/filter generates a path.
|
2022-09-18 17:50:25 +02:00
|
|
|
*/
|
|
|
|
public function removeLocaleFromPath(string $path): string
|
|
|
|
{
|
|
|
|
//Ensure the path has the correct format
|
|
|
|
if (!preg_match('/^\/\w{2}\//', $path)) {
|
|
|
|
throw new \InvalidArgumentException('The given path is not a localized path!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
//Remove the part with locale
|
|
|
|
unset($parts[1]);
|
|
|
|
|
|
|
|
return implode('/', $parts);
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
}
|