Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #17714 Adding new TargetPathTrait to get/set the authenticati…
…on "target_path" (weaverryan) This PR was squashed before being merged into the 3.1-dev branch (closes #17714). Discussion ---------- Adding new TargetPathTrait to get/set the authentication "target_path" | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | not yet... Hi guys! This is a small guy. Basically, when you're doing custom auth (i.e. a guard authenticator), it's common to need the previous URL the user tried to get to (i.e. the "target path"). It's not much work to do it now, but it's very abstract - needing to know a weird string pattern. This just wraps that weirdness up in a simple function (`getTargetPath()`). Thanks! Commits ------- 18dfe37 Adding new TargetPathTrait to get/set the authentication "target_path"
- Loading branch information
Showing
5 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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'); | ||
} | ||
} |