FOSUserBundle使用电子邮件登录(Symfony2) [英] FOSUserBundle login with email (Symfony2)

查看:68
本文介绍了FOSUserBundle使用电子邮件登录(Symfony2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Symfony 2.0.x的FOSUserBundle有疑问. 在该文档中,您可以找到一种更改登录名以使用用户名和电子邮件的方法.太棒了!但是我想登录以仅使用电子邮件.因此,我在CustomUserManager中添加了一个功能(对原始功能进行了扩展),以确保您可以使用电子邮件登录.

I have a question about the FOSUserBundle for Symfony 2.0.x. In there documentation you can find a way to change the login to work with either username and email. That works great! But I want to login to work with just the email. So I added a function in my CustomUserManager (extends from the original) that makes sure you just can logon with your email.

namespace Frontend\UserBundle\Model;

use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class CustomUserManager extends UserManager
{
    public function loadUserByUsername($email)
    {
        /*$user = $this->findUserByUsernameOrEmail($username);

        if (!$user) {
            throw new UsernameNotFoundException(sprintf('No user with name "%s" was found.', $username));
        }

        return $user;*/

        //Change it to only email (Default calls loadUserByUsername -> we send it to our own loadUserByEmail)
        return $this->loadUserByEmail($email);
    }

    public function loadUserByEmail($email)
    {
        $user = $this->findUserByEmail($email);

        if (!$user) {
            throw new UsernameNotFoundException(sprintf('No user with email "%s" was found.', $email));
        }

        return $user;

    }
}

但是现在我有一个问题,我需要控制保存在会话中的值.他将我的用户名保存在会话中,并且当系统检查该用户名时,将没有可用的电子邮件(因为他仅检查电子邮件).

But now I have a problem that I need to control the values that are saved in the session. He saves my username in the session and when the system checks this there will be no email (Because he only checks on email) available.

所以我的问题是,如何/在何处更改存储在username变量中的值.

So my question is how/where can you change the value that is stored inside the username variable.

谢谢!

推荐答案

这可以通过覆盖User实体中的设置器来完成:

This can be done by overriding setters in the User entity:

<?php

namespace Acme\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\UserRepository")
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{

    // ...

    // override methods for username and tie them with email field

    /**
     * Sets the email.
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->setUsername($email);

        return parent::setEmail($email);
    }

    /**
     * Set the canonical email.
     *
     * @param string $emailCanonical
     * @return User
     */
    public function setEmailCanonical($emailCanonical)
    {
        $this->setUsernameCanonical($emailCanonical);

        return parent::setEmailCanonical($emailCanonical);
    }

    // ...

}

目前还不清楚,但是从您的命名空间来看,我假设您拥有自己的子捆绑包,该子捆绑包使用FOSUserBundle作为其父捆绑包(根据

It's not clear from your question, but from your namespaces I assume that you have your own child bundle that uses FOSUserBundle as its parent bundle (as per FOSUserBundle documentation, example "b").

如果这样做,则可以轻松地覆盖表单和模板,以便从UI中删除任何username字段并将其替换为email字段.由于您已按上述方法覆盖了电子邮件设置程序,因此username属性将被正确填充.

If you do, you can then easily override Forms and templates in order to remove any username fields from the UI and replace them with email fields. Since you have overridden the email setters as per above, username property will be populated properly.

尽管可以,但这不是一个很好的解决方案.不利之处在于,数据库中仍然会有usernameusernameCanonical列.我们原本想一并删除这些,但是这将需要对FOSUserBundle进行太多更改(\FOS\UserBundle\Model\UserInterface要求使用username属性,而bundle代码取决于该属性).

Although this works, it's not a very nice solution. The downside is that you will still have username and usernameCanonical columns in the database. We originally wanted to remove these altogether, but this would require too many changes in FOSUserBundle (the username attribute is required by \FOS\UserBundle\Model\UserInterface, and bundle code depends on it).

这篇关于FOSUserBundle使用电子邮件登录(Symfony2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆