1

What i need

  • Username : ABC@gmail.com email address stored in db & input will be lowercase.

  • there is no canonical field which has lower case email address stored.

The solution

     select * from view   where trim(lower(username))=trim(lower(input_username))

I read Url https://symfony.com/doc/current/security/user_provider.html

security.yml

providers:
    entity_provider:
        entity:
            class: App\Entity\User

property: username

I have commented out property as i need to call loadUserByUsername in repository.

Service.yml

app.repository.user_repositry:
    class: Doctrine\ORM\EntityRepository
    autowire: false

    autoconfigure: true
    factory: ["@doctrine.orm.entity_manager", getRepository]
    arguments: []
    tags: ['doctrine.repository_service']

Repository code

            class UserRepository extends EntityRepository implements UserLoaderInterface
            {

                public function loadUserByUsername($username)
                {

                    return $em->createQuery(
                            'SELECT u
                            FROM App\CORE\Entity\User u
                            WHERE LOWER(u.username) = :query'
                        )
                        //->where('LOWER(user.username) = :username')
                        ->setParameter('query', strtolower($username))
                        //->setParameter('query', $usernameOrEmail)
                        ->getQuery()
                        ->getOneOrNullResult();
                }
            }

Problem occurs

  You must either make the "App\Entity\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface" or set the "property" option in the corresponding entity provider configuration."

If i set Entity Abstract class then My app stops working

    namespace App\CORE\Entity;

   use Doctrine\ORM\Mapping as ORM; 
   use Symfony\Component\Security\Core\User\UserInterface;
   use Doctrine\ORM\EntityRepository;
   use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
   use Doctrine\ORM\Mapping\MappedSuperclass;

    /**
    * @ORM\Table(name="users")
   * @ORM\Entity
    */

   class User  implements UserInterface,\Serializable

After fixing eror making enity abstract causing issues.

   //abstract class User extends EntityRepository implements UserLoaderInterface,UserInterface,\Serializable

if i change enity to abstract my application not working.

  • any suggestion can i implement without making entity class abstract.

Refrence

https://symfony.com/doc/current/security/user_provider.html

How to do username case insensitive in login form?

afeef
  • 4,396
  • 11
  • 35
  • 65
  • oracle sir problem in Oracle db 12c – afeef May 28 '20 at 12:38
  • 1
    Configuration is slightly different for a [custom user provider](https://symfony.com/doc/current/security/user_provider.html#creating-a-custom-user-provider). You basically need to explicitly provide the service id. The example you followed with just the entity would work if the entity was correctly mapped to the user repository AND if you defined a service with the repository class name as the service id. – Cerad May 28 '20 at 12:48
  • And your service file is messed up. You should be defining a UserRepository service, not a generic Doctrine entity repository service. What Symfony version are you on? Autowire can actually make you life easier. – Cerad May 28 '20 at 12:52
  • If you use something like `->setParameter('query', strtolower($username))` you have to be careful. `strtolower` is not unicode-safe. You could use the new Symfony String component for this: `u($username)->lower();` – dbrumann May 28 '20 at 13:25
  • symfony 4.4 followed https://symfony.com/doc/current/security/user_provider.html – afeef May 28 '20 at 15:01
  • 1
    As mentioned in my earlier comment, you don't have your UserRepository mapped properly to your User entity. – Cerad May 28 '20 at 17:44
  • Yes problem resolved , customer provider working – afeef May 29 '20 at 06:27
  • can anyone tells full query loadUserByUsername which symfony uses for login_check – afeef May 29 '20 at 17:45

1 Answers1

0

You can use FOSUser approach with canonical username and email

  1. Add additional field && save the canonical value
  2. Use canonical field to search user

    public function findUserByUsername($username)
    {
        return $this->findUserBy(['usernameCanonical' => $this->canonicalFieldsUpdater->canonicalizeUsername($username)]);
    }
    //------------
    
    public function canonicalize($string)
    {
        if (null === $string) {
            return;
        }
    
        $encoding = mb_detect_encoding($string);
        $result = $encoding
            ? mb_convert_case($string, MB_CASE_LOWER, $encoding)
            : mb_convert_case($string, MB_CASE_LOWER);
    
        return $result;
    }
    
Ivan Borysenko
  • 235
  • 1
  • 2
  • 12