Commit f3eca9a7 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

UnauthorizedStrategy déplacé dans le module UnicaenUser.

parent 5e0f7e50
......@@ -184,10 +184,6 @@ class Module implements ConfigProviderInterface, ViewHelperProviderInterface, Se
$sm->get('ZfcUser\Authentication\Adapter\AdapterChain')
);
},
'UnicaenAuth\View\UnauthorizedStrategy' => function (\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
$config = $serviceLocator->get('Config');
return new View\UnauthorizedStrategy($config['bjyauthorize']['template']);
},
'UnicaenAuth\Authentication\Storage\Mixed' => function($sm) {
$storage = new Authentication\Storage\Mixed();
$storage->setLdapStorage($sm->get('UnicaenAuth\Authentication\Storage\Ldap'))
......
<?php
namespace UnicaenAuth\View;
use Zend\Mvc\MvcEvent;
/**
* Stratégie appliquée lorsque l'accès à une page est boquée par le module BjyAuthorize.
* Si aucun utilisateur n'est connecté : redirection vers la page de connexion.
* Sinon : stratégie standard du module BjyAuthorize.
*
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
* @see \BjyAuthorize\View\UnauthorizedStrategy
*/
class UnauthorizedStrategy extends \BjyAuthorize\View\UnauthorizedStrategy
{
/**
* Si aucun utilisateur n'est connecté : redirection vers la page de connexion.
* Sinon : stratégie standard du module BjyAuthorize.
*
* @param \Zend\Mvc\MvcEvent $e
* @return type
*/
public function onDispatchError(MvcEvent $e)
{
// Do nothing if the result is a response object
$result = $e->getResult();
if ($result instanceof Response) {
return;
}
// Common view variables
$viewVariables = array(
'error' => $e->getParam('error'),
'identity' => $e->getParam('identity'),
);
$error = $e->getError();
switch($error)
{
case 'error-unauthorized-controller':
$viewVariables['controller'] = $e->getParam('controller');
$viewVariables['action'] = $e->getParam('action');
break;
case 'error-unauthorized-route':
$viewVariables['route'] = $e->getParam('route');
break;
default:
/*
* do nothing if there is no error in the event or the error
* does not match one of our predefined errors (we don't want
* our 403.phtml to handle other types of errors)
*/
return;
}
$model = new \Zend\View\Model\ViewModel($viewVariables);
$model->setTemplate($this->getTemplate());
$e->getViewModel()->addChild($model);
$response = $e->getResponse();
if (!$response) {
$response = new HttpResponse();
$e->setResponse($response);
}
$response->setStatusCode(403);
//
// Jusqu'ici, c'est texto le code de parent::onDispatchError()
//
$app = $e->getTarget();
$sm = $app->getServiceManager();
$authService = $sm->get('zfcuser_auth_service');
if ($authService->hasIdentity()) {
return;
}
$response = $e->getResponse();
$helpers = $sm->get('ViewHelperManager');
$url = $helpers->get('url');
$origin = $e->getRequest()->getRequestUri();
$response->getHeaders()->addHeaderLine('Location', $url('zfcuser/login') . '?redirect=' . $origin);
$response->setStatusCode(302);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment