Passed
Push — v2 ( f92e78...da15c4 )
by Daniel
11:34
created

AbstractUser::setOldPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Entity\User;
15
16
use ApiPlatform\Core\Annotation\ApiProperty;
17
use DateTime;
18
use Doctrine\ORM\Mapping as ORM;
19
use Silverback\ApiComponentBundle\Entity\Utility\IdTrait;
20
use Silverback\ApiComponentBundle\Validator\Constraints as APIAssert;
21
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
22
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
23
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
24
use Symfony\Component\Serializer\Annotation\Groups;
25
use Symfony\Component\Validator\Constraints as Assert;
26
27
/**
28
 * @ORM\MappedSuperclass(repositoryClass="Silverback\ApiComponentBundle\Repository\User\UserRepository")
29
 * @UniqueEntity(fields={"username"}, errorPath="username", message="Sorry, that user already exists in the database.")
30
 * @APIAssert\NewUsername(groups={"new_username", "Default"})
31
 */
32
abstract class AbstractUser implements SymfonyUserInterface
33
{
34
    use IdTrait;
35
36
    /**
37
     * @ORM\Column(type="string", length=255, unique=true)
38
     * @Assert\NotBlank(groups={"Default"})
39
     * @Groups({"admin"})
40
     */
41
    protected ?string $username;
42
43
    /**
44
     * @ORM\Column(type="string", length=255)
45
     * @ApiProperty(readable=false, writable=false)
46
     */
47
    protected string $password;
48
49
    /**
50
     * @ORM\Column(type="boolean")
51
     * @Groups({"super_admin"})
52
     */
53
    protected bool $enabled;
54
55
    /**
56
     * @ORM\Column(type="array")
57
     * @Groups({"super_admin"})
58
     */
59
    protected array $roles;
60
61
    /**
62
     * @ApiProperty(readable=false)
63
     * @Assert\NotBlank(message="Please enter your desired password", groups={"password_reset", "change_password"})
64
     * @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={"Default", "password_reset", "change_password"})
65
     * @Groups({"default_write"})
66
     */
67
    protected ?string $plainPassword = null;
68
69
    /**
70
     * Random string sent to the user email address in order to verify it.
71
     *
72
     * @ORM\Column(nullable=true)
73
     * @ApiProperty(readable=false, writable=false)
74
     */
75
    protected ?string $passwordResetConfirmationToken = null;
76
77
    /**
78
     * @ORM\Column(type="datetime", nullable=true)
79
     * @ApiProperty(readable=false, writable=false)
80
     */
81
    protected ?DateTime $passwordRequestedAt = null;
82
83
    /**
84
     * @ORM\Column(type="string", length=255, nullable=true)
85
     * @Assert\NotBlank(groups={"new_username"})
86
     * @Groups({"default", "new_username"})
87
     */
88
    protected ?string $newUsername = null;
89
90
    /**
91
     * Random string sent to the user's new email address in order to verify it.
92
     *
93
     * @ORM\Column(nullable=true)
94
     * @ApiProperty(readable=false, writable=false)
95
     */
96
    protected ?string $usernameConfirmationToken = null;
97
98
    /**
99
     * @ApiProperty(readable=false)
100
     * @UserPassword(message="You have not entered your current password correctly. Please try again.", groups={"change_password"})
101
     * @Groups({"default_write"})
102
     */
103
    protected ?string $oldPassword = null;
104
105
    /**
106
     * @ApiProperty(readable=false, writable=false)
107
     * @ORM\Column(type="datetime", nullable=true)
108
     */
109
    protected ?DateTime $passwordLastUpdated = null;
110
111
    public function __construct(
112
        string $username = '',
113
        array $roles = ['ROLE_USER'],
114
        string $password = '',
115
        bool $enabled = true
116
    ) {
117
        $this->username = $username;
118
        $this->roles = $roles;
119
        $this->password = $password;
120
        $this->enabled = $enabled;
121
        $this->setId();
122
    }
123
124
    public function getUsername(): ?string
125
    {
126
        return $this->username;
127
    }
128
129
    public function setUsername(?string $username): self
130
    {
131
        $this->username = $username;
132
133
        return $this;
134
    }
135
136
    public function getPassword(): ?string
137
    {
138
        return $this->password;
139
    }
140
141
    public function setPassword(string $password): self
142
    {
143
        $this->password = $password;
144
145
        return $this;
146
    }
147
148
    public function getRoles(): array
149
    {
150
        return $this->roles;
151
    }
152
153
    public function setRoles(?array $roles): self
154
    {
155
        $this->roles = $roles;
156
157
        return $this;
158
    }
159
160
    public function isEnabled(): bool
161
    {
162
        return $this->enabled;
163
    }
164
165
    public function setEnabled(bool $enabled): self
166
    {
167
        $this->enabled = $enabled;
168
169
        return $this;
170
    }
171
172
    public function getPlainPassword(): ?string
173
    {
174
        return $this->plainPassword;
175
    }
176
177
    public function setPlainPassword(?string $plainPassword): self
178
    {
179
        $this->plainPassword = $plainPassword;
180
        if ($plainPassword) {
181
            // Needs to update mapped field to trigger update event which will encode the plain password
182
            $this->passwordLastUpdated = new \DateTime();
183
        }
184
185
        return $this;
186
    }
187
188
    public function getPasswordResetConfirmationToken(): ?string
189
    {
190
        return $this->passwordResetConfirmationToken;
191
    }
192
193
    public function setPasswordResetConfirmationToken(?string $passwordResetConfirmationToken): self
194
    {
195
        $this->passwordResetConfirmationToken = $passwordResetConfirmationToken;
196
197
        return $this;
198
    }
199
200
    public function getPasswordRequestedAt(): ?DateTime
201
    {
202
        return $this->passwordRequestedAt;
203
    }
204
205
    public function setPasswordRequestedAt(?DateTime $passwordRequestedAt): self
206
    {
207
        $this->passwordRequestedAt = $passwordRequestedAt;
208
209
        return $this;
210
    }
211
212
    public function isPasswordRequestLimitReached($ttl): bool
213
    {
214
        $lastRequest = $this->getPasswordRequestedAt();
215
216
        return $lastRequest instanceof DateTime &&
217
            $lastRequest->getTimestamp() + $ttl > time();
218
    }
219
220
    public function getNewUsername(): ?string
221
    {
222
        return $this->newUsername;
223
    }
224
225
    public function setNewUsername(?string $newUsername): self
226
    {
227
        $this->newUsername = $newUsername;
228
229
        return $this;
230
    }
231
232
    public function getUsernameConfirmationToken(): ?string
233
    {
234
        return $this->usernameConfirmationToken;
235
    }
236
237
    public function setUsernameConfirmationToken(?string $usernameConfirmationToken): self
238
    {
239
        $this->usernameConfirmationToken = $usernameConfirmationToken;
240
241
        return $this;
242
    }
243
244
    public function getOldPassword(): ?string
245
    {
246
        return $this->oldPassword;
247
    }
248
249
    public function setOldPassword(?string $oldPassword): void
250
    {
251
        $this->oldPassword = $oldPassword;
252
    }
253
254
    public function getEmail(): string
255
    {
256
        return $this->username;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->username could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
257
    }
258
259
    /** @see \Serializable::serialize() */
260
    public function serialize(): string
261
    {
262
        return serialize([
263
            $this->id,
264
            $this->username,
265
            $this->password,
266
            $this->enabled,
267
        ]);
268
    }
269
270
    /**
271
     * @see \Serializable::unserialize()
272
     *
273
     * @param string $serialized
274
     */
275
    public function unserialize($serialized): void
276
    {
277
        [
278
            $this->id,
279
            $this->username,
280
            $this->password,
281
            $this->enabled
282
        ] = unserialize($serialized, ['allowed_classes' => false]);
283
    }
284
285
    /**
286
     * Not needed - we use bcrypt.
287
     *
288
     * @ApiProperty(readable=false, writable=false)
289
     */
290
    public function getSalt()
291
    {
292
    }
293
294
    /**
295
     * Remove sensitive data - e.g. plain passwords etc.
296
     */
297
    public function eraseCredentials(): void
298
    {
299
        $this->plainPassword = null;
300
    }
301
302
    public function __toString()
303
    {
304
        return (string) $this->id;
305
    }
306
}
307