Bonjour,
J'aurai besoin d'aide pour mettre en place un guard authenticator. J'ai suivi la documentation de symfony mais j'ai un peu de mal. Mon but c'est d'utiliser un ChainProvider pour l'authentification sur mon site web, un utilisateur peut se connecter via LDAP ou via la BDD. J'ai mis en place la connexion LDAP, elle marche parfaitement, mais je n'arrive pas à lier la connexion BDD. Ce que je voudrais c'est que sur le même formulaire de login, Symfony vérifie LDAP puis BDD. Voici mon code pour l'instant
security.yaml
Code yaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
security:
    providers:
        chain_provider:
            chain:
                providers: [intranet_ldap, from_database]
        from_database:
            entity:
                class: App\Entity\User
                property: username
        fos_userbundle:
            id: fos_user.user_provider.username
        intranet_ldap:
            id: App\Security\LdapUserProvider
 
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:          true
            #switch_user: true
        main:
            guard:
                authenticators:
                    - App\Security\TokenAuthenticator
            provider:       chain_provider
            pattern:            ^/
            context:            user
            anonymous:          true
            form_login_ldap:
                service:        Symfony\Component\Ldap\Ldap
                login_path:     /login
                check_path:     /login_check
                dn_string:      'DC=xxx, DC=lan'
                query_string:   '(&(sAMAccountName={username}))'
                use_forward:    false
                always_use_default_target_path: true
                default_target_path: /profile
                use_referer:    true
            logout:
                path:  /logout
                target: /
 
 
    access_control:
        - { path: ^/efconnect, role: ROLE_USER }
        - { path: ^/elfinder, role: ROLE_USER }
        - { path: ^/profile/, role: ROLE_USER }
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        #- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/logout, role: IS_AUTHENTICATED_ANONYMOUSLY }
 
        # Admin login page needs to be accessed without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
 
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/efconnect, role: ROLE_USER }
        - { path: ^/elfinder, role: ROLE_USER }
        - { path: ^/blog/post/create$, role: ROLE_USER }
        - { path: ^/blog/post/edit, role: ROLE_USER }
        - { path: ^/blog/post/delete, role: ROLE_USER }
        - { path: ^/organigrammes/candidats, role: [ROLE_ALGAM_INTRANET_ADMIN_MEMBRE_STAFF, ROLE_SUPER_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_ALLOWED_TO_SWITCH]
 
 
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512
 
acl:
    connection: default
TokenAuthenticator.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
 
 
namespace App\Security;
 
 
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
 
class TokenAuthenticator extends AbstractGuardAuthenticator
{
 
    public function start(Request $request, AuthenticationException $authException = null)
    {
        $data = array(
            'message' => 'Authentification requise'
        );
 
        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
    }
 
    public function supports(Request $request)
    {
        return $request->headers->has('X-AUTH-TOKEN');
    }
 
    public function getCredentials(Request $request)
    {
        return array(
            'token' => $request->headers->get('X-AUTH-TOKEN'),
        );
    }
 
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $apiKey = $credentials['token'];
 
        if(null === $apiKey){
            return;
        }
 
        return $userProvider->loadUserByUsername($credentials['username']);
    }
 
    public function checkCredentials($credentials, UserInterface $user)
    {
        // TODO: Implement checkCredentials() method.
    }
 
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        $data = array(
            'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
        );
 
        return new JsonResponse($data, Response::HTTP_FORBIDDEN);
    }
 
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        return null;
    }
 
    public function supportsRememberMe()
    {
        return false;
    }
}
J'ai aussi dans mon rep Securirty LdapUser.php et LdapUserProvider.php mais je ne pense pas que ce soit utile donc je ne les fournis pas pour le moment. Est-ce que je dois créé un UserProvider pour la BDD aussi?
Quelqu'un pourrait m'aider pour mettre en place la connexion avec la BDD en parallèle avec le LDAP? Je suis vraiment bloquée et je n'arrive pas à adapter les exemples que je trouve sur le net. Merci d'avance!