mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-23 18:28:49 +02:00
Fixed static analysis issue and added test for UserRepository
This commit is contained in:
parent
c981476706
commit
b3153dac68
5 changed files with 117 additions and 3 deletions
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue