|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\ZAuthModule\Entity\Repository; |
|
15
|
|
|
|
|
16
|
|
|
use DateTime; |
|
17
|
|
|
use DateTimeZone; |
|
18
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
19
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
use Zikula\UsersModule\Entity\UserEntity; |
|
22
|
|
|
use Zikula\ZAuthModule\Entity\AuthenticationMappingEntity; |
|
23
|
|
|
use Zikula\ZAuthModule\Entity\RepositoryInterface\UserVerificationRepositoryInterface; |
|
24
|
|
|
use Zikula\ZAuthModule\Entity\UserVerificationEntity; |
|
25
|
|
|
use Zikula\ZAuthModule\ZAuthConstant; |
|
26
|
|
|
|
|
27
|
|
|
class UserVerificationRepository extends ServiceEntityRepository implements UserVerificationRepositoryInterface |
|
28
|
|
|
{ |
|
29
|
|
|
public function __construct(ManagerRegistry $registry) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($registry, UserVerificationEntity::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function persistAndFlush(UserVerificationEntity $entity): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->_em->persist($entity); |
|
37
|
|
|
$this->_em->flush(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function removeAndFlush(UserVerificationEntity $entity): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->_em->remove($entity); |
|
43
|
|
|
$this->_em->flush(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function removeByZikulaId(int $userId): void |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var UserVerificationEntity $entity */ |
|
49
|
|
|
$entity = $this->findOneBy(['uid' => $userId]); |
|
50
|
|
|
if ($entity) { |
|
|
|
|
|
|
51
|
|
|
$this->removeAndFlush($entity); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function purgeExpiredRecords( |
|
56
|
|
|
int $daysOld, |
|
57
|
|
|
int $changeType = ZAuthConstant::VERIFYCHGTYPE_REGEMAIL, |
|
58
|
|
|
bool $deleteUserEntities = true |
|
59
|
|
|
): array { |
|
60
|
|
|
if ($daysOld < 1) { |
|
61
|
|
|
return []; |
|
62
|
|
|
} |
|
63
|
|
|
// Expiration date/times, as with all date/times in the Users module, are stored as UTC. |
|
64
|
|
|
$staleRecordUTC = new DateTime(null, new DateTimeZone('UTC')); |
|
65
|
|
|
$staleRecordUTC->modify("-{$daysOld} days"); |
|
66
|
|
|
|
|
67
|
|
|
$qb = $this->createQueryBuilder('v'); |
|
68
|
|
|
$and = $qb->expr()->andX() |
|
69
|
|
|
->add($qb->expr()->eq('v.changetype', ':changeType')) |
|
70
|
|
|
->add($qb->expr()->isNotNull('v.createdDate')) |
|
71
|
|
|
->add($qb->expr()->neq('v.createdDate', ':createdDtNot')) |
|
72
|
|
|
->add($qb->expr()->lt('v.createdDate', ':createdDtMax')); |
|
73
|
|
|
$qb->select('v') |
|
74
|
|
|
->where($and) |
|
75
|
|
|
->setParameter('changeType', $changeType) |
|
76
|
|
|
->setParameter('createdDtNot', '0000-00-00 00:00:00') |
|
77
|
|
|
->setParameter('createdDtMax', $staleRecordUTC); |
|
78
|
|
|
$staleVerificationRecords = $qb->getQuery()->getResult(); |
|
79
|
|
|
|
|
80
|
|
|
$deletedUsers = []; |
|
81
|
|
|
$userRepo = $this->_em->getRepository(UserEntity::class); |
|
82
|
|
|
$authRepo = $this->_em->getRepository(AuthenticationMappingEntity::class); |
|
83
|
|
|
if (!empty($staleVerificationRecords)) { |
|
84
|
|
|
foreach ($staleVerificationRecords as $staleVerificationRecord) { |
|
85
|
|
|
if ($deleteUserEntities) { |
|
86
|
|
|
$user = $userRepo->find($staleVerificationRecord['uid']); |
|
87
|
|
|
if (null !== $user) { |
|
88
|
|
|
$deletedUsers[] = $user; |
|
89
|
|
|
// delete user |
|
90
|
|
|
$this->_em->remove($user); |
|
91
|
|
|
} |
|
92
|
|
|
$mapping = $authRepo->findOneBy(['uid' => $staleVerificationRecord['uid']]); |
|
93
|
|
|
if (null !== $mapping) { |
|
94
|
|
|
// delete mapping |
|
95
|
|
|
$this->_em->remove($mapping); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// delete verification record |
|
100
|
|
|
$this->_em->remove($staleVerificationRecord); |
|
101
|
|
|
} |
|
102
|
|
|
$this->_em->flush(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $deletedUsers; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function resetVerifyChgFor(int $userId, $types = null): void |
|
109
|
|
|
{ |
|
110
|
|
|
$qb = $this->createQueryBuilder('v') |
|
111
|
|
|
->delete() |
|
112
|
|
|
->where('v.uid = :uid') |
|
113
|
|
|
->setParameter('uid', $userId); |
|
114
|
|
|
if (isset($types)) { |
|
115
|
|
|
$qb->andWhere($qb->expr()->in('v.changetype', ':changeType')) |
|
116
|
|
|
->setParameter('changeType', $types); |
|
117
|
|
|
} |
|
118
|
|
|
$query = $qb->getQuery(); |
|
119
|
|
|
$query->execute(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function isVerificationEmailSent(int $userId): bool |
|
123
|
|
|
{ |
|
124
|
|
|
/** @var UserVerificationEntity $userVerification */ |
|
125
|
|
|
$userVerification = $this->findOneBy(['uid' => $userId]); |
|
126
|
|
|
|
|
127
|
|
|
return null !== $userVerification && null !== $userVerification->getCreatedDate(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function setVerificationCode( |
|
131
|
|
|
int $userId, |
|
132
|
|
|
int $changeType = ZAuthConstant::VERIFYCHGTYPE_PWD, |
|
133
|
|
|
string $hashedConfirmationCode = null, |
|
134
|
|
|
string $email = null |
|
135
|
|
|
): void { |
|
136
|
|
|
if (empty($hashedConfirmationCode)) { |
|
137
|
|
|
throw new InvalidArgumentException(); |
|
138
|
|
|
} |
|
139
|
|
|
$nowUTC = new DateTime('now', new DateTimeZone('UTC')); |
|
140
|
|
|
|
|
141
|
|
|
$query = $this->createQueryBuilder('v') |
|
142
|
|
|
->delete() |
|
143
|
|
|
->where('v.uid = :uid') |
|
144
|
|
|
->andWhere('v.changetype = :changeType') |
|
145
|
|
|
->setParameter('uid', $userId) |
|
146
|
|
|
->setParameter('changeType', $changeType) |
|
147
|
|
|
->getQuery(); |
|
148
|
|
|
$query->execute(); |
|
149
|
|
|
|
|
150
|
|
|
$entity = new UserVerificationEntity(); |
|
151
|
|
|
$entity->setChangetype($changeType); |
|
152
|
|
|
$entity->setUid($userId); |
|
153
|
|
|
$entity->setVerifycode($hashedConfirmationCode); |
|
154
|
|
|
$entity->setCreatedDate($nowUTC); |
|
155
|
|
|
if (!empty($email)) { |
|
156
|
|
|
$entity->setNewemail($email); |
|
157
|
|
|
} |
|
158
|
|
|
$this->_em->persist($entity); |
|
159
|
|
|
$this->_em->flush(); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|