Fixed static analysis issue and added test for UserRepository

This commit is contained in:
Jan Böhmer 2023-08-01 16:20:31 +02:00
parent c981476706
commit b3153dac68
5 changed files with 117 additions and 3 deletions

View file

@ -42,6 +42,7 @@ final class UserRepository extends NamedDBElementRepository implements PasswordU
/**
* Returns the anonymous user.
* The result is cached, so the database is only called once, after the anonymous user was found.
* @return User|null The user if it is existing, null if no one matched the criteria
*/
public function getAnonymousUser(): ?User
{
@ -54,6 +55,30 @@ final class UserRepository extends NamedDBElementRepository implements PasswordU
return $this->anonymous_user;
}
/**
* Find a user by its username.
* @param string $username
* @return User|null
*/
public function findByUsername(string $username): ?User
{
if ($username === '') {
return null;
}
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where('u.name = (:name)');
$qb->setParameter('name', $username);
try {
return $qb->getQuery()->getOneOrNullResult();
} catch (NonUniqueResultException) {
return null;
}
}
/**
* Find a user by its name or its email. Useful for login or password reset purposes.
*