我有一个门户网站,公司可以为员工提供子帐户,他们可以更改员工密码。问题是,当我试图更改它时,我必须写用户当前的密码,但它不接受它的密码,只有我的 - 当前登录的用户,而不是我正在编辑的用户。
我的控制器:
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
));
}
是的,子帐户和门户网站用户是使用一个实体创建的。
答案 0 :(得分:0)
表单fos_user_change_password
仅适用于当前用户。
在这种情况下,您必须创建一个新表单,编辑用户并使用用户管理器保存它。
一个例子(未经测试):
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(); //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
));
}