Skip to content

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

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

[ALT] Update "User authentication" customization example (5.0)#3088
adriendupuis wants to merge 2 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

Preview of modified files: no change to preview.

@sonarqubecloud
Copy link

❌ The last analysis has failed.

See analysis details on SonarQube Cloud

@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

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:```

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

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:```

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

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:```

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


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:```

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()
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⫶ /** @param array<string, string> $userMap */
014⫶ public function __construct(
015⫶ private readonly UserProviderInterface $userProvider,
016⫶ private readonly array $userMap = [],
017⫶ ) {
018⫶ }
019⫶
020⫶ public static function getSubscribedEvents()
021⫶ {
022⫶ return [
023⫶ SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
024⫶ ];
025⫶ }
026⫶
027⫶ public function onInteractiveLogin(InteractiveLoginEvent $event): void
028⫶ {
029⫶ $tokenUser = $event->getAuthenticationToken()->getUser();
030⫶ if ($tokenUser instanceof InMemoryUser) {
031⫶ $userLogin = $this->userMap[$event->getAuthenticationToken()->getUserIdentifier()] ?? 'anonymous';
032⫶ $wrappedUser = $this->userProvider->loadUserByIdentifier($userLogin);
033⫶ $event->getAuthenticationToken()->setUser($wrappedUser);
034⫶ }
035⫶ }
036⫶}


Download colorized diff

services:
App\EventSubscriber\InteractiveLoginSubscriber:
arguments:
$userProvider: '@ibexa.security.user_provider.username'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This the verbose name (to go along side ibexa.security.user_provider.email). But in fact, this shortcut could be used:

Suggested change
$userProvider: '@ibexa.security.user_provider.username'
$userProvider: '@ibexa.security.user_provider'

Comment on lines +32 to +33
$wrappedUser = $this->userProvider->loadUserByIdentifier($userLogin);
$event->getAuthenticationToken()->setUser($wrappedUser);
Copy link
Contributor Author

@adriendupuis adriendupuis Mar 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$wrappedUser = $this->userProvider->loadUserByIdentifier($userLogin);
$event->getAuthenticationToken()->setUser($wrappedUser);
$ibexaSecurityUser = $this->userProvider->loadUserByIdentifier($userLogin);
$event->getAuthenticationToken()->setUser($ibexaSecurityUser);

@adriendupuis adriendupuis changed the title InteractiveLoginSubscriber.php: Use UsernameProvider [ALT] Update "User authentication" customization example (5.0) Mar 13, 2026
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