/
AnonymousCustomerUserAuthenticationProvider.php
65 lines (56 loc) · 2.21 KB
/
AnonymousCustomerUserAuthenticationProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Oro\Bundle\CustomerBundle\Security;
use Oro\Bundle\CustomerBundle\Entity\CustomerVisitorManager;
use Oro\Bundle\CustomerBundle\Security\Token\AnonymousCustomerUserToken;
use Oro\Bundle\SecurityBundle\Authentication\Token\RolesAwareTokenInterface;
use Oro\Bundle\WebsiteBundle\Manager\WebsiteManager;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
/**
* The authentication provider for the storefront anonymous user.
*/
class AnonymousCustomerUserAuthenticationProvider implements AuthenticationProviderInterface
{
private CustomerVisitorManager $visitorManager;
private WebsiteManager $websiteManager;
private int $updateLatency;
public function __construct(
CustomerVisitorManager $visitorManager,
WebsiteManager $websiteManager,
$updateLatency
) {
$this->visitorManager = $visitorManager;
$this->websiteManager = $websiteManager;
$this->updateLatency = $updateLatency;
}
/**
* {@inheritDoc}
*/
public function supports(TokenInterface $token)
{
return $token instanceof AnonymousCustomerUserToken && $token->getCredentials();
}
/**
* {@inheritDoc}
*/
public function authenticate(TokenInterface $token)
{
$website = $this->websiteManager->getCurrentWebsite();
if (null === $website) {
throw new AuthenticationException('The current website cannot be found.');
}
$organization = $website->getOrganization();
if (null === $organization) {
throw new AuthenticationException('The current website is not assigned to an organization.');
}
$credentials = $token->getCredentials();
$visitor = $this->visitorManager->findOrCreate($credentials['visitor_id'], $credentials['session_id']);
return new AnonymousCustomerUserToken(
$token->getUser(),
$token instanceof RolesAwareTokenInterface ? $token->getRoles() : $token->getRoleNames(),
$visitor,
$organization
);
}
}