Symfony SonataAdminBundle无法更新用户密码
我无法通过sonataadmin仪表板更新用户密码 我使用symfony2 FosUserBundle2.0 SonataAdminBundle(但不使用SonataUserBundle),并按照文档进行操作Symfony SonataAdminBundle无法更新用户密码,symfony,fosuserbundle,sonata-admin,change-password,Symfony,Fosuserbundle,Sonata Admin,Change Password,我无法通过sonataadmin仪表板更新用户密码 我使用symfony2 FosUserBundle2.0 SonataAdminBundle(但不使用SonataUserBundle),并按照文档进行操作 MyUserBundle\Admin\UserAdmin.phpclass UserAdmin像这样扩展Admin protected function configureFormFields(FormMapper $formMapper) { $formMapper
MyUserBundle\Admin\UserAdmin.php
class UserAdmin像这样扩展Admin
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
dashboard创建新用户时没有问题,但使用dashboard更新密码时,DB中的密码不会更改
其他人可以更新密码,但我不知道为什么。没有任何错误消息
我是新来的symfony2,有人帮我吗
多亏了kwozny,现在我修复了它~
我没有更改函数configureFormFields代码,只是按照kwozny的建议,添加以下代码。我不知道为什么,但我可以工作!
我可以更新密码,当我更新其他密码时(密码字段为空),密码不会更改
public function prePersist($object)
{
parent::prePersist($object);
}
public function preUpdate($object)
{
parent::preUpdate($object);
}
这是因为您必须捕获对象用户(preUpdate()和prePersist())并使用$User->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);
}
我根据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)
注意:如果用户存在,我添加了条件:不需要密码,新用户:需要通过
现在只需在管理中添加如下代码:
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);
}
无需更改用户实体,只需像往常一样使用普通密码。在我的例子中添加
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);
}
}
就我而言,我就是这样解决的。我的答案如下。我使用了上述答案的一些代码,并重复了sonata类型的代码,因此验证仍然适用于相同的密码验证
public function prePersist($object)
{
parent::prePersist($object);
}
public function preUpdate($object)
{
parent::preUpdate($object);
$this->updateUser($object);
}
//update password
public function updateUser(\AppBundle\Entity\User $u) {
if ($u->getPlainPassword()) {
$u->setPlainPassword($u->getPlainPassword());
}
$um = $this->getConfigurationPool()->getContainer()->get('fos_user.user_manager');
$um->updateUser($u, false);
}
protected function configureFormFields(FormMapper $formMapper)
{
$pass_required = false;
//if form is create then add required validation
if (!$this->subject->getId())
$pass_required = true;
$formMapper->add('name', 'text')
->add('email', 'email', array('label' => "Email") )
->add('username', 'text', array('label' => "Username") )
->add('groups', 'entity', array(
'class' => 'AppBundle\Entity\Group',
'required' => false,
'multiple' =>true
))
->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' => $pass_required,
))
->end()
;
}
非常感谢,但我不知道如何修复它,你能给我看一下详细的代码吗?谢谢,它可以工作!!!密码现在可以更新。但是有一个错误:当我只想更新其他值(如firstname)时,我必须输入密码,否则密码将更改…尝试将此添加到您的用户实体:
private$newPass;公共函数getNewPass(){return$this->newPass;}公共函数setNewPass($newPass){$this->newPass=$newPass;return$this;}
谢谢Kamwoz。我使用了部分代码来编写代码,经过两个小时的工作,我的问题得到了解决。