-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Adding new TargetPathTrait to get/set the authentication "target_path" #17714
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Security/Http/Tests/Util/TargetPathTraitTest.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 | ||
|
|
||
| namespace Symfony\Component\Security\Http\Tests\Util; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
| use Symfony\Component\Security\Http\Util\TargetPathTrait; | ||
|
|
||
| class TargetPathTraitTest extends \PHPUnit_Framework_TestCase | ||
| { | ||
| public function testSetTargetPath() | ||
| { | ||
| $obj = new TestClassWithTargetPathTrait(); | ||
|
|
||
| $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface') | ||
| ->getMock(); | ||
|
|
||
| $session->expects($this->once()) | ||
| ->method('set') | ||
| ->with('_security.firewall_name.target_path', '/foo'); | ||
|
|
||
| $obj->doSetTargetPath($session, 'firewall_name', '/foo'); | ||
| } | ||
|
|
||
| public function testGetTargetPath() | ||
| { | ||
| $obj = new TestClassWithTargetPathTrait(); | ||
|
|
||
| $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface') | ||
| ->getMock(); | ||
|
|
||
| $session->expects($this->once()) | ||
| ->method('get') | ||
| ->with('_security.cool_firewall.target_path') | ||
| ->willReturn('/bar'); | ||
|
|
||
| $actualUri = $obj->doGetTargetPath($session, 'cool_firewall'); | ||
| $this->assertEquals( | ||
| '/bar', | ||
| $actualUri | ||
| ); | ||
| } | ||
|
|
||
| public function testRemoveTargetPath() | ||
| { | ||
| $obj = new TestClassWithTargetPathTrait(); | ||
|
|
||
| $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface') | ||
| ->getMock(); | ||
|
|
||
| $session->expects($this->once()) | ||
| ->method('remove') | ||
| ->with('_security.best_firewall.target_path'); | ||
|
|
||
| $obj->doRemoveTargetPath($session, 'best_firewall'); | ||
| } | ||
| } | ||
|
|
||
| class TestClassWithTargetPathTrait | ||
| { | ||
| use TargetPathTrait; | ||
|
|
||
| public function doSetTargetPath(SessionInterface $session, $providerKey, $uri) | ||
| { | ||
| $this->saveTargetPath($session, $providerKey, $uri); | ||
| } | ||
|
|
||
| public function doGetTargetPath(SessionInterface $session, $providerKey) | ||
| { | ||
| return $this->getTargetPath($session, $providerKey); | ||
| } | ||
|
|
||
| public function doRemoveTargetPath(SessionInterface $session, $providerKey) | ||
| { | ||
| $this->removeTargetPath($session, $providerKey); | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/Security/Http/Util/TargetPathTrait.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,58 @@ | ||
| <?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\Util; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
|
|
||
| /** | ||
| * Trait to get (and set) the URL the user last visited before being forced to authenticate. | ||
| */ | ||
| trait TargetPathTrait | ||
| { | ||
| /** | ||
| * Set the target path the user should be redirected to after authentication. | ||
| * | ||
| * Usually, you do not need to set this directly. | ||
| * | ||
| * @param SessionInterface $session | ||
| * @param string $providerKey The name of your firewall | ||
| * @param string $uri The URI to set as the target path | ||
| */ | ||
| private function saveTargetPath(SessionInterface $session, $providerKey, $uri) | ||
| { | ||
| $session->set('_security.'.$providerKey.'.target_path', $uri); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the URL (if any) the user visited that forced them to login. | ||
| * | ||
| * @param SessionInterface $session | ||
| * @param string $providerKey The name of your firewall | ||
| * | ||
| * @return string | ||
| */ | ||
| private function getTargetPath(SessionInterface $session, $providerKey) | ||
| { | ||
| return $session->get('_security.'.$providerKey.'.target_path'); | ||
| } | ||
|
|
||
| /** | ||
| * Removes the target path from the session. | ||
| * | ||
| * @param SessionInterface $session | ||
| * @param string $providerKey The name of your firewall | ||
| */ | ||
| private function removeTargetPath(SessionInterface $session, $providerKey) | ||
| { | ||
| $session->remove('_security.'.$providerKey.'.target_path'); | ||
| } | ||
| } | ||
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.
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.
Sets