-
Notifications
You must be signed in to change notification settings - Fork 81
Update "User authentication" customization example (4.6) #3086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.6
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # config/packages/security.yaml | ||
| security: | ||
| providers: | ||
| # Chaining in_memory and ibexa user providers | ||
| chain_provider: | ||
| chain: | ||
| providers: [in_memory, ibexa] | ||
| ibexa: | ||
| id: ibexa.security.user_provider | ||
| in_memory: | ||
| memory: | ||
| users: | ||
| # You will then be able to login with username "user" and password "userpass" | ||
| user: { password: userpass, roles: [ 'ROLE_USER' ] } | ||
| # The "in memory" provider requires an encoder for Symfony\Component\Security\Core\User\User | ||
| password_hashers: | ||
| Symfony\Component\Security\Core\User\User: plaintext |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| services: | ||
| App\EventListener\InteractiveLoginSubscriber: | ||
| arguments: ['@ibexa.api.service.user'] | ||
| tags: | ||
| - { name: kernel.event_subscriber } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||||
| <?php declare(strict_types=1); | ||||||||||||
|
|
||||||||||||
| namespace App\EventSubscriber; | ||||||||||||
|
|
||||||||||||
| use Ibexa\Contracts\Core\Repository\UserService; | ||||||||||||
| use Ibexa\Core\MVC\Symfony\Event\InteractiveLoginEvent; | ||||||||||||
| use Ibexa\Core\MVC\Symfony\MVCEvents; | ||||||||||||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||||||||||||
|
|
||||||||||||
| class InteractiveLoginSubscriber implements EventSubscriberInterface | ||||||||||||
| { | ||||||||||||
| /** | ||||||||||||
| * @var \Ibexa\Contracts\Core\Repository\UserService | ||||||||||||
| */ | ||||||||||||
| private $userService; | ||||||||||||
|
Comment on lines
+12
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| public function __construct(UserService $userService) | ||||||||||||
| { | ||||||||||||
| $this->userService = $userService; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| public static function getSubscribedEvents() | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| { | ||||||||||||
| return [ | ||||||||||||
| MVCEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin', | ||||||||||||
| ]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| public function onInteractiveLogin(InteractiveLoginEvent $event): void | ||||||||||||
| { | ||||||||||||
| // This loads a generic User and assigns it back to the event. | ||||||||||||
| // You may want to create Users here, or even load predefined Users depending on your own rules. | ||||||||||||
| $event->setApiUser($this->userService->loadUserByLogin('generic_user')); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,73 +38,19 @@ You can override `getUser()` to return whatever user class you want, as long a | |
| The following is an example of using the in-memory user provider: | ||
|
|
||
| ``` yaml | ||
| # config/packages/security.yaml | ||
| security: | ||
| providers: | ||
| # Chaining in_memory and ibexa user providers | ||
| chain_provider: | ||
| chain: | ||
| providers: [in_memory, ibexa] | ||
| ibexa: | ||
| id: ibexa.security.user_provider | ||
| in_memory: | ||
| memory: | ||
| users: | ||
| # You will then be able to login with username "user" and password "userpass" | ||
| user: { password: userpass, roles: [ 'ROLE_USER' ] } | ||
| # The "in memory" provider requires an encoder for Symfony\Component\Security\Core\User\User | ||
| encoders: | ||
| Symfony\Component\Security\Core\User\User: plaintext | ||
| [[= include_file('code_samples/user_management/in_memory/config/packages/security.yaml') =]] | ||
| ``` | ||
|
|
||
| ### Implement the listener | ||
|
|
||
| In the `config/services.yaml` file: | ||
|
|
||
| ``` yaml | ||
| services: | ||
| App\EventListener\InteractiveLoginListener: | ||
| arguments: ['@ibexa.api.service.user'] | ||
| tags: | ||
| - { name: kernel.event_subscriber } | ||
| [[= include_file('code_samples/user_management/in_memory/config/services.yaml') =]] | ||
| ``` | ||
|
|
||
| Don't mix `MVCEvents::INTERACTIVE_LOGIN` event (specific to [[= product_name =]]) and `SecurityEvents::INTERACTIVE_LOGIN` event (fired by Symfony security component). | ||
|
|
||
| ``` php | ||
| <?php | ||
|
|
||
| namespace App\EventListener; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\UserService; | ||
| use eIbexa\Core\MVC\Symfony\Event\InteractiveLoginEvent; | ||
| use Ibexa\Core\MVC\Symfony\MVCEvents; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
|
||
| class InteractiveLoginListener implements EventSubscriberInterface | ||
| { | ||
| /** | ||
| * @var \Ibexa\Contracts\Core\Repository\UserService | ||
| */ | ||
| private $userService; | ||
|
|
||
| public function __construct(UserService $userService) | ||
| { | ||
| $this->userService = $userService; | ||
| } | ||
|
|
||
| public static function getSubscribedEvents() | ||
| { | ||
| return [ | ||
| MVCEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin' | ||
| ]; | ||
| } | ||
|
|
||
| public function onInteractiveLogin(InteractiveLoginEvent $event) | ||
| { | ||
| // This loads a generic User and assigns it back to the event. | ||
| // You may want to create Users here, or even load predefined Users depending on your own rules. | ||
| $event->setApiUser($this->userService->loadUserByLogin( 'lolautruche' )); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Big up to @lolautruche! |
||
| } | ||
| } | ||
| [[= include_file('code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php') =]] | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.