SonataAdminBundle cannot update user password
I am unable to update the user's password using the sonataadmin toolbar.
I am using symfony2 FosUserBundle2.0 SonataAdminBundle (but not using SonataUserBundle) and follow the doc.
MyUserBundle\Admin\UserAdmin.php
the UserAdmin class extends admin like this
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
......
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
'required' => false,
)
)
..
}
no problem when i use the toolbar sonataadminbundle
to create a new user, but when i use the toolbar to update the password, the password in the db doesn't change.
others may update, but the password, I don't know why. no error message.
I'm new to symfony2, can anyone help me?
thanks to kwozny, now i will fix it ~ i am not changing the configureFormFields function code, just follow kwozny's advice, add the following code. I don't know why, but I'm working! I can update the password and when I update the rest (the password field is empty) the password will not change.
public function prePersist($object)
{
parent::prePersist($object);
}
public function preUpdate($object)
{
parent::preUpdate($object);
}
source to share
because you need to catch the User object (preUpdate () and prePersist ()) and set the password using $ user-> setPlainPassword. This is what my code looks like:
Instead
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
'required' => false,
)
)
I have:
->add('newPass', 'text', array(
'label' => 'New password (empty filed means no changes)',
'required' => FALSE
))
and then in the same admin file:
public function prePersist($object) {
parent::prePersist($object);
$this->updateUser($object);
}
public function preUpdate($object) {
parent::preUpdate($object);
$this->updateUser($object);
}
public function updateUser(\AppBundle\Entity\User $u) {
if ($u->getNewPass()) {
$u->setPlainPassword($u->getNewPass());
}
$um = $this->getConfigurationPool()->getContainer()->get('fos_user.user_manager');
$um->updateUser($u, false);
}
source to share
I found a better way based on kamwoz's answer and it worked for me.
In FormMapper:
$passwordoptions=array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'translation_domain' => 'FOSUserBundle',
'invalid_message' => 'fos_user.password.mismatch',
);
$this->record_id = $this->request->get($this->getIdParameter());
if (!empty($this->record_id)) {
$passwordoptions['required'] = false;
} else {
$passwordoptions['required'] = true;
}
$formMapper
// ...
->add('plainPassword', 'repeated', $passwordoptions)
Note. I added a condition if user exists: no password required, new user: password required.
Now, just add code like this to admin:
public function prePersist($object) {
parent::prePersist($object);
$this->updateUser($object);
}
public function preUpdate($object) {
parent::preUpdate($object);
$this->updateUser($object);
}
public function updateUser(\AppBundle\Entity\User $u) {
$um = $this->getConfigurationPool()->getContainer()->get('fos_user.user_manager');
$um->updateUser($u, false);
}
No need to change User Entity, just use plainPassword as usual.
source to share
In my case add
public function preUpdate($object) {
parent::preUpdate($object);
//$this->updateUser($object);
if($object->getPlainPassword())
{
$um = $this->getConfigurationPool()->getContainer()->get('fos_user.user_manager');
$um->updateCanonicalFields($object);
$um->updatePassword($object);
}
}
source to share