Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonataAdminBundle can not update the user password

I can not update the user password via sonataadmin dashboard.

I use symfony2 FosUserBundle2.0 SonataAdminBundle(but not use SonataUserBundle) and follow the Document to do.

MyUserBundle\Admin\UserAdmin.php class UserAdmin 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,
            )
        )
        ..
}

there is no problem when i use sonataadminbundle dashboard to create a new user, but when i use dashboard to update the password,the password in DB doesn't change.

the others can update but the password, i don't know why. there is no any errror message.

I am new in symfony2, some one help me?

thanks to kwozny ,now i fix it~ i don't change the function configureFormFields code, just follow kwozny's advise , add the following code.i don't know why ,but i works! i can update the password and when i update the others(password field is empty) the password will not change.

public function prePersist($object)
    {
        parent::prePersist($object);

    }
    public function preUpdate($object)
    {
        parent::preUpdate($object);

    }
like image 648
nistlrooy Avatar asked Feb 09 '23 17:02

nistlrooy


1 Answers

that's because you have to catch the object User (preUpdate() and prePersist()) and set password using $user->setPlainPassword. Thats how my code looks:

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);
}
like image 90
kamwoz Avatar answered Feb 12 '23 06:02

kamwoz