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

标签 symfony backend sonata-admin

当我想从后端编辑现有用户时遇到问题(使用 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/

相关文章:

angular - 如何通过API将从Angular 5表单发送到Symfony的数据插入到mysql中?

表的 Doctrine 计数记录

java - 应用程序引擎动态后端在开发模式下无法按预期运行

symfony - SonataAdminBundle 中的数组类型

symfony - 更改 Sonata Admin 上的左侧菜单标签

php - 使用 Native SQL 的 Doctrine 查询总是返回空数组

php - 如何使用 Symfony2 配置代码接收功能测试?

odata - SAPUI5 OData 模型中的应用程序数据如何写回后端系统

python - 来自flask的响应在React前端收到的Json上有额外的键

symfony - SonataAdminBundle 自定义呈现列表中的文本字段