【问题标题】:FOS User change password form requires my pass for another accountFOS 用户更改密码表格需要我的另一个帐户的通行证
【发布时间】:2014-03-07 22:17:23
【问题描述】:

我有一个门户网站,公司可以在其中为其员工创建子帐户,并且他们可以更改员工密码。问题是当我尝试更改它时,我必须写入用户的当前密码,但它不接受它的密码,只有我的 - 当前登录的用户,而不是我正在编辑的用户。

我的控制器:

  public function changePasswordAction(Request $request, $company_id, $id)
    {
        $company = $this->getCompany($company_id);

        $subaccount = $this->getDoctrine()
            ->getRepository('MyBundle:User')
            ->find($id);

        if (!$subaccount or !$company->hasUser($subaccount))
        {
            throw new AccessDeniedException();
        }

        $form = $this->createForm('fos_user_change_password', $subaccount);
        $form->add('save', 'submit');
        $form->handleRequest($request);

        if ($form->isValid())
        {
            $userManager = $this->container->get('fos_user.user_manager');
            $userManager->updateUser($subaccount);

            $em = $this->getDoctrine()->getManager();
            $em->flush();

            $this->get('session')->getFlashBag()->add('success', 'subaccount.flash.password_changed');

            return $this->redirect($this->generateUrl('subaccount_list', array('company_id' => $company->getId())));
        }

        return $this->render('MyBundle:Subaccount:edit.html.twig', array(
            'form' => $form->createView(),
            'company' => $company
        ));
    }

是的,子帐户和门户用户是使用一个实体创建的。

【问题讨论】:

    标签: php forms symfony fosuserbundle


    【解决方案1】:

    fos_user_change_password 表单仅适用于当前用户。 在这种情况下,您必须创建一个新表单,编辑用户并使用用户管理器保存它。

    一个例子(未测试):

    公共函数changePasswordAction(请求$request, $company_id, $id) { $company = $this->getCompany($company_id);

        $subaccount = $this->getDoctrine()
            ->getRepository('MyBundle:User')
            ->find($id);
    
        if (!$subaccount or !$company->hasUser($subaccount))
        {
            throw new AccessDeniedException(); //You should send HTTP not found
        }
    
        $form = $this->createFormBuilder() // You should create a new form type here
            ->add('password', 'repeated', array(
                'type' => 'password',
                'invalid_message' => 'New password has not been repeated',
                'options' => array('required' => true),
                'first_options'  => array('label' => 'New password'),
                'second_options' => array('label' => 'Repeat new password'),
            )
            ->add('save', 'submit')
            ->getForm();
    
        if('POST' === $request->getMethod())
        {
            $form->handleRequest($request);
            if ($form->isValid())
            {
                $data = $form->getData();
                $subaccount->setPlainPassword($data['password']);
    
                $userManager = $this->container->get('fos_user.user_manager');
                $userManager->updatePassword($subaccount);
    
                $em = $this->getDoctrine()->getManager()->flush();
    
                $this->get('session')->getFlashBag()->add('success', 'subaccount.flash.password_changed');
    
                return $this->redirect($this->generateUrl('subaccount_list', array('company_id' => $company->getId())));
            }
        }
    
        return $this->render('MyBundle:Subaccount:edit.html.twig', array(
            'form' => $form->createView(),
            'company' => $company
        ));
    }
    

    【讨论】:

    • 是的,但是使用 FOS 捆绑包我必须重复密码,而且我必须提供旧密码 - 比只提供新密码更安全。而且我真的不知道如何为旧密码和重复新密码添加字段...
    • 我已经修改了我的答案。现在重复密码。我不明白你为什么需要旧密码。管理员/用户管理员不应该知道用户的密码。为了更加安全,您可以添加一个新的密码字段来表示当前用户密码(管理员或用户管理员)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多