diff --git a/code_samples/user_management/in_memory/config/services.yaml b/code_samples/user_management/in_memory/config/services.yaml index a95531ace9..0aabbdc043 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\AuthenticationTokenCreatedSubscriber: arguments: + $userProvider: '@ibexa.security.user_provider.username' $userMap: from_memory_user: generic_customer from_memory_admin: admin diff --git a/code_samples/user_management/in_memory/src/EventSubscriber/AuthenticationTokenCreatedSubscriber.php b/code_samples/user_management/in_memory/src/EventSubscriber/AuthenticationTokenCreatedSubscriber.php index 4f351028c5..43b840fe70 100644 --- a/code_samples/user_management/in_memory/src/EventSubscriber/AuthenticationTokenCreatedSubscriber.php +++ b/code_samples/user_management/in_memory/src/EventSubscriber/AuthenticationTokenCreatedSubscriber.php @@ -7,14 +7,20 @@ use Ibexa\Core\MVC\Symfony\Security\UserWrapped; 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\AuthenticationTokenCreatedEvent; final readonly class AuthenticationTokenCreatedSubscriber implements EventSubscriberInterface { - /** @param array $userMap */ + /** + * @param array $userMap + * + * @phpstan-param UserProviderInterface<\Ibexa\Core\MVC\Symfony\Security\UserInterface> $userProvider + */ public function __construct( private readonly ConfigResolverInterface $configResolver, private readonly UserService $userService, + private readonly UserProviderInterface $userProvider, private readonly array $userMap = [], ) { } @@ -36,7 +42,7 @@ public function onAuthenticationTokenCreated(AuthenticationTokenCreatedEvent $ev $userIdentifier = $token->getUserIdentifier(); $ibexaUser = null; if (array_key_exists($userIdentifier, $this->userMap)) { - $ibexaUser = $this->userService->loadUserByLogin($this->userMap[$userIdentifier]); + $ibexaUser = $this->userProvider->loadUserByIdentifier($userIdentifier); } if (null === $ibexaUser) { $anonymousUserId = (int)$this->configResolver->getParameter('anonymous_user_id'); diff --git a/docs/users/user_authentication.md b/docs/users/user_authentication.md index 8d24b4aa41..35c50a81cf 100644 --- a/docs/users/user_authentication.md +++ b/docs/users/user_authentication.md @@ -32,8 +32,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/AuthenticationTokenCreatedSubscriber.php` subscribing to the `AuthenticationTokenCreatedEvent` event -and mapping when needed an in-memory authenticated user to an Ibexa user: +Create as `src/EventSubscriber/AuthenticationTokenCreatedSubscriber.php` subscribing to the `AuthenticationTokenCreatedEvent` 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/AuthenticationTokenCreatedSubscriber.php') =]]