From 574377dea612a01d4d21cdf16c99c6a201d74ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Thu, 29 Oct 2015 12:51:34 +0100 Subject: [PATCH] Remove OAuthListener Response Listener after processing --- .../Core/Event/Listener/OAuthListener.php | 33 +------- .../Event/Listener/OAuthResponseListener.php | 80 +++++++++++++++++++ .../Phrasea/Controller/Api/ApiTestCase.php | 30 +++---- 3 files changed, 97 insertions(+), 46 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Core/Event/Listener/OAuthResponseListener.php diff --git a/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php b/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php index a9f76489fc..15ba978fa4 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php +++ b/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthListener.php @@ -24,7 +24,6 @@ use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\KernelEvents; @@ -135,39 +134,9 @@ class OAuthListener private function registerClosingAccountCallback(EventDispatcherInterface $dispatcher, Application $app) { - $dispatcher->addListener(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) { - $request = $event->getRequest(); - $response = $event->getResponse(); - - $session = $this->getSession($app); - /** @var ApiOauthToken $token */ - $token = $session->get('token'); - $this->getApiLogManipulator($app)->create($token->getAccount(), $request, $response); - $this->getApiOAuthTokenManipulator($app)->setLastUsed($token, new \DateTime()); - $session->set('token', null); - if (null !== $this->getAuthenticator($app)->getUser()) { - $this->getAuthenticator($app)->closeAccount(); - } - }, -20); + $dispatcher->addListener(KernelEvents::RESPONSE, new OAuthResponseListener($app), -20); } - /** - * @param Application $app - * @return ApiLogManipulator - */ - private function getApiLogManipulator(Application $app) - { - return $app['manipulator.api-log']; - } - - /** - * @param Application $app - * @return ApiOauthTokenManipulator - */ - private function getApiOAuthTokenManipulator(Application $app) - { - return $app['manipulator.api-oauth-token']; - } /** * @param Application $app diff --git a/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthResponseListener.php b/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthResponseListener.php new file mode 100644 index 0000000000..a780504678 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Event/Listener/OAuthResponseListener.php @@ -0,0 +1,80 @@ +app = $app; + } + + public function __invoke(FilterResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher) + { + $request = $event->getRequest(); + $response = $event->getResponse(); + + $session = $this->getSession(); + /** @var ApiOauthToken $token */ + $token = $session->get('token'); + $this->getApiLogManipulator()->create($token->getAccount(), $request, $response); + $this->getApiOAuthTokenManipulator()->setLastUsed($token, new \DateTime()); + $session->set('token', null); + if (null !== $this->getAuthenticator()->getUser()) { + $this->getAuthenticator()->closeAccount(); + } + + $dispatcher->removeListener($eventName, $this); + } + + /** + * @return ApiLogManipulator + */ + private function getApiLogManipulator() + { + return $this->app['manipulator.api-log']; + } + + /** + * @return ApiOauthTokenManipulator + */ + private function getApiOAuthTokenManipulator() + { + return $this->app['manipulator.api-oauth-token']; + } + + /** + * @return Session + */ + private function getSession() + { + return $this->app['session']; + } + + /** + * @return Authenticator + */ + private function getAuthenticator() + { + return $this->app['authentication']; + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php b/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php index 864c736a31..53349beed7 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php @@ -315,32 +315,34 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase { $this->setToken($this->userAccessToken); - self::$DI['client']->request('GET', '/api/v1/monitor/tasks/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $client = $this->getClient(); + + $client->request('GET', '/api/v1/monitor/tasks/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize($client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); - self::$DI['client']->request('GET', '/api/v1/monitor/scheduler/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $client->request('GET', '/api/v1/monitor/scheduler/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize($client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); - self::$DI['client']->request('GET', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $client->request('GET', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize($client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); - self::$DI['client']->request('POST', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $client->request('POST', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize($client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); - self::$DI['client']->request('POST', '/api/v1/monitor/task/1/start/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $client->request('POST', '/api/v1/monitor/task/1/start/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize($client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); - self::$DI['client']->request('POST', '/api/v1/monitor/task/1/stop/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $client->request('POST', '/api/v1/monitor/task/1/stop/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize($client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); - self::$DI['client']->request('GET', '/api/v1/monitor/phraseanet/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $client->request('GET', '/api/v1/monitor/phraseanet/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize($client->getResponse()->getContent()); $this->assertEquals(401, $content['meta']['http_code']); }