I created a method in the UserRepository: loadUserByIdentifier and loadUserByUsername. However, these are not addressed. Why?
Actually, a dump output should appear.
Do I have to explicitly specify these methods somewhere in order to be used as a login method?
When I click on Login, the login form simply reloads.
My UserRepository:
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use function get_class;
/**
* @extends ServiceEntityRepository<User>
*
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
public function add(User $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(User $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->add($user, true);
}
public function findAllUsers($ownUser, $q = null)
{
$qb = $this->createQueryBuilder('user');
$query = $qb
->select('u')
->from('App\Entity\User', 'u')
->where('u.firstname LIKE :user')
->orWhere('u.lastname LIKE :user')
->andWhere($qb->expr()->neq('u.id', $ownUser))
->setParameter('user', $q)
->setParameter('user', '%'. $q . '%')
->orderBy('u.firstname', 'ASC')
->distinct()
->getQuery();
return $query->getArrayResult();
}
public function loadUserByUsername(string $usernameOrEmail)
{
dump($usernameOrEmail);
exit();
return $this->loadUserByIdentifier($usernameOrEmail);
}
public function loadUserByIdentifier(string $usernameOrEmail): ?User
{
if (!filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) {
$name = explode(" ", $usernameOrEmail);
dump($name);
exit();
}
return $this->createQueryBuilder('u')
->where('u.email = :emailOrUsername')
->orWhere('u.username = :emailOrUsername')
->setParameter('emailOrUsername', $usernameOrEmail)
->getQuery()
->getOneOrNullResult();
}
// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}
My security.yaml:
security:
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
App\Entity\User:
algorithm: auto
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
# used to reload user from session & other features (e.g. switch_user)
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
logout:
path: app_logout
CodePudding user response:
The better way is to do that (according to the Symfony doc here) :
You have to remove the property key from the user provider in security.yaml and write it like that :
# config/packages/security.yaml
security:
providers:
users:
entity:
class: App\Entity\User
# ...
And adapt this function to coresspond with the doc :
public function loadUserByIdentifier(string $usernameOrEmail): ?User
{
$entityManager = $this->getEntityManager();
return $entityManager->createQuery(
'SELECT u
FROM App\Entity\User u
WHERE u.username = :query
OR u.email = :query'
)
->setParameter('query', $usernameOrEmail)
->getOneOrNullResult();
}
Also you have to remove enable_authenticator_manager: true
.
EDIT (to answer your question)
Regarding your question, here is the minimum `main' firewall configuration:
main:
anonymous: ~
form_login:
login_path: app_login
check_path: app_login
logout:
path: app_logout