dtest84004 2014-06-13 11:24
浏览 7

如何在登录Symfony2 FOSUserBundle之前操作用户名?

I have symfony2 web app (with FOSUserBundle) I want to add a string @something before authentication. For example user want to login by his username myusername but the real username in database is myusername@something, so I have to add the @something before authentication process. I checked checkAction (I thought I could manipulate posted username there) but this method throws an execption:

throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');

Where can I change the username after form post?

  • 写回答

1条回答 默认 最新

  • dpd3982 2014-06-14 21:37
    关注

    You can override the ProfileFormType to add an event listener to the form. Before the submission occurs this will modify the data and append the @something suffix if it hasn't been provided already by the user.

    <?php
    
    namespace Acme\UserBundle\Form\Type;
    
    use Symfony\Component\Form\FormBuilderInterface;
    use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
    use Symfony\Component\Form\FormEvent;
    use Symfony\Component\Form\FormEvents;
    
    class ProfileFormType extends BaseType
    {
        const USERNAME_SUFFIX = '@something';
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            parent::buildForm($builder, $options);
    
            $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
                $data = $event->getData();
    
                if (isset($data['username']) && $this->usernameHasSuffix($data['username']) {
                    $data['username'] = $data['username'].self::USERNAME_SUFFIX;
                }
    
                $event->setData($data);
            });
        }
    
        public function getName()
        {
            return 'acme_user_profile';
        }
    
        private function usernameHasSuffix($username)
        {
            return substr($username, -strlen(self::USERNAME_SUFFIX)) === self::USERNAME_SUFFIX;
        }
    }
    

    Once created, just register it and tag it in the service container

    # src/Acme/UserBundle/Resources/config/services.yml
    services:
        acme_user.profile.form.type:
            class: Acme\UserBundle\Form\Type\ProfileFormType
            arguments: [%fos_user.model.user.class%]
            tags:
                - { name: form.type, alias: acme_user_profile }
    

    Then reconfigure the FOS UserBundle to use this form.

    # app/config/config.yml
    fos_user:
        # ...
        profile:
            form:
                type: acme_user_profile
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制