1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components 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\ApiComponentsBundle\Repository\User; |
15
|
|
|
|
16
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser; |
19
|
|
|
use Silverback\ApiComponentsBundle\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
|
|
|
private int $newEmailConfirmTimeout; |
34
|
7 |
|
|
35
|
|
|
public function __construct(ManagerRegistry $registry, string $entityClass, int $passwordRequestTimeout, int $newEmailConfirmTimeout) |
36
|
7 |
|
{ |
37
|
1 |
|
if (!is_subclass_of($entityClass, AbstractUser::class)) { |
38
|
|
|
throw new InvalidArgumentException(sprintf('The entity class `%s` used for the repository `%s` must be a subclass of `%s`', $entityClass, __CLASS__, AbstractUser::class)); |
39
|
7 |
|
} |
40
|
7 |
|
parent::__construct($registry, $entityClass); |
41
|
7 |
|
$this->passwordRequestTimeout = $passwordRequestTimeout; |
42
|
|
|
$this->newEmailConfirmTimeout = $newEmailConfirmTimeout; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
|
public function findOneByEmail($value): ?AbstractUser |
46
|
1 |
|
{ |
47
|
1 |
|
return $this->createQueryBuilder('u') |
48
|
1 |
|
->andWhere('u.emailAddress = :val') |
49
|
1 |
|
->setParameter('val', $value) |
50
|
|
|
->getQuery() |
51
|
|
|
->getOneOrNullResult(); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
2 |
|
public function findOneWithPasswordResetToken(string $username): ?AbstractUser |
55
|
2 |
|
{ |
56
|
|
|
$minimumRequestDateTime = new \DateTime(); |
57
|
2 |
|
$minimumRequestDateTime->modify(sprintf('-%d seconds', $this->passwordRequestTimeout)); |
58
|
2 |
|
|
59
|
2 |
|
return $this->createQueryBuilder('u') |
60
|
2 |
|
->andWhere('u.username = :username') |
61
|
2 |
|
->andWhere('u.newPasswordConfirmationToken IS NOT NULL') |
62
|
2 |
|
->andWhere('u.passwordRequestedAt > :minimumDateTime') |
63
|
2 |
|
->setParameter('username', $username) |
64
|
2 |
|
->setParameter('minimumDateTime', $minimumRequestDateTime) |
65
|
2 |
|
->getQuery() |
66
|
2 |
|
->getOneOrNullResult(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function findOneByUsernameAndNewEmailAddress(string $username, string $email): ?AbstractUser |
70
|
|
|
{ |
71
|
1 |
|
$minimumRequestDateTime = new \DateTime(); |
72
|
1 |
|
$minimumRequestDateTime->modify(sprintf('-%d seconds', $this->newEmailConfirmTimeout)); |
73
|
1 |
|
|
74
|
1 |
|
return $this->createQueryBuilder('u') |
75
|
1 |
|
->andWhere('u.username = :username') |
76
|
1 |
|
->andWhere('u.newEmailAddress = :email') |
77
|
1 |
|
->andWhere('u.newEmailConfirmationToken IS NOT NULL') |
78
|
1 |
|
->andWhere('u.newEmailAddressChangeRequestedAt > :minimumDateTime') |
79
|
1 |
|
->setParameter('username', $username) |
80
|
1 |
|
->setParameter('email', $email) |
81
|
|
|
->setParameter('minimumDateTime', $minimumRequestDateTime) |
82
|
|
|
->getQuery() |
83
|
1 |
|
->getOneOrNullResult(); |
84
|
|
|
} |
85
|
1 |
|
|
86
|
1 |
|
public function loadUserByUsername($usernameOrEmail): ?AbstractUser |
87
|
1 |
|
{ |
88
|
1 |
|
return $this->createQueryBuilder('u') |
89
|
1 |
|
->andWhere('u.username = :username') |
90
|
1 |
|
->orWhere('u.emailAddress = :username') |
91
|
|
|
->setParameter('username', $usernameOrEmail) |
92
|
|
|
->getQuery() |
93
|
1 |
|
->getOneOrNullResult(); |
94
|
|
|
} |
95
|
1 |
|
|
96
|
1 |
|
public function findExistingUserByNewEmail(AbstractUser $user): ?AbstractUser |
97
|
|
|
{ |
98
|
1 |
|
$queryBuilder = $this->createQueryBuilder('u'); |
99
|
1 |
|
$expr = $queryBuilder->expr(); |
100
|
1 |
|
$queryBuilder |
101
|
1 |
|
->andWhere($expr->eq('u.emailAddress', ':email_address')) |
102
|
|
|
->andWhere($expr->neq('u', ':user')) |
103
|
1 |
|
->setParameter('email_address', $user->getNewEmailAddress()) |
104
|
|
|
->setParameter('user', $user, $this->getClassMetadata()->getTypeOfField('id')); |
105
|
|
|
|
106
|
|
|
return $queryBuilder->getQuery()->getOneOrNullResult(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|