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\UsersBundle\Entity; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Collections\Collection; |
18
|
|
|
use Doctrine\ORM\Mapping as ORM; |
19
|
|
|
use Nucleos\UserBundle\Model\User as BaseUser; |
20
|
|
|
use Nucleos\UserBundle\Validator\Constraints\Pattern as PasswordPattern; |
21
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
22
|
|
|
use Zikula\CoreBundle\Doctrine\DBAL\CustomTypes; |
23
|
|
|
use Zikula\LegalBundle\Entity\LegalAwareUserInterface; |
24
|
|
|
use Zikula\LegalBundle\Entity\LegalAwareUserTrait; |
25
|
|
|
use Zikula\UsersBundle\UsersConstant; |
26
|
|
|
|
27
|
|
|
// #[ORM\Entity(repositoryClass: UserRepository::class)] TODO remove if unneeded |
28
|
|
|
#[ORM\Entity] |
29
|
|
|
#[ORM\Table(name: 'nucleos_user__user')] |
30
|
|
|
#[ |
31
|
|
|
ORM\Index(fields: ['username'], name: 'username'), |
32
|
|
|
ORM\Index(fields: ['email'], name: 'email') |
33
|
|
|
] |
34
|
|
|
class User extends BaseUser implements LegalAwareUserInterface |
35
|
|
|
{ |
36
|
|
|
use LegalAwareUserTrait; |
37
|
|
|
use UserAttributesTrait; |
38
|
|
|
|
39
|
|
|
#[ORM\Id] |
40
|
|
|
#[ORM\Column] |
41
|
|
|
#[ORM\GeneratedValue(strategy: 'AUTO')] |
42
|
|
|
protected ?int $id = null; |
43
|
|
|
|
44
|
|
|
#[Assert\NoSuspiciousCharacters] |
45
|
|
|
protected ?string $username = null; |
46
|
|
|
|
47
|
|
|
#[Assert\Length(min: 12)] |
48
|
|
|
#[Assert\PasswordStrength] |
49
|
|
|
#[PasswordPattern(minUpper: 1, minLower: 1, minNumeric: 1, minSpecial: 1)] |
50
|
|
|
protected ?string $plainPassword = null; |
51
|
|
|
|
52
|
|
|
#[ORM\ManyToMany(targetEntity: Group::class)] |
53
|
|
|
#[ORM\JoinTable(name: 'nucleos_user__user_group')] |
54
|
|
|
#[ORM\JoinColumn(name: 'user_id')] |
55
|
|
|
#[ORM\InverseJoinColumn(name: 'group_id')] |
56
|
|
|
protected Collection $groups; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Account State: The user's current state, see \Zikula\UsersBundle\UsersConstant::ACTIVATED_* for defined constants. |
60
|
|
|
* A state represented by a negative integer means that the user's account is in a pending state, and should not yet be considered a "real" user account. |
61
|
|
|
* For example, user accounts pending the completion of the registration process (because either moderation, e-mail verification, or both are in use) |
62
|
|
|
* will have a negative integer representing their state. If the user's registration request expires before it the process is completed, or if the administrator |
63
|
|
|
* denies the request for a new account, the user account record will be deleted. |
64
|
|
|
* When this deletion happens, it will be assumed by the system that no external bundle has yet interacted with the user account record, |
65
|
|
|
* because its state never progressed beyond its pending state, and therefore normal hooks/events may not be triggered |
66
|
|
|
* (although it is possible that events regarding the pending account may be triggered). |
67
|
|
|
*/ |
68
|
|
|
#[Assert\Choice(callback: 'getActivatedValues')] // TODO replace by enum |
69
|
|
|
#[ORM\Column] |
70
|
|
|
private int $activated; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Account Approved Date/Time: The date and time the user's registration request was approved through the moderation process. |
74
|
|
|
* If the moderation process was not in effect at the time the user made a registration request, then this will be the date and time of the registration request. |
75
|
|
|
*/ |
76
|
|
|
#[ORM\Column(name: 'approved_date', type: CustomTypes::DATETIME_UTC)] |
77
|
|
|
private \DateTimeInterface $approvedDate; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The uid of the user account that approved the request to register a new account. |
81
|
|
|
* If this is the same as the user account's uid, then moderation was not in use at the time the request for a new account was made. |
82
|
|
|
* If this is -1, the user account that approved the request has since been deleted. If this is 0, the user account has not yet been approved. |
83
|
|
|
*/ |
84
|
|
|
#[ORM\Column(name: 'approved_by')] |
85
|
|
|
private int $approvedBy; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Registration Date/Time: Date/time the user account was registered. |
89
|
|
|
* For users not pending the completion of the registration process, this is the date and time the user account completed the process. |
90
|
|
|
* For example, if registrations are moderated, then this is the date and time the registration request was approved. |
91
|
|
|
* If registration e-mail addresses must be verified, then this is the date and time the user completed the verification process. |
92
|
|
|
* If both moderation and verification are in use, then this is the later of those two dates. |
93
|
|
|
* If neither is in use, then this is simply the date and time the user's registration request was made. |
94
|
|
|
* If the user account's activated state is "pending registration" (implying that either moderation, verification, or both are in use) |
95
|
|
|
* then this will be the date and time the user made the registration request UNTIL the registration process is complete, and then it is updated as above. |
96
|
|
|
*/ |
97
|
|
|
#[ORM\Column(name: 'user_regdate', type: CustomTypes::DATETIME_UTC)] |
98
|
|
|
private \DateTimeInterface $registrationDate; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Additional attributes of this user |
102
|
|
|
*/ |
103
|
|
|
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAttribute::class, cascade: ['all'], orphanRemoval: true, indexBy: 'name')] |
104
|
|
|
private Collection $attributes; |
105
|
|
|
|
106
|
|
|
public function __construct() |
107
|
|
|
{ |
108
|
|
|
parent::__construct(); |
109
|
|
|
|
110
|
|
|
$utcTZ = new \DateTimeZone('UTC'); |
111
|
|
|
$this->approvedDate = new \DateTime('1970-01-01 00:00:00', $utcTZ); |
112
|
|
|
$this->approvedBy = 0; |
113
|
|
|
$this->registrationDate = new \DateTime('1970-01-01 00:00:00', $utcTZ); |
114
|
|
|
|
115
|
|
|
$this->attributes = new ArrayCollection(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getId(): ?int |
119
|
|
|
{ |
120
|
|
|
return $this->id; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setId(?int $id): self |
124
|
|
|
{ |
125
|
|
|
$this->id = $id; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getActivated(): int |
131
|
|
|
{ |
132
|
|
|
return $this->activated; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setActivated(int $activated): self |
136
|
|
|
{ |
137
|
|
|
$this->activated = $activated; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getApprovedDate(): \DateTimeInterface |
143
|
|
|
{ |
144
|
|
|
if (null !== $this->getTimezone()) { |
145
|
|
|
$this->approvedDate->setTimeZone(new \DateTimeZone($this->getTimezone())); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->approvedDate; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function setApprovedDate(string|\DateTimeInterface $approvedDate): self |
152
|
|
|
{ |
153
|
|
|
if ($approvedDate instanceof \DateTimeInterface) { |
154
|
|
|
$this->approvedDate = $approvedDate; |
155
|
|
|
} else { |
156
|
|
|
$this->approvedDate = new \DateTime($approvedDate); |
157
|
|
|
} |
158
|
|
|
$this->setTimezone($this->approvedDate->getTimeZone()->getName()); |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getApprovedBy(): int |
164
|
|
|
{ |
165
|
|
|
return $this->approvedBy; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function setApprovedBy(int $approvedBy): self |
169
|
|
|
{ |
170
|
|
|
$this->approvedBy = $approvedBy; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function isApproved(): bool |
176
|
|
|
{ |
177
|
|
|
return 0 !== $this->approvedBy; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getRegistrationDate(): \DateTimeInterface |
181
|
|
|
{ |
182
|
|
|
if (null !== $this->getTimezone()) { |
183
|
|
|
$this->registrationDate->setTimeZone(new \DateTimeZone($this->getTimezone())); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->registrationDate; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function setRegistrationDate(string|\DateTimeInterface $registrationDate): self |
190
|
|
|
{ |
191
|
|
|
if ($registrationDate instanceof \DateTimeInterface) { |
192
|
|
|
$this->registrationDate = $registrationDate; |
193
|
|
|
} else { |
194
|
|
|
$this->registrationDate = new \DateTime($registrationDate); |
195
|
|
|
} |
196
|
|
|
$this->setTimezone($this->registrationDate->getTimeZone()->getName()); |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Callback function used to validate the activated value. |
203
|
|
|
*/ |
204
|
|
|
public static function getActivatedValues(): array |
205
|
|
|
{ |
206
|
|
|
return [ |
207
|
|
|
UsersConstant::ACTIVATED_ACTIVE, |
208
|
|
|
UsersConstant::ACTIVATED_INACTIVE, |
209
|
|
|
UsersConstant::ACTIVATED_PENDING_DELETE, |
210
|
|
|
UsersConstant::ACTIVATED_PENDING_REG, |
211
|
|
|
]; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|