-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathAuthenticationTokenCreatedSubscriber.php
More file actions
47 lines (42 loc) · 1.73 KB
/
AuthenticationTokenCreatedSubscriber.php
File metadata and controls
47 lines (42 loc) · 1.73 KB
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
<?php declare(strict_types=1);
namespace App\EventSubscriber;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\MVC\Symfony\Security\UserWrapped;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent;
final readonly class AuthenticationTokenCreatedSubscriber implements EventSubscriberInterface
{
/** @param array<string, string> $userMap */
public function __construct(
private readonly ConfigResolverInterface $configResolver,
private readonly UserService $userService,
private readonly array $userMap = [],
) {
}
public static function getSubscribedEvents(): array
{
return [
AuthenticationTokenCreatedEvent::class => ['onAuthenticationTokenCreated', 11],
];
}
public function onAuthenticationTokenCreated(AuthenticationTokenCreatedEvent $event): void
{
$token = $event->getAuthenticatedToken();
$tokenUser = $token->getUser();
if (!$tokenUser instanceof InMemoryUser) {
return;
}
$userIdentifier = $token->getUserIdentifier();
$ibexaUser = null;
if (array_key_exists($userIdentifier, $this->userMap)) {
$ibexaUser = $this->userService->loadUserByLogin($this->userMap[$userIdentifier]);
}
if (null === $ibexaUser) {
$anonymousUserId = (int)$this->configResolver->getParameter('anonymous_user_id');
$ibexaUser = $this->userService->loadUser($anonymousUserId);
}
$token->setUser(new UserWrapped($tokenUser, $ibexaUser));
}
}