0

私は symfony 3.3 と fos_userbundle および fr3d_ldapbundle を使用して、LDAP を介してユーザーを認証しています。生成された標準のログイン フォームを使用しようとすると、ログインは正しく機能します。しかし、私がする必要があるのは、手動(プログラムによる)ログインです。fr3d_ldapbundle でそれを行う最良の方法は何ですか?

申し訳ありませんが、詳細を教えてください:

このガイドに従おうとしました: https://ourcodeworld.com/articles/read/459/how-to-authenticate-login-manually-an-user-in-a-controller-with-or-without-fosuserbundle-on -symfony-3

fos_user.user_managerを使用しようとすると、ログインは正しく機能しますが、fr3d_ldap.ldap_manager を使用する機能しません。( isPasswordValid関数は「ユーザー名またはパスワードが無効です」を返します)

ユーザーは LDAP サーバーから正しく取得されますが、 $userオブジェクトを出力すると「パスワード」フィールドが空になります。標準のログイン フォームを使用すると、認証は正しく機能し、ユーザー名は fos ユーザー バンドル テーブルに保存され、パスワード フィールドは空になります。これは私の問題でしょうか?

$saltも空です。

これはLoginActionの私のコードです:

public function loginAction(Request $request)
{
    // This data is most likely to be retrieven from the Request object (from Form)
    // But to make it easy to understand ...
    $_username = "user";
    $_password = "password";

    // Retrieve the security encoder of symfony
    $factory = $this->get('security.encoder_factory');

    /// Start retrieve user
    // Let's retrieve the user by its username:
    /*
    // If you are using FOSUserBundle:
    $user_manager = $this->get('fos_user.user_manager');
    $user = $user_manager->findUserByUsername($_username);
    //Or by yourself
    $user = $this->getDoctrine()->getManager()->getRepository("ApiBundle:User")
            ->findOneBy(array('username' => $_username));
    */
    //Using fr3d/ldap-bundle 
    $user_manager = $this->get('fr3d_ldap.ldap_manager');
    $user = $user_manager->findUserByUsername($_username);
    //print_r($user);die();
    /// End Retrieve user

    // Check if the user exists !
    if(!$user){
        return new Response(
            'Username doesnt exists',
            Response::HTTP_UNAUTHORIZED,
            array('Content-type' => 'application/json')
        );
    }

    /// Start verification
    $encoder = $factory->getEncoder($user);
    $salt = $user->getSalt();

    if(!$encoder->isPasswordValid($user->getPassword(), $_password, $salt)) {
        return new Response(
            'Username or Password not valid.',
            Response::HTTP_UNAUTHORIZED,
            array('Content-type' => 'application/json')
        );
    } 
    /// End Verification

    // The password matches ! then proceed to set the user in session

    //Handle getting or creating the user entity likely with a posted form
    // The third parameter "main" can change according to the name of your firewall in security.yml
    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
    $this->get('security.token_storage')->setToken($token);

    // If the firewall name is not main, then the set value would be instead:
    // $this->get('session')->set('_security_XXXFIREWALLNAMEXXX', serialize($token));
    $this->get('session')->set('_security_main', serialize($token));

    // Fire the login event manually
    $event = new InteractiveLoginEvent($request, $token);
    $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

    /*
     * Now the user is authenticated !!!! 
     * Do what you need to do now, like render a view, redirect to route etc.
     */
    return new Response(
        'Welcome '. $user->getUsername(),
        Response::HTTP_OK,
        array('Content-type' => 'application/json')
    );
}

誰かが私を助けることができますか?ありがとうございました。

4

1 に答える 1