使用自定义控制器更改密码 - FOSUserBundle(无 FormBuilder)

Change password using custom controller - FOSUserBundle (without FormBuilder)

我正在实现自己的配置文件设置页面。这将允许用户更改他们的姓名、电子邮件、密码等。

我了解 FOSUserBundle 已预构建 /profile/edit 和 /profile/change-password。我想在这里使用相同的功能,除了在我自己的树枝包中。我没有使用 FormBuilder。

这是我用于更改用户设置的控制器。

public function applySettings(Request $request)
{ 
    $user = $this->getUser();

    $dm = $this->get('doctrine_mongodb')->getManager();
    $repository = $dm->getRepository('AppBundle:User');

    $name = $request->get('_username');
    $email = $request->get('_email');
    $password = $request->get('_password');
    $confirm = $request->get('_confirm');

    if ($name != null) {
        if ($name == ($repository->findOneBy(array('username' => $name )))) { 
            throw $this->createNotFoundException('Username already in use');
        } else { 
            // username changed
            $user->setUsername($name); 
        }         
    }

    if ($email != null) {
        if ($email == ($repository->findOneBy(array('email' => $email )))) { 
            throw $this->createNotFoundException('Email already in use');
        } else { 
            // email changed
            $user->setEmail($email);    
        }      
    }

    if (strcmp($password, $confirm) == 0) {
        // password = confirm here
        // change password functionality done here
    } else {
        throw $this->createNotFoundException(
            'Passwords do not match '
        );
    }

    $dm->flush();
    return $this->redirectToRoute('settings');
}

有没有一种方法可以在我自己的控制器中进行密码验证,如果有效则加盐并存储密码?

还有没有办法应用 /register/ 和 /profile/change-password 中存在的相同验证方法,如果密码在提交前不匹配,则会出现警报?

如有任何帮助,我们将不胜感激。

我正在使用 Symfony 2.6.1 和最新版本的 FOSUserBundle。

这是我使用的功能。有效但不进行实时验证检查。

    if (strcmp($password, $confirm) == 0) {
        $encoder_service = $this->get('security.encoder_factory');
        $encoder = $encoder_service->getEncoder($user);

        $newpass = $encoder->encodePassword($password, $user->getSalt());
        $user->setPassword($newpass);
    } else {
        throw $this->createNotFoundException(
            'Passwords do not match '
        );
    }