-
Notifications
You must be signed in to change notification settings - Fork 339
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
New redirect strategy #480
base: master
Are you sure you want to change the base?
Changes from 1 commit
418ac43
a720f37
5d54a4b
2becf29
98f0953
2115f58
d2f12b0
779a577
a4ca815
d3c228a
de165fb
e5b2977
d674ec2
6abfd1a
cf16a3f
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace ZfcUser\Controller; | ||
|
||
use Zend\Mvc\Controller\Plugin\Redirect; | ||
use Zend\Mvc\Router\RouteInterface; | ||
use Zend\Mvc\Router\Exception; | ||
use Zend\Http\PhpEnvironment\Request; | ||
use Zend\Http\PhpEnvironment\Response; | ||
use ZfcUser\Options\ModuleOptions; | ||
|
||
class RedirectCallback | ||
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. Missing docblock |
||
{ | ||
/** @var RouteInterface */ | ||
protected $router; | ||
|
||
/** @var Response */ | ||
protected $response; | ||
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. Request and response should not be localized as properties. |
||
|
||
/** @var Request */ | ||
protected $request; | ||
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. Same as above |
||
|
||
/** @var ModuleOptions */ | ||
protected $options; | ||
|
||
/** | ||
* @param RouteInterface $router | ||
* @param Response $response | ||
* @param Request $request | ||
* @param ModuleOptions $options | ||
*/ | ||
public function __construct(RouteInterface $router, Response $response, Request $request, ModuleOptions $options) | ||
{ | ||
$this->router = $router; | ||
$this->request = $request; | ||
$this->response = $response; | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* @return Response | ||
*/ | ||
public function __invoke() | ||
{ | ||
$routeMatch = $this->router->match($this->request); | ||
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. You can inject the MVC routematch instead of matching again, see below in the factory 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. |
||
$response = $this->response; | ||
$response->getHeaders()->addHeaderLine('Location', $this->getRedirect($routeMatch->getMatchedRouteName(), $routeMatch->getParam('redirect', false))); | ||
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. I would extract a variable here to make it more readble: $url = $this->getRedirectUrl(
$routeMatch->getMatchedRouteName(),
$routeMatch->getParam('redirect', false)
); Note I also changed 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 thing: where do you the the route parameter from? I don't see a route definition in this PR which adds a parameter to the route called |
||
$response->setStatusCode(302); | ||
return $response; | ||
} | ||
|
||
/** | ||
* @param $route | ||
* @return bool | ||
*/ | ||
protected function routeExists($route) | ||
{ | ||
try{ | ||
$this->router->assemble($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) | ||
{ | ||
if (!$this->options->getUseRedirectParameterIfPresent() || ($redirect && !$this->routeExists($redirect))) { | ||
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. I would make this more readable: $useRedirect = $this->options->getUseRedirectParameterIfPresent();
$routeExists = ($redirect && $this->routeExists($redirect));
if (!$useRedirect || !$routeExists) {
$redirect = false;
} |
||
$redirect = false; | ||
} | ||
|
||
switch ($currentRoute) { | ||
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. If you make this thing a controller plugin (which I would advice), you can also reuse the url and/or redirect controller plugins. This just duplicates logic of assembling and returning responses 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. Agree, this should be a controller plugin. I am refactoring as we speak. |
||
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']); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,16 @@ class UserController extends AbstractActionController | |
*/ | ||
protected $options; | ||
|
||
/** | ||
* @var Callable $redirectCallback | ||
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.
|
||
*/ | ||
protected $redirectCallback; | ||
|
||
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. Remove this empty newline 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. Was 2 before, 1 now as it should be. |
||
public function __construct($redirectCallback) | ||
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. Does ZfcUser require 5.3? Otherwise, |
||
{ | ||
$this->redirectCallback = $redirectCallback; | ||
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. Check against |
||
} | ||
|
||
/** | ||
* User page | ||
*/ | ||
|
@@ -116,13 +126,8 @@ public function logoutAction() | |
$this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters(); | ||
$this->zfcUserAuthentication()->getAuthService()->clearIdentity(); | ||
|
||
$redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false)); | ||
|
||
if ($this->getOptions()->getUseRedirectParameterIfPresent() && $redirect) { | ||
return $this->redirect()->toRoute($redirect); | ||
} | ||
|
||
return $this->redirect()->toRoute($this->getOptions()->getLogoutRedirectRoute()); | ||
$redirect = $this->redirectCallback; | ||
return $redirect(); | ||
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. Newline before this line if possible |
||
} | ||
|
||
/** | ||
|
@@ -165,7 +170,8 @@ public function authenticateAction() | |
$route = $route($this->zfcUserAuthentication()->getIdentity()); | ||
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 is a callback support needed here? 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. Now that we have this redirect callback, it's not needed, but it would be BC to remove. |
||
} | ||
|
||
return $this->redirect()->toRoute($route); | ||
$redirect = $this->redirectCallback; | ||
return $redirect(); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
namespace ZfcUser\Factory\Controller; | ||
|
||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use ZfcUser\Controller\RedirectCallback; | ||
|
||
class RedirectCallbackFactory implements FactoryInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$router = $serviceLocator->get('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. Can you add some IDE hints here? They help with static analysis |
||
$response = $serviceLocator->get('Response'); | ||
$request = $serviceLocator->get('Request'); | ||
$options = $serviceLocator->get('zfcuser_module_options'); | ||
return new RedirectCallback($router, $response, $request, $options); | ||
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. I'd inject the route match, so you don't need to match twice. On top of my head: $mvcEvent = $serviceLocator->get('Application')->getMvcEvent();
$routeMatch = $mvcEvent->getRouteMatch(); |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace ZfcUser\Factory\Controller; | ||
|
||
use Zend\Mvc\Controller\ControllerManager; | ||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use ZfcUser\Authentication\Adapter; | ||
use ZfcUser\Controller\UserController; | ||
|
||
class UserControllerFactory implements FactoryInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function createService(ServiceLocatorInterface $controllerManager) | ||
{ | ||
/** @var ControllerManager $controllerManager*/ | ||
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.
|
||
$serviceManager = $controllerManager->getServiceLocator(); | ||
|
||
$redirectCallback = $serviceManager->get('zfcuser_redirect_callback'); | ||
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. Same as above - hints |
||
$controller = new UserController($redirectCallback); | ||
|
||
return $controller; | ||
} | ||
} |
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.
Missing headers
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.
As discussed, this may be done later