Skip to content

[ALT] Update "User authentication" customization example (5.0)#3088

Draft
adriendupuis wants to merge 12 commits intouser_auth_5.0from
user_auth_5.0-alt
Draft

[ALT] Update "User authentication" customization example (5.0)#3088
adriendupuis wants to merge 12 commits intouser_auth_5.0from
user_auth_5.0-alt

Conversation

@adriendupuis
Copy link
Contributor

@adriendupuis adriendupuis commented Mar 13, 2026

Question Answer
JIRA Ticket
Versions
Edition

Alternative implementation of InteractiveLoginSubscriber from #3087

Checklist

  • Text renders correctly
  • Text has been checked with vale
  • Description metadata is up to date
  • Redirects cover removed/moved pages
  • Code samples are working
  • PHP code samples have been fixed with PHP CS fixer
  • Added link to this PR in relevant JIRA ticket or code PR

@github-actions
Copy link

github-actions bot commented Mar 13, 2026

Preview of modified files

Preview of modified Markdown:

@adriendupuis adriendupuis changed the title InteractiveLoginSubscriber.php: Use UsernameProvider [ALT] Update "User authentication" customization example (5.0) Mar 13, 2026
@github-actions
Copy link

code_samples/ change report

Before (on target branch)After (in current PR)

code_samples/user_management/in_memory/config/services.yaml


code_samples/user_management/in_memory/config/services.yaml

docs/users/user_authentication.md@49:``` yaml
docs/users/user_authentication.md@50:[[= include_file('code_samples/user_management/in_memory/config/services.yaml') =]]
docs/users/user_authentication.md@51:```
docs/users/user_authentication.md@50:``` yaml
docs/users/user_authentication.md@51:[[= include_file('code_samples/user_management/in_memory/config/services.yaml') =]]
docs/users/user_authentication.md@52:```

001⫶services:
002⫶ App\EventSubscriber\InteractiveLoginSubscriber:
003⫶ arguments:

001⫶services:
002⫶ App\EventSubscriber\InteractiveLoginSubscriber:
003⫶ arguments:
004⫶            $userMap:
005⫶ from_memory_user: customer
006⫶ from_memory_admin: admin
004⫶            $userProvider: '@ibexa.security.user_provider.username'
005⫶ $userMap:
006⫶ from_memory_user: customer
007⫶ from_memory_admin: admin


code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php



code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php

docs/users/user_authentication.md@32:``` php
docs/users/user_authentication.md@33:[[= include_file('code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php') =]]
docs/users/user_authentication.md@34:```
docs/users/user_authentication.md@33:``` php
docs/users/user_authentication.md@34:[[= include_file('code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php') =]]
docs/users/user_authentication.md@35:```

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\EventSubscriber;
004⫶

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\EventSubscriber;
004⫶
005⫶use Ibexa\Contracts\Core\Repository\UserService;
006⫶use Ibexa\Core\MVC\Symfony\Security\User;
007⫶use Symfony\Component\EventDispatcher\EventSubscriberInterface;
008⫶use Symfony\Component\Security\Core\User\InMemoryUser;
009⫶use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
010⫶use Symfony\Component\Security\Http\SecurityEvents;
011⫶
012⫶class InteractiveLoginSubscriber implements EventSubscriberInterface
013⫶{
014⫶ /** @param array<string, string> $userMap */
015⫶ public function __construct(
016⫶ private readonly UserService $userService,
017⫶ private readonly array $userMap = [],
018⫶ ) {
019⫶ }
020⫶
021⫶ public static function getSubscribedEvents(): array
022⫶ {
023⫶ return [
024⫶ SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
025⫶ ];
026⫶ }
027⫶
028⫶ public function onInteractiveLogin(InteractiveLoginEvent $event): void
029⫶ {
030⫶ $tokenUser = $event->getAuthenticationToken()->getUser();
031⫶ if ($tokenUser instanceof InMemoryUser) {
032⫶ $userLogin = $this->userMap[$event->getAuthenticationToken()->getUserIdentifier()] ?? 'anonymous';
033⫶ $ibexaUser = $this->userService->loadUserByLogin($userLogin);
034⫶ $event->getAuthenticationToken()->setUser(new User($ibexaUser));
035⫶ }
036⫶ }
037⫶}
005⫶use Symfony\Component\EventDispatcher\EventSubscriberInterface;
006⫶use Symfony\Component\Security\Core\User\InMemoryUser;
007⫶use Symfony\Component\Security\Core\User\UserProviderInterface;
008⫶use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
009⫶use Symfony\Component\Security\Http\SecurityEvents;
010⫶
011⫶class InteractiveLoginSubscriber implements EventSubscriberInterface
012⫶{
013⫶ /**
014⫶ * @param array<string, string> $userMap
015⫶ * @phpstan-param UserProviderInterface<\Ibexa\Core\MVC\Symfony\Security\UserInterface> $userProvider
016⫶ */
017⫶ public function __construct(
018⫶ private readonly UserProviderInterface $userProvider,
019⫶ private readonly array $userMap = [],
020⫶ ) {
021⫶ }
022⫶
023⫶ public static function getSubscribedEvents(): array
024⫶ {
025⫶ return [
026⫶ SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
027⫶ ];
028⫶ }
029⫶
030⫶ public function onInteractiveLogin(InteractiveLoginEvent $event): void
031⫶ {
032⫶ $tokenUser = $event->getAuthenticationToken()->getUser();
033⫶ if ($tokenUser instanceof InMemoryUser) {
034⫶ $userLogin = $this->userMap[$event->getAuthenticationToken()->getUserIdentifier()] ?? 'anonymous';
035⫶ $ibexaSecurityUser = $this->userProvider->loadUserByIdentifier($userLogin);
036⫶ $event->getAuthenticationToken()->setUser($ibexaSecurityUser);
037⫶ }
038⫶ }
039⫶}


Download colorized diff

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant