Manual authentication check Symfony 2

后端 未结 5 1322
我在风中等你
我在风中等你 2021-02-01 07:53

I\'m working on a Symfony 2 application where the user must select a profile during the login process.

Users may have multiples profiles to work with and they only know

5条回答
  •  余生分开走
    2021-02-01 08:16

    In Symfony 4, the usage of the UserPasswordEncoderInterface is recommended in Controllers. Simply add a UserPasswordEncoderInterface as a parameter to the function in which you want to check the password and then add the code below.

    public function changePasswordAction($old, $new, UserPasswordEncoderInterface $enc) {
       // Fetch logged in user object, can also be done differently.
       $auth_checker = $this->get('security.authorization_checker');
       $token = $this->get('security.token_storage')->getToken();
       $user = $token->getUser();
    
       // Check for valid password
       $valid = $encoder->isPasswordValid($user, $old);
    
       // Do something, e.g. change the Password
       if($valid)
          $user->setPassword($encoder->encodePassword($user, $new));
    }
    

提交回复
热议问题