Skip to content

Commit

Permalink
Merge pull request #101 from damianz5/multiple-user-providers-support
Browse files Browse the repository at this point in the history
EZEE-1782: support for multiple user providers
  • Loading branch information
Łukasz Serwatka authored Nov 16, 2017
2 parents a35a244 + 664f571 commit 41313f1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 35 deletions.
68 changes: 45 additions & 23 deletions EventListener/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}
}
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 23 additions & 11 deletions Twig/RecommendationTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 41313f1

Please sign in to comment.