简体   繁体   English

使用REST API密码更改的FosUserBundle-Symfony 4

[英]FosUserBundle with REST API password change - Symfony 4

Posted this on github issues but actually it probably belongs here. 将此问题发布在github问题上,但实际上它可能属于这里。

I am steadily upgrading a Symfony app to S4, and have hit a bit of a roadblock with regards to changing passwords via a REST API. 我正在稳步将Symfony应用程序升级到S4,并且在通过REST API更改密码方面遇到了一些障碍。

The current controller I have essentially steals from the FosUserBundle one ( ChangePasswordController.php ) with a couple of changes, notably disabling csrf and of course returning a json response rather than rendering a template. 我目前拥有的当前控制器从FosUserBundle一个( ChangePasswordController.php )中进行了一些更改,尤其是禁用了csrf,当然还返回了json响应,而不是渲染了模板。

With the new changes in S4 I get service errors as expected but, unfortunately, these don't seem to be fixable with Dependency Injection. 通过S4的新更改,我得到了预期的服务错误,但是不幸的是,这些错误似乎无法通过依赖注入来修复。 The one that errors out is FOS\\UserBundle\\Form\\Factory\\FactoryInterface - I get 'cannot autowire service'. 出错的是FOS\\UserBundle\\Form\\Factory\\FactoryInterface我得到“无法自动装配服务”。

I understand that this is not going to change, but I also am keen to find a way forward. 我知道这不会改变,但我也渴望找到前进的道路。 What would be the recommended way to update a user password via REST API going forward? 今后通过REST API更新用户密码的推荐方法是什么?

My idea so far is to: 到目前为止,我的想法是:

  1. Take the plain 'current' password, hash it using the same mechanism as the bundle and compare the hashes to make sure the correct password was entered in the first place. 使用普通的“当前”密码,使用与捆绑软件相同的机制对它进行哈希处理,并比较散列值,以确保首先输入了正确的密码。
  2. Compare the two plain 'new' passwords, make sure they match, then use the UserManager to setPlainPassword 比较两个普通的“新”密码,确保它们匹配,然后使用UserManager设置setPlainPassword

Is this a bad way to go? 这是一个不好的方法吗?

Symfony 4 Method Symfony 4方法

UserController.php UserController.php

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Routing\Annotation\Route;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

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

...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM