gpt4 book ai didi

symfony - 使用 SonataAdminBundle 编辑特定用户时避免空密码字段

转载 作者:行者123 更新时间:2023-12-04 15:11:35 24 4
gpt4 key购买 nike

当我想从后端编辑现有用户时遇到问题(使用 SonataAdminBundle 和 FOSUserBundle)。在我的 UserAdmin 类的 configureFormFields 方法中,密码字段显示为空,当我需要编辑另一个字段(例如姓氏)保持相同的用户密码时,这是一个问题。此字段(和密码验证字段)必须再次填写! (我不想修改用户密码)

在我的 UserAdmin 类中,我有:

public function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('User Data')
->add('username')

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

->add('firstname')
->add('lastname')
->add('email')
->add('user_roles')
->add('enabled', 'checkbox', array(
'label' => 'Enable Account',
'required' => false,
))
->end()
;
}

我试图在我的 UserAdmin 类中覆盖 prePersist 和 preUpdate 方法,但这些方法不起作用。密码按照 FOS 标准(使用 salt 和 sha512)刻录在数据库中。

有什么解决办法吗?
非常感谢!

最佳答案

您可以覆盖您的 preUpdate在您的管理类(class)中发挥作用,这就是我所做的

public function preUpdate($object)
{
$DM = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
$repository = $DM->getRepository('Namespace\YourBundle\Entity\User')->find($object->getId());

$Password = $object->getPassword();
if (!empty($Password)) {
$salt = md5(time());

$encoderservice = $this->getConfigurationPool()->getContainer()->get('security.encoder_factory');
$encoder = $encoderservice->getEncoder($object);
$encoded_pass = $encoder->encodePassword($object->getPassword(), $salt);
$object->setSalt($salt);
$object->setPassword($encoded_pass);
} else {
$object->setPassword($repository->getPassword());
}
}

还有我的 configureFormFields功能
protected function configureFormFields(FormMapper $formMapper)
{
$passwordoptions=array('type' => 'password','invalid_message' => 'The password fields must match.',
'options' => array('attr' => array('class' => 'password-field')),'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Confirm password'),'translation_domain' => 'FOSUserBundle'
);

$this->record_id = $this->request->get($this->getIdParameter());
if (!empty($this->record_id)) {
$passwordoptions['required'] = false;
$passwordoptions['constraints'] = array(new Assert\Length(array('min' => 10))
,new Assert\Regex(array('pattern' => '/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{10,}$/','match'=>true,'message'=>'Password must contain atleast 1 special character 1 upper case letter 1 number and 1 lower case letter !'))
);
} else {
$passwordoptions['required'] = true;
$passwordoptions['constraints'] = array(new Assert\Length(array('min' => 10))
,new Assert\Regex(array('pattern' => '/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{10,}$/','match'=>true,'message'=>'Password must contain atleast 1 special character 1 upper case letter 1 number and 1 lower case letter !'))
);
}
$formMapper->add('password', 'repeated', $passwordoptions); /*you can add your other fields*/
}

关于symfony - 使用 SonataAdminBundle 编辑特定用户时避免空密码字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21609118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com