2015-06-30 66 views
3

我無法通過sonataadmin儀表板更新用戶密碼。SonataAdminBundle無法更新用戶密碼

我使用symfony2 FosUserBundle2.0 SonataAdminBundle(但是不是使用SonataUserBundle)並按照文檔來做。

MyUserBundle\Admin\UserAdmin.php類UserAdmin擴展管理員這樣

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, 
      ) 
     ) 
     .. 
} 

沒有問題,當我使用sonataadminbundle儀表盤創建一個新用戶,但是當我使用的儀表板更新密碼,在DB的密碼不對更改。

其他人可以更新但密碼,我不知道爲什麼。沒有任何錯誤信息。

我是symfony2中的新人,有人幫助我嗎?

感謝kwozny,現在我修正了吧〜 我不改變功能configureFormFields代碼,只要按照kwozny的提醒,添加以下code.i不知道爲什麼,但我的作品! 我可以更新密碼,當我更新其他人(密碼字段爲空)時,密碼不會更改。

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

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

    } 

回答

5

那是因爲你必須捕捉對象用戶(更新前的()和prePersist()),並使用$用戶> setPlainPassword設置密碼。這就是我的代碼的外觀:

相反:

->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, 
     ) 
    ) 

我:

  ->add('newPass', 'text', array(
       'label' => 'New password (empty filed means no changes)', 
       'required' => FALSE 
      )) 

,然後在同一個管理文件:

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); 
} 
+0

非常感謝你,但我不知道如何解決它,你能告訴我詳細的代碼? – nistlrooy

+0

我編輯我的帖子 – kamwoz

+0

謝謝,它的作品!密碼可以立即更新。但有一個錯誤:當我只想更新其他值(如firstname)時,我必須輸入密碼,否則密碼將會更改... – nistlrooy

0

我發現基於一個更好的辦法在kamwoz答案,它爲我工作。

在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) 

注:我加了條件,如果用戶存在:不需要密碼,新用戶:通過必需的。

現在只需添加這樣的代碼在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); 
} 

無需改變用戶實體,只是用plainPassword如常。

0

在我的情況下加

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); 
    } 
}