diff --git a/code_samples/user_management/in_memory/config/services.yaml b/code_samples/user_management/in_memory/config/services.yaml index 83124bea6a..b4783d8871 100644 --- a/code_samples/user_management/in_memory/config/services.yaml +++ b/code_samples/user_management/in_memory/config/services.yaml @@ -1,6 +1,7 @@ services: App\EventSubscriber\InteractiveLoginSubscriber: arguments: + $userProvider: '@ibexa.security.user_provider.username' $userMap: from_memory_user: customer from_memory_admin: admin diff --git a/code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php b/code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php index 82776991c6..f792727794 100644 --- a/code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php +++ b/code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php @@ -2,18 +2,21 @@ namespace App\EventSubscriber; -use Ibexa\Contracts\Core\Repository\UserService; -use Ibexa\Core\MVC\Symfony\Security\User; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Core\User\InMemoryUser; +use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Http\SecurityEvents; class InteractiveLoginSubscriber implements EventSubscriberInterface { - /** @param array $userMap */ + /** + * @param array $userMap + * + * @phpstan-param UserProviderInterface<\Ibexa\Core\MVC\Symfony\Security\UserInterface> $userProvider + */ public function __construct( - private readonly UserService $userService, + private readonly UserProviderInterface $userProvider, private readonly array $userMap = [], ) { } @@ -30,8 +33,8 @@ public function onInteractiveLogin(InteractiveLoginEvent $event): void $tokenUser = $event->getAuthenticationToken()->getUser(); if ($tokenUser instanceof InMemoryUser) { $userLogin = $this->userMap[$event->getAuthenticationToken()->getUserIdentifier()] ?? 'anonymous'; - $ibexaUser = $this->userService->loadUserByLogin($userLogin); - $event->getAuthenticationToken()->setUser(new User($ibexaUser)); + $ibexaSecurityUser = $this->userProvider->loadUserByIdentifier($userLogin); + $event->getAuthenticationToken()->setUser($ibexaSecurityUser); } } } diff --git a/deptrac.baseline.yaml b/deptrac.baseline.yaml index c551265dea..ccf0a25d06 100644 --- a/deptrac.baseline.yaml +++ b/deptrac.baseline.yaml @@ -104,8 +104,6 @@ deptrac: - Ibexa\FormBuilder\Event\FormEvents App\EventSubscriber\HelpMenuSubscriber: - Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent - App\EventSubscriber\InteractiveLoginSubscriber: - - Ibexa\Core\MVC\Symfony\Security\User App\EventSubscriber\MyMenuSubscriber: - Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent - Ibexa\AdminUi\Menu\MainMenuBuilder diff --git a/docs/users/user_authentication.md b/docs/users/user_authentication.md index a649b269bb..98fb45e107 100644 --- a/docs/users/user_authentication.md +++ b/docs/users/user_authentication.md @@ -26,8 +26,9 @@ The following example uses the [memory user provider]([[= symfony_doc =]]/securi maps memory user to Ibexa repository user, and [chains]([[= symfony_doc =]]/security/user_providers.html#chain-user-provider) with the Ibexa user provider to be able to use both: -Create as `src/EventSubscriber/InteractiveLoginSubscriber.php` subscribing to the `SecurityEvents::INTERACTIVE_LOGIN` event -and mapping when needed an in-memory authenticated user to an Ibexa user: +Create as `src/EventSubscriber/InteractiveLoginSubscriber.php` subscribing to the `SecurityEvents::INTERACTIVE_LOGIN` event, +mapping when needed an in-memory authenticated user to an Ibexa user, +and using the Ibexa `UsernameProvider` implementation of `UserProviderInterface::loadUserByIdentifier()` to have a repository user wrapped into a security user: ``` php [[= include_file('code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php') =]]