-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Create impersonation_exit_path() and *_url() functions #32841
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,39 +19,47 @@ | |
class SwitchUserToken extends UsernamePasswordToken | ||
{ | ||
private $originalToken; | ||
private $originatedFromUri; | ||
|
||
/** | ||
* @param string|object $user The username (like a nickname, email address, etc.), or a UserInterface instance or an object implementing a __toString method | ||
* @param mixed $credentials This usually is the password of the user | ||
* @param string|object $user The username (like a nickname, email address, etc.), or a UserInterface instance or an object implementing a __toString method | ||
* @param mixed $credentials This usually is the password of the user | ||
* @param string|null $originatedFromUri The URI where was the user at the switch | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct($user, $credentials, string $firewallName, array $roles, TokenInterface $originalToken) | ||
public function __construct($user, $credentials, string $firewallName, array $roles, TokenInterface $originalToken, string $originatedFromUri = null) | ||
{ | ||
parent::__construct($user, $credentials, $firewallName, $roles); | ||
|
||
$this->originalToken = $originalToken; | ||
$this->originatedFromUri = $originatedFromUri; | ||
} | ||
|
||
public function getOriginalToken(): TokenInterface | ||
{ | ||
return $this->originalToken; | ||
} | ||
|
||
public function getOriginatedFromUri(): ?string | ||
{ | ||
return $this->originatedFromUri; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __serialize(): array | ||
{ | ||
return [$this->originalToken, parent::__serialize()]; | ||
return [$this->originalToken, $this->originatedFromUri, parent::__serialize()]; | ||
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. Don't we need to add at the end to avoid issues with existing data when applications are updated? |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __unserialize(array $data): void | ||
{ | ||
[$this->originalToken, $parentData] = $data; | ||
[$this->originalToken, $this->originatedFromUri, $parentData] = $data; | ||
$parentData = \is_array($parentData) ? $parentData : unserialize($parentData); | ||
parent::__unserialize($parentData); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Security/Http/Impersonate/ImpersonateUrlGenerator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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\Impersonate; | ||
|
||
use Symfony\Bundle\SecurityBundle\Security\FirewallMap; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; | ||
use Symfony\Component\Security\Http\Firewall\SwitchUserListener; | ||
|
||
/** | ||
* Provides generator functions for the impersonate url exit. | ||
* | ||
* @author Amrouche Hamza <hamza.simperfit@gmail.com> | ||
* @author Damien Fayet <damienf1521@gmail.com> | ||
*/ | ||
class ImpersonateUrlGenerator | ||
{ | ||
private $requestStack; | ||
private $tokenStorage; | ||
private $firewallMap; | ||
|
||
public function __construct(RequestStack $requestStack, FirewallMap $firewallMap, TokenStorageInterface $tokenStorage) | ||
{ | ||
$this->requestStack = $requestStack; | ||
$this->tokenStorage = $tokenStorage; | ||
$this->firewallMap = $firewallMap; | ||
} | ||
|
||
public function generateExitPath(string $targetUri = null): string | ||
{ | ||
return $this->buildExitPath($targetUri); | ||
} | ||
|
||
public function generateExitUrl(string $targetUri = null): string | ||
{ | ||
if (null === $request = $this->requestStack->getCurrentRequest()) { | ||
return ''; | ||
} | ||
|
||
return $request->getUriForPath($this->buildExitPath($targetUri)); | ||
} | ||
|
||
private function isImpersonatedUser(): bool | ||
{ | ||
return $this->tokenStorage->getToken() instanceof SwitchUserToken; | ||
} | ||
|
||
private function buildExitPath(string $targetUri = null): string | ||
{ | ||
if (null === ($request = $this->requestStack->getCurrentRequest()) || !$this->isImpersonatedUser()) { | ||
return ''; | ||
} | ||
|
||
if (null === $switchUserConfig = $this->firewallMap->getFirewallConfig($request)->getSwitchUser()) { | ||
throw new \LogicException('Unable to generate the impersonate exit URL without a firewall configured for the user switch.'); | ||
} | ||
|
||
if (null === $targetUri) { | ||
$targetUri = $request->getRequestUri(); | ||
} | ||
|
||
$targetUri .= (parse_url($targetUri, \PHP_URL_QUERY) ? '&' : '?').http_build_query([$switchUserConfig['parameter'] => SwitchUserListener::EXIT_VALUE]); | ||
|
||
return $targetUri; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.