2014-03-26 39 views
1

我对symfony2非常陌生,所以这可能是一个有点蹩脚的问题;-) 我有一种形式,编辑2 etities - 工人和用户。两者都有关系。用户是用于登录和工人存储有关用户的其他信息(在系统中,我有3种不同类型的用户:工人,客户端和管理员)。下面是生成的updateAction,我想要的是在保存编辑的用户之前对密码进行编码。怎么做?如何编码密码?

public function updateAction(Request $request, $id) 
{ 
    $em = $this->getDoctrine()->getManager(); 
$entity = $em->getRepository('MainBundle:Worker')->find($id); 

if (!$entity) { 
    throw $this->createNotFoundException('Unable to find Worker entity.'); 
} 

$deleteForm = $this->createDeleteForm($id); 
$editForm = $this->createEditForm($entity); 
$editForm->handleRequest($request); 

if ($editForm->isValid()) { 

    $em->flush(); 

    return $this->redirect($this->generateUrl('worker_edit', array('id' => $id))); 
} 

return $this->render('MainBundle:Worker:edit.html.twig', array(
    'entity'  => $entity, 
    'edit_form' => $editForm->createView(), 
    'delete_form' => $deleteForm->createView(), 
)); 

}

回答

1

看一看文件关于编码密码here

基本上你会呼吁安全的工厂让你去,以编码,得到编码器,然后编码用户密码。

$factory = $this->get('security.encoder_factory'); 
$user = //get the user from the worker entity or just use the worker entity 


$encoder = $factory->getEncoder($user); 
$theSuppliedPassword = // however they are supplying the password, likely $entity->getPassword(); 
$password = $encoder->encodePassword($theSuppliedPassword, $user->getSalt()); 
$user->setPassword($password); 
+0

非常感谢! – user1483208