如何编码密码?

时间:2014-03-26 21:51:29

标签: php symfony

我对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 个答案:

答案 0 :(得分: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);