-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathResetPasswordRequestRepositoryTrait.php
More file actions
127 lines (108 loc) · 4.14 KB
/
Copy pathResetPasswordRequestRepositoryTrait.php
File metadata and controls
127 lines (108 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the SymfonyCasts ResetPasswordBundle package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyCasts\Bundle\ResetPassword\Persistence\Repository;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Clock\Clock;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
/**
* Trait can be added to a Doctrine ORM repository to help implement
* ResetPasswordRequestRepositoryInterface.
*
* @author Jesse Rushlow <jr@rushlow.dev>
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
trait ResetPasswordRequestRepositoryTrait
{
public function getUserIdentifier(object $user): string
{
return (string) $this->getEntityManager()
->getUnitOfWork()
->getSingleIdentifierValue($user)
;
}
public function persistResetPasswordRequest(ResetPasswordRequestInterface $resetPasswordRequest): void
{
$this->getEntityManager()->persist($resetPasswordRequest);
$this->getEntityManager()->flush();
}
public function findResetPasswordRequest(string $selector): ?ResetPasswordRequestInterface
{
return $this->findOneBy(['selector' => $selector]);
}
public function getMostRecentNonExpiredRequestDate(object $user): ?\DateTimeInterface
{
$builder = $this->setUserParam($this->createQueryBuilder('t'), $user);
// Normally there is only 1 max request per use, but written to be flexible
/** @var ResetPasswordRequestInterface $resetPasswordRequest */
$resetPasswordRequest = $builder
->where('t.user = :user')
->orderBy('t.requestedAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
if (null !== $resetPasswordRequest && !$resetPasswordRequest->isExpired()) {
return $resetPasswordRequest->getRequestedAt();
}
return null;
}
public function removeResetPasswordRequest(ResetPasswordRequestInterface $resetPasswordRequest): void
{
$builder = $this->setUserParam($this->createQueryBuilder('t'), $resetPasswordRequest->getUser());
$builder
->delete()
->where('t.user = :user')
->getQuery()
->execute()
;
}
public function removeExpiredResetPasswordRequests(): int
{
$time = Clock::get()->now()->modify('-1 week');
$query = $this->createQueryBuilder('t')
->delete()
->where('t.expiresAt <= :time')
->setParameter('time', $time)
->getQuery()
;
return $query->execute();
}
/**
* Remove a users ResetPasswordRequest objects from persistence.
*
* Warning - This is a destructive operation. Calling this method
* may have undesired consequences for users who have valid
* ResetPasswordRequests but have not "checked their email" yet.
*
* @see https://github.com/SymfonyCasts/reset-password-bundle?tab=readme-ov-file#advanced-usage
*/
public function removeRequests(object $user): void
{
$builder = $this->setUserParam($this->createQueryBuilder('t'), $user)
->delete()
->where('t.user = :user')
;
$builder->getQuery()->execute();
}
private function setUserParam(QueryBuilder $queryBuilder, object $user): QueryBuilder
{
$meta = $this->getEntityManager()->getClassMetadata($user::class);
$identifier = PropertyAccess::createPropertyAccessor()->getValue($user, $meta->getSingleIdentifierFieldName());
if ($identifier instanceof Ulid) {
$queryBuilder->setParameter('user', $identifier, 'ulid');
} elseif ($identifier instanceof Uuid) {
$queryBuilder->setParameter('user', $identifier, 'uuid');
} else {
$queryBuilder->setParameter('user', $user);
}
return $queryBuilder;
}
}