Test Failed
Pull Request — master (#72)
by Daniel
05:27
created

AbstractUser::setNewEmailConfirmationToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Entity\User;
15
16
use ApiPlatform\Core\Annotation\ApiProperty;
17
use DateTime;
18
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface;
19
use Ramsey\Uuid\Uuid;
20
use Silverback\ApiComponentsBundle\Annotation as Silverback;
21
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait;
22
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
23
use Silverback\ApiComponentsBundle\Validator\Constraints as AcbAssert;
24
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
25
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
26
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
27
use Symfony\Component\Serializer\Annotation\Groups;
28
use Symfony\Component\Validator\Constraints as Assert;
29
30
/**
31
 * @author Daniel West <[email protected]>
32
 *
33
 * @Silverback\Timestamped
34
 * @UniqueEntity(fields={"username"}, errorPath="username", message="Sorry, that user already exists in the database.")
35
 * @UniqueEntity(fields={"emailAddress"}, errorPath="emailAddress", message="Sorry, that email address already exists in the database.")
36
 * @AcbAssert\NewEmailAddress(groups={"User:emailAddress", "Default"})
37
 */
38
abstract class AbstractUser implements SymfonyUserInterface, JWTUserInterface
39
{
40
    use IdTrait;
41
    use TimestampedTrait;
42
43
    /**
44
     * @Assert\NotBlank(groups={"Default"}, message="Please enter a username.")
45
     * @Groups({"User:superAdmin", "User:output"})
46
     */
47
    protected ?string $username;
48
49
    /**
50
     * @Assert\NotBlank(groups={"Default"})
51
     * @Assert\Email()
52
     * @Groups({"User:superAdmin", "User:output"})
53
     */
54
    protected ?string $emailAddress;
55
56
    /**
57
     * @Groups({"User:superAdmin"})
58
     */
59
    protected array $roles;
60
61
    /**
62
     * @Groups({"User:superAdmin"})
63
     */
64
    protected bool $enabled;
65
66
    /**
67
     * @ApiProperty(readable=false, writable=false)
68
     */
69
    protected string $password;
70
71
    /**
72
     * @ApiProperty(readable=false)
73
     * @Assert\NotBlank(message="Please enter your desired password.", groups={"User:password:create"})
74
     * @Assert\Length(max="4096", min="6", maxMessage="Your password cannot be over 4096 characters", minMessage="Your password must be more than 6 characters long.", groups={"User:password:create"})
75
     * @Groups({"User:input"})
76
     */
77
    protected ?string $plainPassword = null;
78
79
    /**
80
     * Random string sent to the user email address in order to verify it.
81
     *
82
     * @ApiProperty(readable=false, writable=false)
83
     */
84
    protected ?string $newPasswordConfirmationToken = null;
85
86
    /**
87
     *  @ApiProperty(readable=false, writable=false)
88
     */
89
    public ?string $plainNewPasswordConfirmationToken = null;
90
91
    /**
92
     * @ApiProperty(readable=false, writable=false)
93
     */
94
    protected ?DateTime $passwordRequestedAt = null;
95
96
    /**
97
     * @ApiProperty(readable=false)
98
     * @UserPassword(message="You have not entered your current password correctly. Please try again.", groups={"User:password:change"})
99
     * @Groups({"User:input"})
100
     */
101
    protected ?string $oldPassword = null;
102
103
    /**
104
     * @ApiProperty(readable=false, writable=false)
105
     */
106
    protected ?DateTime $passwordUpdatedAt = null;
107
108
    /**
109
     * @Assert\NotBlank(groups={"User:emailAddress"})
110
     * @Groups({"User:input", "User:output", "User:emailAddress"})
111
     */
112
    protected ?string $newEmailAddress = null;
113
114
    /**
115
     * Random string sent to the user's new email address in order to verify it.
116
     *
117
     * @ApiProperty(readable=false, writable=false)
118
     */
119
    protected ?string $newEmailConfirmationToken = null;
120
121 56
    /**
122
     *  @ApiProperty(readable=false, writable=false)
123 56
     */
124 56
    public ?string $plainNewEmailConfirmationToken = null;
125 56
126 56
    /**
127 56
     * @ApiProperty(readable=false, writable=false)
128 56
     */
129 56
    protected bool $emailAddressVerified = false;
130
131 23
    /**
132
     * Random string sent to previous email address when email is changed to permit email restore and password change.
133 23
     *
134
     * @ApiProperty(readable=false, writable=false)
135
     */
136 22
    protected ?string $emailAddressVerifyToken = null;
137
138 22
    /**
139
     *  @ApiProperty(readable=false, writable=false)
140 22
     */
141
    public ?string $plainEmailAddressVerifyToken = null;
142
143 18
    /**
144
     * @ApiProperty(readable=false, writable=false)
145 18
     */
146
    protected ?DateTime $emailLastUpdatedAt = null;
147
148 24
    public function __construct(string $username = '', string $emailAddress = '', bool $emailAddressVerified = false, array $roles = ['ROLE_USER'], string $password = '', bool $enabled = true)
149
    {
150 24
        $this->username = $username;
151
        $this->emailAddress = $emailAddress;
152 24
        $this->emailAddressVerified = $emailAddressVerified;
153
        $this->roles = $roles;
154
        $this->password = $password;
155 4
        $this->enabled = $enabled;
156
    }
157 4
158
    public function getUsername(): ?string
159
    {
160 1
        return $this->username;
161
    }
162 1
163
    public function setUsername(?string $username): self
164 1
    {
165
        $this->username = $username;
166
167 6
        return $this;
168
    }
169 6
170
    public function getEmailAddress(): ?string
171
    {
172 4
        return $this->emailAddress;
173
    }
174 4
175
    public function setEmailAddress(?string $emailAddress): self
176 4
    {
177
        $this->emailAddress = $emailAddress;
178
        if ($emailAddress) {
179 3
            $this->emailLastUpdatedAt = new \DateTime();
180
        }
181 3
182
        return $this;
183
    }
184 2
185
    public function getRoles(): array
186 2
    {
187
        return $this->roles;
188 2
    }
189
190
    public function setRoles(array $roles): self
191 1
    {
192
        $this->roles = $roles;
193 1
194
        return $this;
195
    }
196 1
197
    public function isEnabled(): bool
198 1
    {
199 1
        return $this->enabled;
200
    }
201 1
202
    public function setEnabled(bool $enabled): self
203
    {
204 1
        $this->enabled = $enabled;
205
206
        return $this;
207 3
    }
208
209 3
    public function getPassword(): ?string
210
    {
211
        return $this->password;
212 4
    }
213
214 4
    public function setPassword(string $password): self
215
    {
216 4
        $this->password = $password;
217
218
        return $this;
219 2
    }
220
221 2
    public function getPlainPassword(): ?string
222
    {
223
        return $this->plainPassword;
224 4
    }
225
226 4
    public function setPlainPassword(?string $plainPassword): self
227
    {
228 4
        $this->plainPassword = $plainPassword;
229
        if ($plainPassword) {
230
            // Needs to update mapped field to trigger update event which will encode the plain password
231 1
            $this->passwordUpdatedAt = new \DateTime();
232
        }
233 1
234
        return $this;
235
    }
236 1
237
    public function getNewPasswordConfirmationToken(): ?string
238 1
    {
239
        return $this->newPasswordConfirmationToken;
240 1
    }
241
242
    public function setNewPasswordConfirmationToken(?string $newPasswordConfirmationToken): self
243 7
    {
244
        $this->newPasswordConfirmationToken = $newPasswordConfirmationToken;
245 7
246
        return $this;
247
    }
248 6
249
    public function getPasswordRequestedAt(): ?DateTime
250 6
    {
251
        return $this->passwordRequestedAt;
252 6
    }
253
254
    public function setPasswordRequestedAt(?DateTime $passwordRequestedAt): self
255 5
    {
256
        $this->passwordRequestedAt = $passwordRequestedAt;
257 5
258
        return $this;
259
    }
260 4
261
    public function getOldPassword(): ?string
262 4
    {
263
        return $this->oldPassword;
264 4
    }
265
266
    public function setOldPassword(?string $oldPassword): self
267 6
    {
268
        $this->oldPassword = $oldPassword;
269 6
270
        return $this;
271
    }
272 5
273
    public function getNewEmailAddress(): ?string
274 5
    {
275
        return $this->newEmailAddress;
276 5
    }
277
278
    public function setNewEmailAddress(?string $newEmailAddress): self
279 1
    {
280
        $this->newEmailAddress = $newEmailAddress;
281 1
282
        return $this;
283 1
    }
284 1
285
    public function getNewEmailConfirmationToken(): ?string
286
    {
287
        return $this->newEmailConfirmationToken;
288 1
    }
289
290 1
    public function setNewEmailConfirmationToken(?string $newEmailConfirmationToken): self
291 1
    {
292 1
        $this->newEmailConfirmationToken = $newEmailConfirmationToken;
293 1
294 1
        return $this;
295 1
    }
296 1
297
    public function isEmailAddressVerified(): bool
298
    {
299
        return $this->emailAddressVerified;
300
    }
301
302
    public function setEmailAddressVerified(bool $emailAddressVerified): self
303 2
    {
304
        $this->emailAddressVerified = $emailAddressVerified;
305 2
306
        return $this;
307
    }
308 2
309 2
    public function getEmailAddressVerifyToken(): ?string
310 2
    {
311 2
        return $this->emailAddressVerifyToken;
312 2
    }
313 2
314 2
    public function setEmailAddressVerifyToken(?string $emailAddressVerifyToken): void
315
    {
316 2
        $this->emailAddressVerifyToken = $emailAddressVerifyToken;
317
    }
318
319
    public function isPasswordRequestLimitReached($ttl): bool
320
    {
321
        $lastRequest = $this->getPasswordRequestedAt();
322
323
        return $lastRequest instanceof DateTime &&
324
            $lastRequest->getTimestamp() + $ttl > time();
325
    }
326
327
    /** @see \Serializable::serialize() */
328
    public function serialize(): string
329
    {
330
        return serialize([
331 1
            (string) $this->id,
332
            $this->username,
333 1
            $this->emailAddress,
334 1
            $this->password,
335
            $this->enabled,
336 2
            $this->roles,
337
        ]);
338 2
    }
339
340
    /**
341
     * @see \Serializable::unserialize()
342
     */
343
    public function unserialize(string $serialized): self
344
    {
345
        $id = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
346
        [
347
            $id,
348
            $this->username,
349
            $this->emailAddress,
350
            $this->password,
351
            $this->enabled,
352
            $this->roles,
353
        ] = unserialize($serialized, ['allowed_classes' => false]);
354
        $this->id = Uuid::fromString($id);
355
356
        return $this;
357
    }
358
359
    /**
360
     * Not needed - we use bcrypt.
361
     *
362
     * @ApiProperty(readable=false, writable=false)
363
     */
364
    public function getSalt()
365
    {
366
    }
367
368
    /**
369
     * Remove sensitive data - e.g. plain passwords etc.
370
     */
371
    public function eraseCredentials(): void
372
    {
373
        $this->plainPassword = null;
374
    }
375
376
    public function __toString()
377
    {
378
        return (string) $this->id;
379
    }
380
381
    public static function createFromPayload($username, array $payload)
382
    {
383
        return new static(
384
            $username,
385
            $payload['roles']
386
        );
387
    }
388
}
389