Skip to content

Commit 6dd4372

Browse files
committed
Change redirect strategy
1 parent 206ead0 commit 6dd4372

6 files changed

Lines changed: 728 additions & 283 deletions

File tree

Module.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Zend\ModuleManager\Feature\ConfigProviderInterface;
88
use Zend\ModuleManager\Feature\ServiceProviderInterface;
99
use Zend\Stdlib\Hydrator\ClassMethods;
10+
use ZfcUser\Controller\RedirectCallback;
1011

1112
class Module implements
1213
AutoloaderProviderInterface,
@@ -88,7 +89,18 @@ public function getServiceConfig()
8889
'zfcuser_register_form_hydrator' => 'Zend\Stdlib\Hydrator\ClassMethods',
8990
),
9091
'factories' => array(
92+
'zfcuser_redirect_callback' => function ($sm) {
93+
/* @var RouteInterface $router */
94+
$router = $sm->get('Router');
9195

96+
/* @var Application $application */
97+
$application = $sm->get('Application');
98+
99+
/* @var ModuleOptions $options */
100+
$options = $sm->get('zfcuser_module_options');
101+
102+
return new RedirectCallback($application, $router, $options);
103+
},
92104
'zfcuser_module_options' => function ($sm) {
93105
$config = $sm->get('Config');
94106
return new Options\ModuleOptions(isset($config['zfcuser']) ? $config['zfcuser'] : array());

config/module.config.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
<?php
2+
use ZfcUser\Controller\UserController;
3+
24
return array(
35
'view_manager' => array(
46
'template_path_stack' => array(
57
'zfcuser' => __DIR__ . '/../view',
68
),
79
),
810
'controllers' => array(
9-
'invokables' => array(
10-
'zfcuser' => 'ZfcUser\Controller\UserController',
11+
12+
'factories' => array(
13+
'zfcuser' => function($controllerManager){
14+
/* @var ControllerManager $controllerManager*/
15+
$serviceManager = $controllerManager->getServiceLocator();
16+
17+
/* @var RedirectCallback $redirectCallback */
18+
$redirectCallback = $serviceManager->get('zfcuser_redirect_callback');
19+
20+
/* @var UserController $controller */
21+
$controller = new UserController($redirectCallback);
22+
23+
return $controller;
24+
},
1125
),
1226
),
1327
'service_manager' => array(
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace ZfcUser\Controller;
4+
5+
use Zend\Mvc\Application;
6+
use Zend\Mvc\Router\RouteInterface;
7+
use Zend\Mvc\Router\Exception;
8+
use Zend\Http\PhpEnvironment\Response;
9+
use ZfcUser\Options\ModuleOptions;
10+
11+
/**
12+
* Returns a redirect response based on the current routing and parameters
13+
*/
14+
class RedirectCallback
15+
{
16+
17+
/** @var RouteInterface */
18+
private $router;
19+
20+
/** @var Application */
21+
private $application;
22+
23+
/** @var ModuleOptions */
24+
private $options;
25+
26+
/**
27+
* @param Application $application
28+
* @param RouteInterface $router
29+
* @param ModuleOptions $options
30+
*/
31+
public function __construct(Application $application, RouteInterface $router, ModuleOptions $options)
32+
{
33+
$this->router = $router;
34+
$this->application = $application;
35+
$this->options = $options;
36+
}
37+
38+
/**
39+
* @return Response
40+
*/
41+
public function __invoke()
42+
{
43+
$routeMatch = $this->application->getMvcEvent()->getRouteMatch();
44+
$redirect = $this->getRedirect($routeMatch->getMatchedRouteName(), $this->getRedirectRouteFromRequest());
45+
46+
$response = $this->application->getResponse();
47+
$response->getHeaders()->addHeaderLine('Location', $redirect);
48+
$response->setStatusCode(302);
49+
return $response;
50+
}
51+
52+
/**
53+
* Return the redirect from param.
54+
* First checks GET then POST
55+
* @return string
56+
*/
57+
protected function getRedirectRouteFromRequest()
58+
{
59+
$request = $this->application->getRequest();
60+
$redirect = $request->getQuery('redirect');
61+
if ($redirect && $this->routeExists($redirect)) {
62+
return $redirect;
63+
}
64+
65+
$redirect = $request->getPost('redirect');
66+
if ($redirect && $this->routeExists($redirect)) {
67+
return $redirect;
68+
}
69+
70+
return false;
71+
}
72+
73+
/**
74+
* @param $route
75+
* @return bool
76+
*/
77+
protected function routeExists($route)
78+
{
79+
try {
80+
$this->router->assemble([], ['name' => $route]);
81+
} catch (Exception\RuntimeException $e) {
82+
return false;
83+
}
84+
return true;
85+
}
86+
87+
/**
88+
* Returns the url to redirect to based on current route.
89+
* If $redirect is set and the option to use redirect is set to true, it will return the $redirect url.
90+
*
91+
* @param string $currentRoute
92+
* @param bool $redirect
93+
* @return mixed
94+
*/
95+
protected function getRedirect($currentRoute, $redirect = false)
96+
{
97+
$useRedirect = $this->options->getUseRedirectParameterIfPresent();
98+
$routeExists = ($redirect && $this->routeExists($redirect));
99+
if (!$useRedirect || !$routeExists) {
100+
$redirect = false;
101+
}
102+
103+
switch ($currentRoute) {
104+
case 'zfcuser/login':
105+
$route = ($redirect) ?: $this->options->getLoginRedirectRoute();
106+
return $this->router->assemble([], ['name' => $route]);
107+
break;
108+
case 'zfcuser/logout':
109+
$route = ($redirect) ?: $this->options->getLogoutRedirectRoute();
110+
return $this->router->assemble([], ['name' => $route]);
111+
break;
112+
default:
113+
return $this->router->assemble([], ['name' => 'zfcuser']);
114+
}
115+
}
116+
}

src/ZfcUser/Controller/UserController.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ class UserController extends AbstractActionController
5555
*/
5656
protected $options;
5757

58+
/**
59+
* @var callable $redirectCallback
60+
*/
61+
protected $redirectCallback;
62+
63+
/**
64+
* @param callable $redirectCallback
65+
*/
66+
public function __construct($redirectCallback)
67+
{
68+
if (!is_callable($redirectCallback)) {
69+
throw new \InvalidArgumentException('You must supply a callable redirectCallback');
70+
}
71+
$this->redirectCallback = $redirectCallback;
72+
}
73+
5874
/**
5975
* User page
6076
*/
@@ -115,13 +131,9 @@ public function logoutAction()
115131
$this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters();
116132
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
117133

118-
$redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false));
119-
120-
if ($this->getOptions()->getUseRedirectParameterIfPresent() && $redirect) {
121-
return $this->redirect()->toRoute($redirect);
122-
}
134+
$redirect = $this->redirectCallback;
123135

124-
return $this->redirect()->toRoute($this->getOptions()->getLogoutRedirectRoute());
136+
return $redirect();
125137
}
126138

127139
/**
@@ -154,11 +166,9 @@ public function authenticateAction()
154166
);
155167
}
156168

157-
if ($this->getOptions()->getUseRedirectParameterIfPresent() && $redirect) {
158-
return $this->redirect()->toRoute($redirect);
159-
}
169+
$redirect = $this->redirectCallback;
160170

161-
return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
171+
return $redirect();
162172
}
163173

164174
/**

0 commit comments

Comments
 (0)