1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Repository\User; |
15
|
|
|
|
16
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Silverback\ApiComponentBundle\Entity\User\AbstractUser; |
19
|
|
|
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException; |
20
|
|
|
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Daniel West <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @method AbstractUser|null find($id, $lockMode = null, $lockVersion = null) |
26
|
|
|
* @method AbstractUser|null findOneBy(array $criteria, array $orderBy = null) |
27
|
|
|
* @method AbstractUser[] findAll() |
28
|
|
|
* @method AbstractUser[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
29
|
|
|
*/ |
30
|
|
|
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface |
31
|
|
|
{ |
32
|
|
|
private int $passwordRequestTimeout; |
33
|
|
|
|
34
|
6 |
|
public function __construct(ManagerRegistry $registry, int $passwordRequestTimeout, string $entityClass) |
35
|
|
|
{ |
36
|
6 |
|
if (!is_subclass_of($entityClass, AbstractUser::class)) { |
37
|
1 |
|
throw new InvalidArgumentException(sprintf('The entity class `%s` used for the repository `%s` must be a subclass of `%s`', $entityClass, __CLASS__, AbstractUser::class)); |
38
|
|
|
} |
39
|
6 |
|
parent::__construct($registry, $entityClass); |
40
|
6 |
|
$this->passwordRequestTimeout = $passwordRequestTimeout; |
41
|
6 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function findOneByEmail($value): ?AbstractUser |
44
|
|
|
{ |
45
|
1 |
|
return $this->createQueryBuilder('u') |
46
|
1 |
|
->andWhere('u.emailAddress = :val') |
47
|
1 |
|
->setParameter('val', $value) |
48
|
1 |
|
->getQuery() |
49
|
1 |
|
->getOneOrNullResult(); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function findOneByPasswordResetToken(string $username, string $token): ?AbstractUser |
53
|
|
|
{ |
54
|
2 |
|
$minimumRequestDateTime = new \DateTime(); |
55
|
2 |
|
$minimumRequestDateTime->modify(sprintf('-%d seconds', $this->passwordRequestTimeout)); |
56
|
|
|
|
57
|
2 |
|
return $this->createQueryBuilder('u') |
58
|
2 |
|
->andWhere('u.username = :username') |
59
|
2 |
|
->andWhere('u.newPasswordConfirmationToken = :token') |
60
|
2 |
|
->andWhere('u.newPasswordConfirmationToken IS NOT NULL') |
61
|
2 |
|
->andWhere('u.passwordRequestedAt > :passwordRequestedAt') |
62
|
2 |
|
->setParameter('username', $username) |
63
|
2 |
|
->setParameter('token', $token) |
64
|
2 |
|
->setParameter('passwordRequestedAt', $minimumRequestDateTime) |
65
|
2 |
|
->getQuery() |
66
|
2 |
|
->getOneOrNullResult(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function findOneByEmailVerificationToken(string $username, string $email, string $token): ?AbstractUser |
70
|
|
|
{ |
71
|
1 |
|
return $this->createQueryBuilder('u') |
72
|
1 |
|
->andWhere('u.username = :username') |
73
|
1 |
|
->andWhere('u.newEmailAddress = :email') |
74
|
1 |
|
->andWhere('u.newEmailVerificationToken = :token') |
75
|
1 |
|
->andWhere('u.newEmailVerificationToken IS NOT NULL') |
76
|
1 |
|
->setParameter('username', $username) |
77
|
1 |
|
->setParameter('email', $email) |
78
|
1 |
|
->setParameter('token', $token) |
79
|
1 |
|
->getQuery() |
80
|
1 |
|
->getOneOrNullResult(); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function loadUserByUsername($usernameOrEmail): ?AbstractUser |
84
|
|
|
{ |
85
|
1 |
|
return $this->createQueryBuilder('u') |
86
|
1 |
|
->andWhere('u.username = :username') |
87
|
1 |
|
->orWhere('u.emailAddress = :username') |
88
|
1 |
|
->setParameter('username', $usernameOrEmail) |
89
|
1 |
|
->getQuery() |
90
|
1 |
|
->getOneOrNullResult(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|