Partage
  • Partager sur Facebook
  • Partager sur Twitter

[SF4] Login LDAP - rien ne se passe

7 mai 2021 à 10:54:34

Bonjour,

Alors voilà jusqu'à présent je m'authentifiait à LDPA avec un fonction manuelle, sans utiliser les ofnctionnalités de symfony (ca marche très bien)

Maintenant, pour gérer l'accès aux pages, je voulais l'implémenter.

J'ai donc suivi plusieurs liens de doc officielle.

J'ai créé mon formulaire (https://symfony.com/doc/4.4/security/form_login_setup.html#generating-the-login-form

{% extends 'base.html.twig' %}

{% block title %}
    {{ parent() ~ ' | ' ~ "signin.title"|trans}}
{% endblock %}

{% block content %}
    <div id="login">
        <article id="Connexion">
            <h2>{{ "login.subtitle"|trans }}</h2> 
            {% if app.session.get('Login') %}
                <div class="mb-3">
                    {{ 'alreadyLogged'|trans({'%username%':app.session.get('Login')}) }}
                    <a href="{{ path('app_security_logout') }}">{{ "user.logout"|trans({},'menus') }}</a>
                </div>
            {% else %}
                <form method="post">
                    {% if error %}
                        <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
                    {% endif %}

                    {% if app.user %}
                        <div class="mb-3">
                            You are logged in as {{ app.user.username }}, <a href="{{ path('app_logout') }}">Logout</a>
                        </div>
                    {% endif %}
                    <label for="matricule">{{ "login.label.login"|trans({}, "forms") }}</label>
                    <input type="text" value="{{ last_username }}" name="matricule" id="matricule" class="form-control" required autofocus>
                    <label for="inputPassword">{{ "login.label.password"|trans({}, "forms") }}</label>
                    <input type="password" name="password" id="inputPassword" class="form-control" required>

                    <input type="hidden" name="_csrf_token"
                           value="{{ csrf_token('authenticate') }}"
                           >
                    <button class="btn btn-lg btn-primary" type="submit">
                        {{ "login.button.login"|trans({}, "forms") }}
                    </button>
                </form>
            {% endif %}
        </article>
    </div>

{% endblock %}

J'ai modifié mon contrôlleur (https://symfony.com/doc/4.4/security/form_login_setup.html#generating-the-login-form) :

public function login(Request $request, LDAPAuthentication $LDAP, AuthenticationUtils $authenticationUtils): Response {

        /*$form = $this->createForm(LoginType::class, null);
        $form->handleRequest($request);
        $error = '';
        if ($form->isSubmitted() && $form->isValid()) {
            $user = $form->getdata()['Login'];
            $password = $form->getdata()['Password'];
            if ($LDAP->authenticate($user, $password)) {
                return $this->redirectToRoute('app_item_list');
            } else {
                $error = 'Connexion échouée: Mauvais Login ou mot de passe.';
            }
        }

        return $this->render('Security/login.html.twig', ['form' => $form->createView(), 'error' => $error]);*/
        if ($this->getUser()) {
             return $this->redirectToRoute('app_item_list');
        }

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('Security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
    }

    /**
     * @Route("/Deconnexion",
     * name="app_security_logout",
     * methods={"GET"})
     */
    public function logout(LDAPAuthentication $LDAP) {
        // $LDAP->logout();
        return $this->redirectToRoute('app_security_login');
    }

J'ai modifié mon fichier security.yaml (https://symfony.com/doc/4.4/security/ldap.html#fetching-users-using-the-ldap-user-provider)

providers:
        my_ldap:
            ldap:
                service: Symfony\Component\Ldap\Ldap
                base_dn: DC=acc,DC=accronyme,DC=net
                search_dn: "OU=Users,OU=a,OU=FR,OU=AGC,DC=acc,DC=accronyme,DC=net"
                search_password: password
                default_roles: ROLE_USER
                uid_key: sAMAccountName
                extra_fields: ['email']
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true
            lazy: true
            # provider: users_in_memory
            provider: my_ldap
            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true
            form_login_ldap:
                service: Symfony\Component\Ldap\Ldap
                dn_string: 'sAMAccountName={username},DC=acc,DC=accronyme,DC=net'
            logout:
                path: app_security_logout


Je pense avoir tout fait correctement pourtant quand je saisi mes identifiants, cela ne fonctionne pas, il me recharge la page de login sans message d'erreur ni warning... et surtout au niveau utilisateurs dans symfony, il affiche "Logged in as anon., Authenticated Yes" est-ce normal que je ne sois pas redirigée ?

Si quelqu'un peut m'aider à comprendre svp...

-
Edité par lindadu01 7 mai 2021 à 10:59:07

  • Partager sur Facebook
  • Partager sur Twitter

Parfois, arrêter 5 minutes son développement permet de mieux repartir face à un problème ;)