-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
206ead0
commit 6dd4372
Showing
6 changed files
with
728 additions
and
283 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace ZfcUser\Controller; | ||
|
||
use Zend\Mvc\Application; | ||
use Zend\Mvc\Router\RouteInterface; | ||
use Zend\Mvc\Router\Exception; | ||
use Zend\Http\PhpEnvironment\Response; | ||
use ZfcUser\Options\ModuleOptions; | ||
|
||
/** | ||
* Returns a redirect response based on the current routing and parameters | ||
*/ | ||
class RedirectCallback | ||
{ | ||
|
||
/** @var RouteInterface */ | ||
private $router; | ||
|
||
/** @var Application */ | ||
private $application; | ||
|
||
/** @var ModuleOptions */ | ||
private $options; | ||
|
||
/** | ||
* @param Application $application | ||
* @param RouteInterface $router | ||
* @param ModuleOptions $options | ||
*/ | ||
public function __construct(Application $application, RouteInterface $router, ModuleOptions $options) | ||
{ | ||
$this->router = $router; | ||
$this->application = $application; | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* @return Response | ||
*/ | ||
public function __invoke() | ||
{ | ||
$routeMatch = $this->application->getMvcEvent()->getRouteMatch(); | ||
$redirect = $this->getRedirect($routeMatch->getMatchedRouteName(), $this->getRedirectRouteFromRequest()); | ||
|
||
$response = $this->application->getResponse(); | ||
$response->getHeaders()->addHeaderLine('Location', $redirect); | ||
$response->setStatusCode(302); | ||
return $response; | ||
} | ||
|
||
/** | ||
* Return the redirect from param. | ||
* First checks GET then POST | ||
* @return string | ||
*/ | ||
protected function getRedirectRouteFromRequest() | ||
{ | ||
$request = $this->application->getRequest(); | ||
$redirect = $request->getQuery('redirect'); | ||
if ($redirect && $this->routeExists($redirect)) { | ||
return $redirect; | ||
} | ||
|
||
$redirect = $request->getPost('redirect'); | ||
if ($redirect && $this->routeExists($redirect)) { | ||
return $redirect; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param $route | ||
* @return bool | ||
*/ | ||
protected function routeExists($route) | ||
{ | ||
try { | ||
$this->router->assemble([], ['name' => $route]); | ||
} catch (Exception\RuntimeException $e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Returns the url to redirect to based on current route. | ||
* If $redirect is set and the option to use redirect is set to true, it will return the $redirect url. | ||
* | ||
* @param string $currentRoute | ||
* @param bool $redirect | ||
* @return mixed | ||
*/ | ||
protected function getRedirect($currentRoute, $redirect = false) | ||
{ | ||
$useRedirect = $this->options->getUseRedirectParameterIfPresent(); | ||
$routeExists = ($redirect && $this->routeExists($redirect)); | ||
if (!$useRedirect || !$routeExists) { | ||
$redirect = false; | ||
} | ||
|
||
switch ($currentRoute) { | ||
case 'zfcuser/login': | ||
$route = ($redirect) ?: $this->options->getLoginRedirectRoute(); | ||
return $this->router->assemble([], ['name' => $route]); | ||
break; | ||
case 'zfcuser/logout': | ||
$route = ($redirect) ?: $this->options->getLogoutRedirectRoute(); | ||
return $this->router->assemble([], ['name' => $route]); | ||
break; | ||
default: | ||
return $this->router->assemble([], ['name' => 'zfcuser']); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Removing this breaks redirects from the loginAction as in this use case
http://stackoverflow.com/questions/25001558/zfcuser-redirect-dynamic-url-after-login
Now the redirect parameter passed to the loginAction is totally ignored in the authenticateAction.
How is this supposed to work now?