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
|
|
|
|
34
|
7 |
|
public function __construct(ManagerRegistry $registry, int $passwordRequestTimeout, string $entityClass) |
35
|
|
|
{ |
36
|
7 |
|
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
|
7 |
|
parent::__construct($registry, $entityClass); |
40
|
7 |
|
$this->passwordRequestTimeout = $passwordRequestTimeout; |
41
|
7 |
|
} |
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 findOneWithPasswordResetToken(string $username): ?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 IS NOT NULL') |
60
|
2 |
|
->andWhere('u.passwordRequestedAt > :passwordRequestedAt') |
61
|
2 |
|
->setParameter('username', $username) |
62
|
2 |
|
->setParameter('passwordRequestedAt', $minimumRequestDateTime) |
63
|
2 |
|
->getQuery() |
64
|
2 |
|
->getOneOrNullResult(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function findOneByUsernameAndNewEmailAddress(string $username, string $email): ?AbstractUser |
68
|
|
|
{ |
69
|
1 |
|
return $this->createQueryBuilder('u') |
70
|
1 |
|
->andWhere('u.username = :username') |
71
|
1 |
|
->andWhere('u.newEmailAddress = :email') |
72
|
1 |
|
->andWhere('u.newEmailConfirmationToken IS NOT NULL') |
73
|
1 |
|
->setParameter('username', $username) |
74
|
1 |
|
->setParameter('email', $email) |
75
|
1 |
|
->getQuery() |
76
|
1 |
|
->getOneOrNullResult(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function loadUserByUsername($usernameOrEmail): ?AbstractUser |
80
|
|
|
{ |
81
|
1 |
|
return $this->createQueryBuilder('u') |
82
|
1 |
|
->andWhere('u.username = :username') |
83
|
1 |
|
->orWhere('u.emailAddress = :username') |
84
|
1 |
|
->setParameter('username', $usernameOrEmail) |
85
|
1 |
|
->getQuery() |
86
|
1 |
|
->getOneOrNullResult(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function findExistingUserByNewEmail(AbstractUser $user): ?AbstractUser |
90
|
|
|
{ |
91
|
1 |
|
$queryBuilder = $this->createQueryBuilder('u'); |
92
|
1 |
|
$expr = $queryBuilder->expr(); |
93
|
|
|
$queryBuilder |
94
|
1 |
|
->andWhere($expr->eq('u.emailAddress', ':email_address')) |
95
|
1 |
|
->andWhere($expr->neq('u', ':user')) |
96
|
1 |
|
->setParameter('email_address', $user->getNewEmailAddress()) |
97
|
1 |
|
->setParameter('user', $user, $this->getClassMetadata()->getTypeOfField('id')); |
98
|
|
|
|
99
|
1 |
|
return $queryBuilder->getQuery()->getOneOrNullResult(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|