首页 文章

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

提问于
浏览
0

我正在实施自己的 Profiles 设置页面 . 这将允许用户更改他们的姓名,电子邮件,密码等 .

我了解FOSUserBundle有预建/配置文件/编辑和/配置文件/更改密码 . 我想在这里使用相同的功能,除了我自己的twig捆绑 . 我没有使用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 .

1 回答

  • 0

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

    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 '
            );
        }
    

相关问题