-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[TwigBridge] Create impersonation_exit_path() and *_url() functions #24737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,10 @@ public function load(array $configs, ContainerBuilder $container) | |
$container->removeDefinition('security.access.expression_voter'); | ||
} | ||
|
||
if (!class_exists('Symfony\Component\HttpFoundation\RequestStack') || !class_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would RequestStack not exist? It's required by dependencies |
||
$container->removeDefinition('security.impersonate_url_generator'); | ||
} | ||
|
||
// set some global scalars | ||
$container->setParameter('security.access.denied_url', $config['access_denied_url']); | ||
$container->setParameter('security.authentication.manager.erase_credentials', $config['erase_credentials']); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Logout; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Role\SwitchUserRole; | ||
use Symfony\Component\Security\Http\Firewall\SwitchUserListener; | ||
use Symfony\Component\Security\Http\FirewallMapInterface; | ||
use Symfony\Bundle\SecurityBundle\Security\FirewallMap; | ||
|
||
/** | ||
* Provides generator functions for the impersonate url exit. | ||
* | ||
* @author Amrouche Hamza <hamza.simperfit@gmail.com> | ||
*/ | ||
class ImpersonateUrlGenerator | ||
{ | ||
private $requestStack; | ||
private $router; | ||
private $tokenStorage; | ||
private $firewallMap; | ||
|
||
public function __construct(RequestStack $requestStack, UrlGeneratorInterface $router, TokenStorageInterface $tokenStorage = null, FirewallMapInterface $firewallMap) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class does not work with other implementation than FirewallMap, so you can as well just require it, instead of interface and get rid of an instanceof check. Also not sure why is non-optional argument last. |
||
{ | ||
$this->requestStack = $requestStack; | ||
$this->router = $router; | ||
$this->tokenStorage = $tokenStorage; | ||
$this->firewallMap = $firewallMap; | ||
} | ||
|
||
public function getImpersonateExitPath(): string | ||
{ | ||
return $this->generateImpersonateExitUrl(UrlGeneratorInterface::ABSOLUTE_PATH); | ||
} | ||
|
||
public function getImpersonateExitUrl(): string | ||
{ | ||
return $this->generateImpersonateExitUrl(UrlGeneratorInterface::ABSOLUTE_URL); | ||
} | ||
|
||
private function generateImpersonateExitUrl($referenceType): string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be more flexible if this was public and twig functions would call this with correct parameters. We then wouldn't need two public methods. Also, missing typehint. |
||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
$parameters = $request->query; | ||
$exitPath = null; | ||
if ($this->firewallMap instanceof FirewallMap) { | ||
$firewallConfig = $this->firewallMap->getFirewallConfig($request); | ||
|
||
// generate exit impersonation path from current request | ||
if ($this->isImpersonatedUser() && null !== $switchUserConfig = $firewallConfig->getSwitchUser()) { | ||
$exitPath = $request->getRequestUri(); | ||
$exitPath .= null === $request->getQueryString() ? '?' : '&'; | ||
$exitPath .= sprintf('%s=%s', urlencode($switchUserConfig['parameter']), SwitchUserListener::EXIT_VALUE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using http_build_query would be better |
||
} | ||
} | ||
if (null === $exitPath) { | ||
throw new \LogicException('Unable to generate the impersonate exit URL without a path.'); | ||
} | ||
|
||
if ('/' === $exitPath[0]) { | ||
if (!$this->requestStack) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a dead code. RequestStack is required so will never be null |
||
throw new \LogicException('Unable to generate the impersonate exit URL without a RequestStack.'); | ||
} | ||
|
||
$url = UrlGeneratorInterface::ABSOLUTE_URL === $referenceType ? $request->getUriForPath($exitPath) : $request->getBaseUrl().$exitPath; | ||
|
||
if (!empty($parameters)) { | ||
$url .= '?'.http_build_query($parameters); | ||
} | ||
} else { | ||
if (!$this->router) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another dead path |
||
throw new \LogicException('Unable to generate the impersonate exit URL without a Router.'); | ||
} | ||
|
||
$url = $this->router->generate($exitPath, array(), $referenceType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong, first parameter of route path is name of the route, query string is given instead |
||
} | ||
|
||
return $url; | ||
} | ||
|
||
private function isImpersonatedUser(): bool | ||
{ | ||
if (null === $token = $this->tokenStorage->getToken()) { | ||
return false; | ||
} | ||
|
||
$assignedRoles = $token->getRoles(); | ||
|
||
foreach ($assignedRoles as $role) { | ||
if ($role instanceof SwitchUserRole) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than this mechanism to check if user is currently impersonating, shouldn't it be done the way docs recommend? That means checking if user has ROLE_PREVIOUS_ADMIN via AuthorizationChecker. https://symfony.com/doc/current/security/impersonating_user.html |
||
return true; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think PHP strongly discourages non-optional arguments after optional
http://php.net/manual/en/functions.arguments.php