Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
App\EventSubscriber\InteractiveLoginSubscriber:
arguments:
$userProvider: '@ibexa.security.user_provider.username'
$userMap:
from_memory_user: customer
from_memory_admin: admin
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> $userMap */
/**
* @param array<string, string> $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 = [],
) {
}
Expand All @@ -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);
}
}
}
2 changes: 0 additions & 2 deletions deptrac.baseline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions docs/users/user_authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -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') =]]
Expand Down