找到你要的答案

Q:Case-insensitive user-names in Symfony2

Q:不区分大小写的用户名Symfony2

By default, Symfony2 matches usernames case-sensitively. I want users to be able to enter johnsmith, JohnSmith, johnSMITH, or any variant of that, and have them all register as johnsmith. How do I do this?

I though the easiest way would be to always convert each username to lower-case before comparing them. This is easy to do on the one side (just throw a lower() into the SQL statement), but how do I do that for what the user types in in the login form? Since Symfony automatically takes care of the login_check route handling, I can't figure it out.

Better yet, is there some option I can set to enable case-insensitivity instead?

默认情况下,用户案例敏感Symfony2的比赛。我希望用户能够进入JohnSmith,约翰史密斯,约翰史密斯,或任何变异,而且他们全部登记为约翰史密斯。我该怎么做?

我虽然最简单的方法是总是转换每个用户名到较低的情况下,然后比较它们。这是很容易做到的一面(把一lower()到SQL语句),但我怎么做,什么类型的用户在登录表单?因为symfony会自动处理的login_check路由处理,我弄不明白。

更好的是,有一些选项,我可以设置,使案件不敏感,而不是?

answer1: 回答1:

Did you try to convert the input into lowercase using CSS ? There are actually ways to control data input before it is handed to the login_check controller, but if you want a quick fix :

p {
    text-transform: lowercase;
}

你试着将输入转换为小写使用CSS?其实之前就交给login_check控制器控制数据输入的方式,但如果你想要快速的解决方法:

p {
    text-transform: lowercase;
}
answer2: 回答2:

In your setUsername you could just have the text changed to lowercase like..

public function setUsername($username)
{
    $this->username = mb_strtolower($username);

    return $this;
}

For reference, FOSUserBundle handles this by "canonicalizing" the username (to usernameCanonical) and email address (to canonicalEmail) in the updateUser call (see Doctrine\UserManager that calls the Canonicalizer) which it then uses for searches.

在你的setusername你可能只是有文本转换为小写的样子。

public function setUsername($username)
{
    $this->username = mb_strtolower($username);

    return $this;
}

作为参考,FOSUserBundle处理这一“canonicalizing“用户名(以usernamecanonical)和电子邮件地址(以canonicalemail)在updateuser调用(参见原则\ UserManager调用canonicalizer)然后用它来搜索。

answer3: 回答3:

I feel like an idiot. All I had to do was add another lower() around the where clause in my SQL statement. Like this:

select lower(USERNAME) as USERNAME, PASSWORD, ROLES, ALL_CUSTOMER_ACCESS
from COMPANYNAME_USERS
where lower(USERNAME) = lower(:username)

我觉得自己像个白痴。我所要做的就是添加在WHERE子句的SQL语句在我的另一个lower()。这样地:

select lower(USERNAME) as USERNAME, PASSWORD, ROLES, ALL_CUSTOMER_ACCESS
from COMPANYNAME_USERS
where lower(USERNAME) = lower(:username)
answer4: 回答4:

You already fixed it, but I will explain another solution for users with similar problems:

  1. You have to implement your own Provider this way: http://symfony.com/doc/current/cookbook/security/entity_provider.html#authenticating-someone-with-a-custom-entity-provider
  2. Use the following query instead in method loadUserByUsername:

    $user = $this->findOneBy(array('username' => strtolower($username)));

This worked for me. (Also in Doctrine Mongo ODM)

您已经修正了,但我会为类似问题的用户解释另一个解决方案:

  1. You have to implement your own Provider this way: http://symfony.com/doc/current/cookbook/security/entity_provider.html#authenticating-someone-with-a-custom-entity-provider
  2. 使用下面的查询方法loaduserbyusername代替:

    用户名=美元- >;findoneby(阵列('username ' = & gt;strtolower($用户名)));

这对我有用。(也在学说蒙戈ODM)

answer5: 回答5:

Just write correct loadUserByUsername function in UserRepository. It must not be case sensitive:

public function loadUserByUsername($username)
{
    $user = $this->createQueryBuilder('u')
        ->where('LOWER(u.email) = :username')
        ->setParameter('username', strtolower($username))
        ->getQuery()
        ->getOneOrNullResult();

    if ($user){
       return $user;
    }

    throw new UsernameNotFoundException('Unable to find user "' . $username . '"');
}

Just write correct loadUserByUsername function in UserRepository. It must not be case sensitive:

public function loadUserByUsername($username)
{
    $user = $this->createQueryBuilder('u')
        ->where('LOWER(u.email) = :username')
        ->setParameter('username', strtolower($username))
        ->getQuery()
        ->getOneOrNullResult();

    if ($user){
       return $user;
    }

    throw new UsernameNotFoundException('Unable to find user "' . $username . '"');
}
symfony2  username  case-insensitive