Symfony2 SonataAdminBundle密码字段加密

时间:2014-01-08 17:06:22

标签: security symfony encryption fosuserbundle sonata-admin

我有FOSUserBundle来管理我的用户,而SonataAdminBundle来管理我的网站...我有一个问题,每当我尝试更改/添加密码给任何用户时,密码都没有编码到sha512 ,但是当用户在fosuserbundle注册页面中注册时...

因此Symfony2配置既没有fosuserbundle配置也没有任何问题,它可能在某个地方的SonataAdminBundle内部,或者可能在我的管理类中......

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

1 个答案:

答案 0 :(得分:9)

为每个遇到与我相同问题的人找到解决方案,只需在您的管理类上,您可以在其中定义创建/更新表单,使用此密码,您的密码将完全加密并准备好登录到您的新用户;)

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