Skip to content

Commit 41313f1

Browse files
author
Łukasz Serwatka
authored
Merge pull request #101 from damianz5/multiple-user-providers-support
EZEE-1782: support for multiple user providers
2 parents a35a244 + 664f571 commit 41313f1

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

EventListener/Login.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace EzSystems\RecommendationBundle\EventListener;
77

88
use eZ\Publish\API\Repository\UserService;
9+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
910
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
1011
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
1112
use Symfony\Component\HttpFoundation\Session\Session;
@@ -75,34 +76,35 @@ public function setCustomerId($value)
7576

7677
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
7778
{
78-
if (
79-
$this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in
80-
|| $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie
79+
if (!$this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in
80+
|| !$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie
8181
) {
82-
if (!$event->getRequest()->cookies->has('yc-session-id')) {
83-
$event->getRequest()->cookies->set('yc-session-id', $this->session->getId());
84-
}
82+
return;
83+
}
8584

86-
$notificationUri = sprintf($this->getNotificationEndpoint() . '%s/%s/%s',
87-
'login',
88-
$event->getRequest()->cookies->get('yc-session-id'),
89-
$this->userService->loadUserByLogin($event->getAuthenticationToken()->getUsername())->id
90-
);
85+
if (!$event->getRequest()->cookies->has('yc-session-id')) {
86+
$event->getRequest()->cookies->set('yc-session-id', $this->session->getId());
87+
}
9188

92-
if (isset($this->logger)) {
93-
$this->logger->debug(sprintf('Send login event notification to YooChoose: %s', $notificationUri));
94-
}
89+
$notificationUri = sprintf($this->getNotificationEndpoint() . '%s/%s/%s',
90+
'login',
91+
$event->getRequest()->cookies->get('yc-session-id'),
92+
$this->getUser($event->getAuthenticationToken())
93+
);
9594

96-
try {
97-
$response = $this->guzzleClient->get($notificationUri);
95+
if (isset($this->logger)) {
96+
$this->logger->debug(sprintf('Send login event notification to YooChoose: %s', $notificationUri));
97+
}
98+
99+
try {
100+
$response = $this->guzzleClient->get($notificationUri);
98101

99-
if (isset($this->logger)) {
100-
$this->logger->debug(sprintf('Got %s from YooChoose login event notification', $response->getStatusCode()));
101-
}
102-
} catch (RequestException $e) {
103-
if (isset($this->logger)) {
104-
$this->logger->error(sprintf('YooChoose login event notification error: %s', $e->getMessage()));
105-
}
102+
if (isset($this->logger)) {
103+
$this->logger->debug(sprintf('Got %s from YooChoose login event notification', $response->getStatusCode()));
104+
}
105+
} catch (RequestException $e) {
106+
if (isset($this->logger)) {
107+
$this->logger->error(sprintf('YooChoose login event notification error: %s', $e->getMessage()));
106108
}
107109
}
108110
}
@@ -120,4 +122,24 @@ private function getNotificationEndpoint()
120122
$this->options['customerId']
121123
);
122124
}
125+
126+
/**
127+
* Returns current username or ApiUser id.
128+
*
129+
* @param TokenInterface $authenticationToken
130+
*
131+
* @return int|string
132+
*/
133+
private function getUser(TokenInterface $authenticationToken)
134+
{
135+
$user = $authenticationToken->getUser();
136+
137+
if (is_string($user)) {
138+
return $user;
139+
} elseif (method_exists($user, 'getAPIUser')) {
140+
return $user->getAPIUser()->id;
141+
}
142+
143+
return $authenticationToken->getUsername();
144+
}
123145
}

Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ services:
9494
calls:
9595
- [setCustomerId, ['$yoochoose.customer_id;ez_recommendation$']]
9696
tags:
97-
- { name: kernel.event_listener, event: security.interactive_login }
97+
- { name: kernel.event_listener, event: security.interactive_login, priority: 255 }
9898
- { name: monolog.logger, channel: ez_recommendation }
9999

100100
ez_recommendation.event_listener.session_backup:

Twig/RecommendationTwigExtension.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,31 @@ protected function getFeedbackUrl($outputContentTypeId)
341341
*/
342342
private function getCurrentUserId()
343343
{
344-
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') || // user has just logged in
345-
$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { // user has logged in using remember_me cookie
346-
return $this->userService->loadUserByLogin($this->tokenStorage->getToken()->getUsername())->id;
347-
} else {
348-
if (!$this->session->isStarted()) {
349-
$this->session->start();
350-
}
351-
$request = $this->requestStack->getMasterRequest();
352-
if (!$request->cookies->has('yc-session-id')) {
353-
$request->cookies->set('yc-session-id', $this->session->getId());
344+
if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in
345+
|| $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie
346+
) {
347+
$authenticationToken = $this->tokenStorage->getToken();
348+
$user = $authenticationToken->getUser();
349+
350+
if (is_string($user)) {
351+
return $user;
352+
} elseif (method_exists($user, 'getAPIUser')) {
353+
return $user->getAPIUser()->id;
354354
}
355355

356-
return $request->cookies->get('yc-session-id');
356+
return $authenticationToken->getUsername();
357+
}
358+
359+
if (!$this->session->isStarted()) {
360+
$this->session->start();
357361
}
362+
363+
$request = $this->requestStack->getMasterRequest();
364+
365+
if (!$request->cookies->has('yc-session-id')) {
366+
$request->cookies->set('yc-session-id', $this->session->getId());
367+
}
368+
369+
return $request->cookies->get('yc-session-id');
358370
}
359371
}

0 commit comments

Comments
 (0)