FosUserBundle with REST API 密码更改 - Symfony 4.



将其发布在github问题上,但实际上它可能属于这里。

我正在稳步将Symfony应用程序升级到S4,并且在通过REST API更改密码方面遇到了一些障碍。

我拥有的当前控制器基本上是从 FosUserBundle 一个 (ChangePasswordController.php) 中窃取的,并进行了一些更改,特别是禁用了 csrf,当然还返回了 json 响应而不是渲染模板。

随着 S4 中的新更改,我按预期收到服务错误,但不幸的是,这些错误似乎无法通过依赖注入修复。出错的是FOSUserBundleFormFactoryFactoryInterface- 我得到"无法自动连线服务"。

我知道这不会改变,但我也渴望找到前进的道路。通过 REST API 更新用户密码的推荐方法是什么?

到目前为止,我的想法是:

  1. 获取普通的"当前"密码,使用与捆绑包相同的机制对其进行哈希处理,并比较哈希值以确保首先输入正确的密码。
  2. 比较两个普通的"新"密码,确保它们匹配,然后使用UserManager进行setPlainPassword

这是一条糟糕的路吗?

Symfony 4 方法

用户控制器.php

<?php
namespace AppController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SensioBundleFrameworkExtraBundleConfigurationIsGranted;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
class UserController extends Controller {
...

public function appUsersPasswordChange(Request $request, UserPasswordEncoderInterface $encoder){
$user = $this->getUser();
$currentPassword = $request->request->get('current_password');
/* Checking current user password with given password param from $reqest */
$passwordValid = $encoder->isPasswordValid($user,$currentPassword );
if($passwordValid){
($request->request->get('new_password')) ? $user->setPassword($encoder->encodePassword($user, $request->request->get('new_password'))) : '';        
$this->em->persist($user);
$this->em->flush();
}else{
return $this->errorResponse('Mismatch current password', $passwordValid );
}
return $this->successResponse('Successfully changed password', $passwordValid );
}
...
}

最新更新