diff --git a/config/module.config.php b/config/module.config.php index 7ac9feeec61e656372d15e01b96ca861bf93f19d..a5df943e1b6f3564f554614e0487f61b991d21d9 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -367,7 +367,8 @@ return [ 'UnicaenAuth\Provider\Role\Config' => 'UnicaenAuth\Provider\Role\ConfigServiceFactory', 'UnicaenAuth\Provider\Role\DbRole' => 'UnicaenAuth\Provider\Role\DbRoleServiceFactory', 'UnicaenAuth\Provider\Role\Username' => 'UnicaenAuth\Provider\Role\UsernameServiceFactory', - 'BjyAuthorize\Service\Authorize' => 'UnicaenAuth\Service\AuthorizeServiceFactory', // surcharge!!! + 'BjyAuthorize\Service\Authorize' => 'UnicaenAuth\Service\AuthorizeServiceFactory', // substituion + 'zfcuser_redirect_callback' => 'UnicaenAuth\Authentication\RedirectCallbackFactory', // substituion ], 'initializers' => [ 'UnicaenAuth\Service\UserAwareInitializer', diff --git a/src/UnicaenAuth/Authentication/RedirectCallback.php b/src/UnicaenAuth/Authentication/RedirectCallback.php new file mode 100644 index 0000000000000000000000000000000000000000..a2a1a697a1f457e88f0affd62a58c44b0529d883 --- /dev/null +++ b/src/UnicaenAuth/Authentication/RedirectCallback.php @@ -0,0 +1,127 @@ +<?php + +namespace UnicaenAuth\Authentication; + +use Zend\Http\PhpEnvironment\Response; +use Zend\Mvc\Application; +use Zend\Mvc\Router\Exception; +use Zend\Mvc\Router\RouteInterface; +use ZfcUser\Options\ModuleOptions; + +/** + * Buils 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 + */ + private 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; + } + + if ($redirect) { + return $redirect; + } + + return false; + } + + /** + * @param $route + * @return bool + */ + private function routeExists($route) + { + try { + $this->router->assemble(array(), array('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) { + $redirect = false; + } + elseif (!$routeExists && $redirect) { + return $redirect; + } + else { + $redirect = false; + } + + switch ($currentRoute) { + case 'zfcuser/register': + case 'zfcuser/login': + case 'zfcuser/authenticate': + $route = ($redirect) ?: $this->options->getLoginRedirectRoute(); + return $this->router->assemble(array(), array('name' => $route)); + break; + case 'zfcuser/logout': + $route = ($redirect) ?: $this->options->getLogoutRedirectRoute(); + return $this->router->assemble(array(), array('name' => $route)); + break; + default: + return $this->router->assemble(array(), array('name' => 'zfcuser')); + } + } +} diff --git a/src/UnicaenAuth/Authentication/RedirectCallbackFactory.php b/src/UnicaenAuth/Authentication/RedirectCallbackFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..76359f280485d7ae18552ec91a5070530a5df790 --- /dev/null +++ b/src/UnicaenAuth/Authentication/RedirectCallbackFactory.php @@ -0,0 +1,32 @@ +<?php + +namespace UnicaenAuth\Authentication; + +use Zend\Mvc\Application; +use Zend\Mvc\Router\RouteInterface; +use Zend\ServiceManager\FactoryInterface; +use Zend\ServiceManager\ServiceLocatorInterface; +use ZfcUser\Options\ModuleOptions; + +class RedirectCallbackFactory implements FactoryInterface +{ + /** + * Create service + * + * @param ServiceLocatorInterface $serviceLocator + * @return mixed + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + /* @var RouteInterface $router */ + $router = $serviceLocator->get('Router'); + + /* @var Application $application */ + $application = $serviceLocator->get('Application'); + + /* @var ModuleOptions $options */ + $options = $serviceLocator->get('zfcuser_module_options'); + + return new RedirectCallback($application, $router, $options); + } +}