Symfony2 SonataAdminBundle 密码字段加密[英] Symfony2 SonataAdminBundle Password field encryption

本文是小编为大家收集整理的关于Symfony2 SonataAdminBundle 密码字段加密的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有fosuserbundle来管理我的用户,而sonataadminbundle来管理我的网站...我有问题,每当我尝试更改/添加密码时,密码都不会编码sha512,但是当用户在fosuserbundle注册页面中注册时,它确实会这样...

因此,symfony2配置没有任何问题,既不是fosuserbundle config,它可能在sonataadminbundle内部,或者可能在我的admin类中...

<?php
// src/Acme/DemoBundle/Admin/PostAdmin.php

namespace Web\DificilBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Web;

class UserAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('firstname')
            ->add('lastname')
            ->add('username')
            ->add('email')
            ->add('password', 'password') // -> I WANT THIS TO BE ENCODED INTO SHA512!
             ->add('roles','choice',array('choices'=>$this->getConfigurationPool()->getContainer()->getParameter('security.role_hierarchy.roles'),'multiple'=>true ));

            //->add('body')
        ;
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('firstname')
            ->add('lastname')
            ->add('username')
            ->add('email')
            ->add('password')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->add('firstname')
            ->add('lastname')
            ->add('username')
            ->add('password')
            ->add('email')
            ->add('facebookid')
            ->add('roles');

            //->add('password', 'password')
        ;
    }
}

推荐答案

找到了一个与我有相同问题的人,在您的管理类中,您定义创建/更新表格,使用此解决方案,并且您的密码将被完美地加密并准备登录您的新内容用户;)

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
        ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
        ->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',
        ))
    ;
}

本文地址:https://www.itbaoku.cn/post/2090811.html

问题描述

I have FOSUserBundle to manage my users, and SonataAdminBundle to manage my website... I have a problem, whenever I try to change/add a password to any user, the password isn't encoded into sha512, but it does when the user register itself inside fosuserbundle registration page...

So there isn't any problem with Symfony2 configuration neither fosuserbundle config, it may be inside SonataAdminBundle somewhere, or maybe into my admin class...

<?php
// src/Acme/DemoBundle/Admin/PostAdmin.php

namespace Web\DificilBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Web;

class UserAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('firstname')
            ->add('lastname')
            ->add('username')
            ->add('email')
            ->add('password', 'password') // -> I WANT THIS TO BE ENCODED INTO SHA512!
             ->add('roles','choice',array('choices'=>$this->getConfigurationPool()->getContainer()->getParameter('security.role_hierarchy.roles'),'multiple'=>true ));

            //->add('body')
        ;
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('firstname')
            ->add('lastname')
            ->add('username')
            ->add('email')
            ->add('password')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->add('firstname')
            ->add('lastname')
            ->add('username')
            ->add('password')
            ->add('email')
            ->add('facebookid')
            ->add('roles');

            //->add('password', 'password')
        ;
    }
}

推荐答案

Found a solution for everyone who has the same problem as me, just on your admin class, where you define your Create/Update form, use this and your password will be perfectly encrypted and ready to log into your new user ;)

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
        ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
        ->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',
        ))
    ;
}