Allow users to be queried by either id, email or username

This commit is contained in:
2024-03-21 12:40:48 +01:00
parent eda2cc21c8
commit 798aeaeb97
2 changed files with 19 additions and 7 deletions
+4 -7
View File
@@ -13,23 +13,20 @@ use Symfony\Component\Validator\Constraints as Assert;
final class UserTokenLoginDto
{
/**
* The username of the User to be authenticated.
* The identifier (email, username) of the User to be authenticated.
*/
#[Assert\NotBlank()]
#[Assert\Length(min: 4, max: 30)]
#[Assert\Regex('/^[a-z0-9_-]+$/')]
public readonly string $username;
public readonly string $identifier;
/**
* The password of the User to be authenticated.
*/
#[Assert\NotBlank()]
#[Assert\Length(min: 12)]
public readonly string $password;
public function __construct(string $username, string $password)
public function __construct(string $identifier, string $password)
{
$this->username = $username;
$this->identifier = $identifier;
$this->password = $password;
}
}
+15
View File
@@ -41,6 +41,21 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
$this->getEntityManager()->flush();
}
/**
* Find an User by any of it's unique properties
* @param string $identifier An id, email or username
*/
public function findOneByIdentifier(string $identifier): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.id = :val')
->orWhere('u.email = :val')
->orWhere('u.username = :val')
->setParameter('val', $identifier)
->getQuery()
->getOneOrNullResult();
}
public function findOneByAccounting(Accounting $accounting): ?User
{
return $this->createQueryBuilder('u')