我沒有使用FOSUserBundle,我自己創建了實體用戶,角色等。配置去如下:編輯用戶時出現問題,如何存儲正確編碼的密碼?
security:
encoders:
Bundle\UserBundle\Entity\User:
algorithm: sha512
encode-as-base64: true
iterations: 10
和創建時,我這樣做:
if ($form->isValid()) {
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($entity);
$entity->setSalt(md5(time()));
$entity->setCompany($company);
$encoded_password = $encoder->encodePassword($entity->getPassword(), $entity->getSalt());
$entity->setPassword($encoded_password);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('user'));
}
然而,當編輯任何用戶的密碼顯示爲:
2cp/D6OTBEx4m2Y9xM/ku6rOtgODhrXK7//3Pd8D0jnZkOuusPB120Jwb6x+kan/52qSQuQxcUYDP1T6q6zqbw==
但如果我保存任何其他更改密碼被覆蓋,我將不再能訪問,同樣,如果我輸入新的密碼,它不會編碼(顯然是因爲我沒有做的事情updateAction),但我想知道是什麼爲了實現這一目標是否正確?
使用preUpdate?分隔「編輯」中的密碼字段,以便字段不會被覆蓋?
我會用密碼另一個屬性(FOSUserBundle使用'plainPassword'),然後,在prePersist /更新前的聽衆,如果plainPassword不爲空做的編碼和更新的實際密碼字段。 – qooplmao