Skip to content

Commit dc09c2f

Browse files
Update "User authentication" customization example (4.6) (#3086)
* user_authentication.md: Move code to files * code_samples.yaml: PHP 8.2 PKSA-fs5b-x5k4-1h39 * No need to declare the service --------- Co-authored-by: adriendupuis <adriendupuis@users.noreply.github.com> Co-authored-by: Konrad Oboza <konrad.oboza@ibexa.co>
1 parent 4d1f4a4 commit dc09c2f

4 files changed

Lines changed: 60 additions & 62 deletions

File tree

.github/workflows/code_samples.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ jobs:
7878
"PKSA-xx6c-6d96-db2w": "As this is for code quality tests and not to run a production DXP, this can be ignored."
7979
}'
8080
81+
- name: Ignore audit advisory for PHP 8.2
82+
if: matrix.php == '8.2'
83+
run: |
84+
composer config audit.ignore --json '{
85+
"PKSA-fs5b-x5k4-1h39": "As this is for code quality tests and not to run a production DXP, this can be ignored."
86+
}'
87+
8188
- uses: ramsey/composer-install@v3
8289
with:
8390
dependency-versions: highest
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# config/packages/security.yaml
2+
security:
3+
providers:
4+
# Chaining in_memory and ibexa user providers
5+
chain_provider:
6+
chain:
7+
providers: [in_memory, ibexa]
8+
ibexa:
9+
id: ibexa.security.user_provider
10+
in_memory:
11+
memory:
12+
users:
13+
# You will then be able to login with username "user" and password "userpass"
14+
user: { password: userpass, roles: [ 'ROLE_USER' ] }
15+
# The "in memory" provider requires an encoder for Symfony\Component\Security\Core\User\User
16+
password_hashers:
17+
Symfony\Component\Security\Core\User\User: plaintext
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\EventSubscriber;
4+
5+
use Ibexa\Contracts\Core\Repository\UserService;
6+
use Ibexa\Core\MVC\Symfony\Event\InteractiveLoginEvent;
7+
use Ibexa\Core\MVC\Symfony\MVCEvents;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
final class InteractiveLoginSubscriber implements EventSubscriberInterface
11+
{
12+
private UserService $userService;
13+
14+
public function __construct(UserService $userService)
15+
{
16+
$this->userService = $userService;
17+
}
18+
19+
public static function getSubscribedEvents(): array
20+
{
21+
return [
22+
MVCEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
23+
];
24+
}
25+
26+
public function onInteractiveLogin(InteractiveLoginEvent $event): void
27+
{
28+
// This loads a generic User and assigns it back to the event.
29+
// You may want to create Users here, or even load predefined Users depending on your own rules.
30+
$event->setApiUser($this->userService->loadUserByLogin('generic_user'));
31+
}
32+
}

docs/users/user_authentication.md

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -38,73 +38,15 @@ You can override `getUser()` to return whatever user class you want, as long a
3838
The following is an example of using the in-memory user provider:
3939

4040
``` yaml
41-
# config/packages/security.yaml
42-
security:
43-
providers:
44-
# Chaining in_memory and ibexa user providers
45-
chain_provider:
46-
chain:
47-
providers: [in_memory, ibexa]
48-
ibexa:
49-
id: ibexa.security.user_provider
50-
in_memory:
51-
memory:
52-
users:
53-
# You will then be able to login with username "user" and password "userpass"
54-
user: { password: userpass, roles: [ 'ROLE_USER' ] }
55-
# The "in memory" provider requires an encoder for Symfony\Component\Security\Core\User\User
56-
encoders:
57-
Symfony\Component\Security\Core\User\User: plaintext
41+
[[= include_file('code_samples/user_management/in_memory/config/packages/security.yaml') =]]
5842
```
5943

6044
### Implement the listener
6145

62-
In the `config/services.yaml` file:
63-
64-
``` yaml
65-
services:
66-
App\EventListener\InteractiveLoginListener:
67-
arguments: ['@ibexa.api.service.user']
68-
tags:
69-
- { name: kernel.event_subscriber } 
70-
```
71-
7246
Don't mix `MVCEvents::INTERACTIVE_LOGIN` event (specific to [[= product_name =]]) and `SecurityEvents::INTERACTIVE_LOGIN` event (fired by Symfony security component).
7347

7448
``` php
75-
<?php
76-
77-
namespace App\EventListener;
78-
79-
use Ibexa\Contracts\Core\Repository\UserService;
80-
use eIbexa\Core\MVC\Symfony\Event\InteractiveLoginEvent;
81-
use Ibexa\Core\MVC\Symfony\MVCEvents;
82-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
83-
84-
class InteractiveLoginListener implements EventSubscriberInterface
85-
{
86-
/**
87-
* @var \Ibexa\Contracts\Core\Repository\UserService
88-
*/
89-
private $userService;
90-
91-
public function __construct(UserService $userService)
92-
{
93-
$this->userService = $userService;
94-
}
95-
96-
public static function getSubscribedEvents()
97-
{
98-
return [
99-
MVCEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin'
100-
];
101-
}
102-
103-
public function onInteractiveLogin(InteractiveLoginEvent $event)
104-
{
105-
// This loads a generic User and assigns it back to the event.
106-
// You may want to create Users here, or even load predefined Users depending on your own rules.
107-
$event->setApiUser($this->userService->loadUserByLogin( 'lolautruche' ));
108-
}
109-
49+
[[= include_file('code_samples/user_management/in_memory/src/EventSubscriber/InteractiveLoginSubscriber.php') =]]
11050
```
51+
52+
It's automatically tagged `kernel.event_subscriber` as implementing the `EventSubscriberInterface` and the user service injection is auto-wired.

0 commit comments

Comments
 (0)