【问题标题】:FosUserBundle with REST API password change - Symfony 4带有 REST API 密码更改的 FosUserBundle - Symfony 4
【发布时间】:2018-03-14 12:31:33
【问题描述】:

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

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

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

随着 S4 中的新变化,我得到了预期的服务错误,但不幸的是,这些似乎无法通过依赖注入修复。出错的是FOS\UserBundle\Form\Factory\FactoryInterface - 我得到“无法自动装配服务”。

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

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

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

这是一个糟糕的方式吗?

【问题讨论】:

    标签: php symfony fosuserbundle


    【解决方案1】:

    Symfony 4 方法

    用户控制器.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 );
        }
    
    ...
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      相关资源
      最近更新 更多