Inject authentication manager into Application using constructor

This commit is contained in:
Dmitriy Simushev 2014-07-04 13:11:40 +00:00
parent af112aa72b
commit 93eeeafd3c
2 changed files with 39 additions and 9 deletions

View File

@ -19,6 +19,7 @@
require_once(dirname(__FILE__) . '/libs/init.php'); require_once(dirname(__FILE__) . '/libs/init.php');
use Mibew\Application; use Mibew\Application;
use Mibew\Authentication\AuthenticationManager;
use Mibew\Routing\RouteCollectionLoader; use Mibew\Routing\RouteCollectionLoader;
use Mibew\Routing\Router; use Mibew\Routing\Router;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -28,7 +29,7 @@ $file_locator = new FileLocator(array(MIBEW_FS_ROOT));
$router = new Router(new RouteCollectionLoader($file_locator)); $router = new Router(new RouteCollectionLoader($file_locator));
$router->setOption('route_collection', RouteCollectionLoader::ROUTES_ALL); $router->setOption('route_collection', RouteCollectionLoader::ROUTES_ALL);
$application = new Application($router); $application = new Application($router, new AuthenticationManager());
// Process request // Process request
$request = Request::createFromGlobals(); $request = Request::createFromGlobals();

View File

@ -18,7 +18,8 @@
namespace Mibew; namespace Mibew;
use Mibew\AccessControl\Check\CheckResolver; use Mibew\AccessControl\Check\CheckResolver;
use Mibew\Authentication\AuthenticationManager; use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Controller\ControllerResolver; use Mibew\Controller\ControllerResolver;
use Mibew\EventDispatcher; use Mibew\EventDispatcher;
use Mibew\Http\CookieFactory; use Mibew\Http\CookieFactory;
@ -39,7 +40,7 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException as ResourceNot
/** /**
* Incapsulates whole application * Incapsulates whole application
*/ */
class Application implements RouterAwareInterface class Application implements RouterAwareInterface, AuthenticationManagerAwareInterface
{ {
/** /**
* @var RouterInterface|null * @var RouterInterface|null
@ -57,7 +58,7 @@ class Application implements RouterAwareInterface
protected $accessCheckResolver = null; protected $accessCheckResolver = null;
/** /**
* @var AuthenticationManager|null * @var AuthenticationManagerInterface|null
*/ */
protected $authenticationManager = null; protected $authenticationManager = null;
@ -65,11 +66,13 @@ class Application implements RouterAwareInterface
* Class constructor. * Class constructor.
* *
* @param RouterInterface $router Appropriate router instance. * @param RouterInterface $router Appropriate router instance.
* @param AuthenticationManagerInterface $manager Appropriate authentication
* manager.
*/ */
public function __construct(RouterInterface $router) public function __construct(RouterInterface $router, AuthenticationManagerInterface $manager)
{ {
$this->router = $router; $this->router = $router;
$this->authenticationManager = new AuthenticationManager(); $this->authenticationManager = $manager;
$this->controllerResolver = new ControllerResolver( $this->controllerResolver = new ControllerResolver(
$this->router, $this->router,
$this->authenticationManager $this->authenticationManager
@ -91,9 +94,10 @@ class Application implements RouterAwareInterface
$this->getRouter()->setContext($context); $this->getRouter()->setContext($context);
// Actualize cookie factory in the authentication manager. // Actualize cookie factory in the authentication manager.
$authentication_manager = $this->getAuthenticationManager();
$cookie_factory = CookieFactory::fromRequest($request); $cookie_factory = CookieFactory::fromRequest($request);
$this->authenticationManager->setCookieFactory($cookie_factory); $authentication_manager->setCookieFactory($cookie_factory);
$this->authenticationManager->setOperatorFromRequest($request); $authentication_manager->setOperatorFromRequest($request);
try { try {
// Try to match a route, check if the client can access it and add // Try to match a route, check if the client can access it and add
@ -142,7 +146,7 @@ class Application implements RouterAwareInterface
// Attach operator's authentication info to the response to distinguish // Attach operator's authentication info to the response to distinguish
// him in the next requests. // him in the next requests.
$this->authenticationManager->attachOperatorToResponse($response); $authentication_manager->attachOperatorToResponse($response);
return $response; return $response;
} }
@ -168,6 +172,31 @@ class Application implements RouterAwareInterface
return $this->router; return $this->router;
} }
/**
* {@inheritdoc}
*/
public function setAuthenticationManager(AuthenticationManagerInterface $manager)
{
$this->authenticationManager = $manager;
// Update authentication manager in internal objects
if (!is_null($this->controllerResolver)) {
$this->controllerResolver->setAuthenticationManager($manager);
}
if (!is_null($this->accessCheckResolver)) {
$this->accessCheckResolver->setAuthenticationManager($manager);
}
}
/**
* {@inheritdoc}
*/
public function getAuthenticationManager()
{
return $this->authenticationManager;
}
/** /**
* Builds response for pages with denied access * Builds response for pages with denied access
* *