From 664f571fcab4b87f38f27359232d60a785e5e8f6 Mon Sep 17 00:00:00 2001 From: Damian Zabawa Date: Thu, 9 Nov 2017 18:41:33 +0100 Subject: [PATCH] EZEE-1782: support for multiple user providers https://jira.ez.no/browse/EZEE-1782 --- EventListener/Login.php | 68 ++++++++++++++++++---------- Resources/config/services.yml | 2 +- Twig/RecommendationTwigExtension.php | 34 +++++++++----- 3 files changed, 69 insertions(+), 35 deletions(-) diff --git a/EventListener/Login.php b/EventListener/Login.php index 943e96c..3139b83 100644 --- a/EventListener/Login.php +++ b/EventListener/Login.php @@ -6,6 +6,7 @@ namespace EzSystems\RecommendationBundle\EventListener; use eZ\Publish\API\Repository\UserService; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\HttpFoundation\Session\Session; @@ -75,34 +76,35 @@ public function setCustomerId($value) public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) { - if ( - $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in - || $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie + if (!$this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in + || !$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie ) { - if (!$event->getRequest()->cookies->has('yc-session-id')) { - $event->getRequest()->cookies->set('yc-session-id', $this->session->getId()); - } + return; + } - $notificationUri = sprintf($this->getNotificationEndpoint() . '%s/%s/%s', - 'login', - $event->getRequest()->cookies->get('yc-session-id'), - $this->userService->loadUserByLogin($event->getAuthenticationToken()->getUsername())->id - ); + if (!$event->getRequest()->cookies->has('yc-session-id')) { + $event->getRequest()->cookies->set('yc-session-id', $this->session->getId()); + } - if (isset($this->logger)) { - $this->logger->debug(sprintf('Send login event notification to YooChoose: %s', $notificationUri)); - } + $notificationUri = sprintf($this->getNotificationEndpoint() . '%s/%s/%s', + 'login', + $event->getRequest()->cookies->get('yc-session-id'), + $this->getUser($event->getAuthenticationToken()) + ); - try { - $response = $this->guzzleClient->get($notificationUri); + if (isset($this->logger)) { + $this->logger->debug(sprintf('Send login event notification to YooChoose: %s', $notificationUri)); + } + + try { + $response = $this->guzzleClient->get($notificationUri); - if (isset($this->logger)) { - $this->logger->debug(sprintf('Got %s from YooChoose login event notification', $response->getStatusCode())); - } - } catch (RequestException $e) { - if (isset($this->logger)) { - $this->logger->error(sprintf('YooChoose login event notification error: %s', $e->getMessage())); - } + if (isset($this->logger)) { + $this->logger->debug(sprintf('Got %s from YooChoose login event notification', $response->getStatusCode())); + } + } catch (RequestException $e) { + if (isset($this->logger)) { + $this->logger->error(sprintf('YooChoose login event notification error: %s', $e->getMessage())); } } } @@ -120,4 +122,24 @@ private function getNotificationEndpoint() $this->options['customerId'] ); } + + /** + * Returns current username or ApiUser id. + * + * @param TokenInterface $authenticationToken + * + * @return int|string + */ + private function getUser(TokenInterface $authenticationToken) + { + $user = $authenticationToken->getUser(); + + if (is_string($user)) { + return $user; + } elseif (method_exists($user, 'getAPIUser')) { + return $user->getAPIUser()->id; + } + + return $authenticationToken->getUsername(); + } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index a44e9f0..9e58a76 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -94,7 +94,7 @@ services: calls: - [setCustomerId, ['$yoochoose.customer_id;ez_recommendation$']] tags: - - { name: kernel.event_listener, event: security.interactive_login } + - { name: kernel.event_listener, event: security.interactive_login, priority: 255 } - { name: monolog.logger, channel: ez_recommendation } ez_recommendation.event_listener.session_backup: diff --git a/Twig/RecommendationTwigExtension.php b/Twig/RecommendationTwigExtension.php index fdcaf82..df47fdb 100644 --- a/Twig/RecommendationTwigExtension.php +++ b/Twig/RecommendationTwigExtension.php @@ -341,19 +341,31 @@ protected function getFeedbackUrl($outputContentTypeId) */ private function getCurrentUserId() { - if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') || // user has just logged in - $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { // user has logged in using remember_me cookie - return $this->userService->loadUserByLogin($this->tokenStorage->getToken()->getUsername())->id; - } else { - if (!$this->session->isStarted()) { - $this->session->start(); - } - $request = $this->requestStack->getMasterRequest(); - if (!$request->cookies->has('yc-session-id')) { - $request->cookies->set('yc-session-id', $this->session->getId()); + if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in + || $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie + ) { + $authenticationToken = $this->tokenStorage->getToken(); + $user = $authenticationToken->getUser(); + + if (is_string($user)) { + return $user; + } elseif (method_exists($user, 'getAPIUser')) { + return $user->getAPIUser()->id; } - return $request->cookies->get('yc-session-id'); + return $authenticationToken->getUsername(); + } + + if (!$this->session->isStarted()) { + $this->session->start(); } + + $request = $this->requestStack->getMasterRequest(); + + if (!$request->cookies->has('yc-session-id')) { + $request->cookies->set('yc-session-id', $this->session->getId()); + } + + return $request->cookies->get('yc-session-id'); } }