User::setFirstname()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
namespace App\Entity;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Gedmo\Mapping\Annotation as Gedmo;
10
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
11
use JMS\Serializer\Annotation as Serializer;
12
use Ramsey\Uuid\Uuid;
13
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
14
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
15
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
#[ORM\Entity]
20
#[ORM\InheritanceType('SINGLE_TABLE')]
21
#[ORM\DiscriminatorColumn(name: 'class', type: 'string')]
22
#[ORM\DiscriminatorMap(['user' => 'User', 'ad' => 'ActiveDirectoryUser'])]
23
#[UniqueEntity(fields: ['username'])]
24
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
25
#[Serializer\Discriminator(disabled: true)]
26
class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface {
27
28
    use IdTrait;
29
    use UuidTrait;
30
    use SoftDeleteableEntity;
31
32
    #[ORM\Column(type: 'string', unique: true)]
33
    #[Assert\NotBlank]
34
    #[Assert\Email(mode: 'html5')]
35
    #[Assert\Length(min: 4, max: 128)]
36
    private $username;
37
38
    #[ORM\Column(type: 'string', nullable: true)]
39
    #[Assert\NotBlank(allowNull: true)]
40
    private ?string $firstname = null;
41
42
    #[ORM\Column(type: 'string', nullable: true)]
43
    #[Assert\NotBlank(allowNull: true)]
44
    private ?string $lastname = null;
45
46
    #[ORM\Column(type: 'string', length: 62, nullable: true)]
47
    #[Serializer\Exclude]
48
    private $password;
49
50
    #[ORM\Column(type: 'string', nullable: true)]
51
    #[Assert\NotBlank(allowNull: true)]
52
    #[Assert\Length(max: 191)]
53
    #[Assert\Email]
54
    private ?string $email = null;
55
56
    #[ORM\Column(type: 'string', nullable: true)]
57
    private ?string $grade = null;
58
59
    #[ORM\ManyToOne(targetEntity: UserType::class, inversedBy: 'users')]
60
    #[ORM\JoinColumn(onDelete: 'SET NULL')]
61
    #[Assert\NotNull]
62
    #[Serializer\ReadOnlyProperty]
63
    #[Serializer\Accessor(getter: 'getTypeString')]
64
    #[Serializer\Type('string')]
65
    private ?UserType $type = null;
66
67
    #[ORM\Column(type: 'json')]
68
    #[Serializer\Exclude]
69
    private array $roles = [ 'ROLE_USER' ];
70
71
    #[ORM\Column(type: 'string', nullable: true)]
72
    private $externalId;
73
74
    #[ORM\Column(type: 'boolean')]
75
    private bool $isActive = true;
76
77
    #[ORM\Column(type: 'boolean')]
78
    #[Serializer\Exclude]
79
    private bool $isEmailConfirmationPending = false;
80
81
    #[ORM\JoinTable]
82
    #[ORM\JoinColumn(onDelete: 'CASCADE')]
83
    #[ORM\InverseJoinColumn(onDelete: 'CASCADE')]
84
    #[ORM\ManyToMany(targetEntity: ServiceProvider::class)]
85
    #[Serializer\Exclude]
86
    private $enabledServices;
87
88
    #[ORM\OneToMany(targetEntity: ServiceAttributeValue::class, mappedBy: 'user')]
89
    #[Serializer\Exclude]
90
    private $attributes;
91
92
    #[ORM\ManyToMany(targetEntity: UserRole::class, inversedBy: 'users')]
93
    #[Serializer\Exclude]
94
    private $userRoles;
95
96
    #[ORM\Column(type: 'string', nullable: true)]
97
    #[Serializer\Exclude]
98
    private $googleAuthenticatorSecret;
99
100
    #[ORM\Column(type: 'json')]
101
    #[Serializer\Exclude]
102
    private array $backupCodes = [ ];
103
104
    #[ORM\Column(name: 'trusted_version', type: 'integer')]
105
    #[Serializer\Exclude]
106
    private int $trustedVersion = 0;
107
108
    #[ORM\Column(type: 'datetime')]
109
    #[Gedmo\Timestampable(on: 'create')]
110
    #[Serializer\Exclude]
111
    private $createdAt;
112
113
    #[ORM\Column(type: 'datetime', nullable: true)]
114
    #[Gedmo\Timestampable(on: 'update', field: ['firstname', 'lastname', 'email', 'type', 'userRoles'])]
115
    #[Serializer\Exclude]
116
    private $updatedAt;
117
118
    #[ORM\Column(type: 'datetime', nullable: true)]
119
    private $enabledFrom;
120
121
    #[ORM\Column(type: 'datetime', nullable: true)]
122
    private $enabledUntil;
123
124
    #[ORM\Column(type: 'json')]
125
    #[Serializer\Exclude]
126
    private array $data = [ ];
127
128
    #[ORM\Column(type: 'datetime', nullable: true)]
129
    private ?DateTime $privacyPolicyConfirmedAt = null;
130
131
    #[ORM\Column(type: 'boolean')]
132
    private bool $isProvisioned = true;
133
134
    #[ORM\Column(type: 'boolean')]
135
    private bool $mustChangePassword = false;
136
137
    #[ORM\Column(type: 'boolean', options: ['default' => 1])]
138
    private bool $canChangePassword = true;
139
140
    /**
141
     * @var Collection<User>
142
     */
143
    #[ORM\JoinTable(name: 'user_links')]
144
    #[ORM\JoinColumn(name: 'source_user_id', onDelete: 'CASCADE')]
145
    #[ORM\InverseJoinColumn(name: 'target_user_id', onDelete: 'CASCADE')]
146
    #[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'parents')]
147
    private $linkedStudents;
148
149
    /**
150
     * @var Collection<User>
151
     */
152
    #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'linkedStudents')]
153
    private $parents;
154
155
    public function __construct() {
156
        $this->uuid = Uuid::uuid4();
157
158
        $this->enabledServices = new ArrayCollection();
159
        $this->attributes = new ArrayCollection();
160
        $this->userRoles = new ArrayCollection();
161
        $this->linkedStudents = new ArrayCollection();
162
        $this->parents = new ArrayCollection();
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getUsername() {
169
        return $this->username;
170
    }
171
172
    public function getUserIdentifier(): string {
173
        return $this->getUsername();
174
    }
175
176
    /**
177
     * @param string $username
178
     * @return User
179
     */
180
    public function setUsername($username) {
181
        $this->username = mb_strtolower($username);
182
        return $this;
183
    }
184
185
    /**
186
     * @return string|null
187
     */
188
    public function getFirstname() {
189
        return $this->firstname;
190
    }
191
192
    /**
193
     * @param string|null $firstname
194
     * @return User
195
     */
196
    public function setFirstname($firstname) {
197
        $this->firstname = $firstname;
198
        return $this;
199
    }
200
201
    /**
202
     * @return string|null
203
     */
204
    public function getLastname() {
205
        return $this->lastname;
206
    }
207
208
    /**
209
     * @param string|null $lastname
210
     * @return User
211
     */
212
    public function setLastname($lastname) {
213
        $this->lastname = $lastname;
214
        return $this;
215
    }
216
217
    /**
218 22
     * @param string $password
219 22
     * @return User
220
     */
221 22
    public function setPassword($password) {
222 22
        $this->password = $password;
223 22
        return $this;
224 22
    }
225 22
226
    /**
227
     * @param string|null $email
228
     * @return User
229
     */
230 9
    public function setEmail($email) {
231 9
        $this->email = $email;
232
        return $this;
233
    }
234
235
    /**
236
     * @return string|null
237
     */
238 13
    public function getEmail() {
239 13
        return $this->email;
240 13
    }
241
242
    /**
243
     * @return string|null
244
     */
245
    public function getGrade() {
246 5
        return $this->grade;
247 5
    }
248
249
    /**
250
     * @param string $grade
251
     * @return User
252
     */
253
    public function setGrade($grade) {
254 8
        $this->grade = $grade;
255 8
        return $this;
256 8
    }
257
258
    /**
259
     * @return UserType
260
     */
261
    public function getType() {
262 5
        return $this->type;
263 5
    }
264
265
    /**
266
     * @return User
267
     */
268
    public function setType(UserType $userType) {
269
        $this->type = $userType;
270 8
        return $this;
271 8
    }
272 8
273
    /**
274
     * @return string|null
275
     */
276
    public function getExternalId() {
277
        if($this->linkedStudents->count() > 0) {
278
            return implode(',', $this->linkedStudents->map(fn(User $user) => $user->getEmail())->toArray());
279 12
        }
280 12
281 12
        return $this->externalId;
282
    }
283
284
    /**
285
     * @param string|null $externalId
286
     * @return User
287
     */
288 8
    public function setExternalId($externalId) {
289 8
        $this->externalId = $externalId;
290 8
        return $this;
291
    }
292
293
    /**
294
     * @return bool
295
     */
296 1
    public function isActive() {
297 1
        return $this->isActive;
298
    }
299
300
    /**
301
     * @param bool $active
302
     * @return User
303
     */
304
    public function setIsActive($active) {
305
        $this->isActive = $active;
306
        return $this;
307
    }
308
309
    public function isEmailConfirmationPending(): bool {
310
        return $this->isEmailConfirmationPending;
311
    }
312
313
    public function setIsEmailConfirmationPending(bool $isEmailConfirmationPending): User {
314
        $this->isEmailConfirmationPending = $isEmailConfirmationPending;
315
        return $this;
316
    }
317
318
    public function addEnabledService(ServiceProvider $serviceProvider) {
319 8
        $this->enabledServices->add($serviceProvider);
320 8
    }
321
322
    public function removeEnabledService(ServiceProvider $serviceProvider) {
323
        $this->enabledServices->removeElement($serviceProvider);
324
    }
325
326
    public function getEnabledServices(): Collection {
327 12
        return $this->enabledServices;
328 12
    }
329 12
330
    public function getAttributes(): Collection {
331
        return $this->attributes;
332
    }
333
334
    public function getUserRoles(): Collection {
335 7
        return $this->userRoles;
336 7
    }
337
338
    public function addUserRole(UserRole $role) {
339
        $this->userRoles->add($role);
340
    }
341
342
    public function removeUserRole(UserRole $role) {
343 2
        $this->userRoles->removeElement($role);
344 2
    }
345 2
346
    /**
347
     * @return string[]
348
     */
349
    public function getRoles(): array {
350
        return $this->roles;
351 13
    }
352 13
353
    /**
354
     * @param string[] $roles
355
     * @return User
356
     */
357
    public function setRoles(array $roles) {
358
        $this->roles = $roles;
359 13
        return $this;
360 13
    }
361 13
362
    public function getPassword(): string {
363
        return $this->password ?? '';
364
    }
365
366
    /**
367 12
     * @return null
368 12
     */
369
    public function getSalt() {
370
        return null;
371
    }
372
373
    /**
374
     * @return null
375
     */
376
    public function eraseCredentials() {
377
        return null;
378
    }
379
380
    /**
381
     * @inheritDoc
382
     */
383
    public function getGoogleAuthenticatorSecret(): string {
384
        if($this->googleAuthenticatorSecret === null) {
385
            return ''; // dirty hack
386
        }
387
388
        return $this->googleAuthenticatorSecret;
389
    }
390
391
    /**
392
     * @inheritDoc
393
     */
394
    public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void {
395
        $this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
396
    }
397 6
398 6
    /**
399
     * @return string[]
400
     */
401
    public function getBackupCodes() {
402
        return $this->backupCodes;
403
    }
404
405
    public function emptyBackupCodes() {
406
        $this->backupCodes = [ ];
407
    }
408
409
    /**
410
     * @param string[] $backupCodes
411 6
     * @return User
412 6
     */
413
    public function setBackupCodes(array $backupCodes) {
414
        $this->backupCodes = $backupCodes;
415
        return $this;
416
    }
417
418
    /**
419
     * @inheritDoc
420
     */
421
    public function isBackupCode(string $code): bool {
422
        return in_array($code, $this->backupCodes);
423
    }
424
425
    /**
426
     * @inheritDoc
427
     */
428
    public function invalidateBackupCode(string $code): void {
429
        $key = array_search($code, $this->backupCodes);
430
        if ($key !== false){
431
            unset($this->backupCodes[$key]);
432 8
        }
433 8
    }
434
435
    public function isGoogleAuthenticatorEnabled(): bool {
436
        return $this->googleAuthenticatorSecret !== null;
437
    }
438
439
    public function getGoogleAuthenticatorUsername(): string {
440
        return $this->getUsername();
441
    }
442
443
    public function getTrustedTokenVersion(): int {
444
        return $this->trustedVersion;
445
    }
446
447
    public function getCreatedAt(): DateTime {
448 10
        return $this->createdAt;
449 10
    }
450
451
    public function getUpdatedAt(): ?DateTime {
452
        return $this->updatedAt;
453
    }
454
455 9
    public function getEnabledFrom(): ?DateTime {
456 9
        return $this->enabledFrom;
457
    }
458
459
    public function setEnabledFrom(?DateTime $enabledFrom): User {
460
        $this->enabledFrom = $enabledFrom;
461
        return $this;
462 7
    }
463 7
464
    public function getEnabledUntil(): ?DateTime {
465
        return $this->enabledUntil;
466
    }
467
468
    public function setEnabledUntil(?DateTime$enabledUntil): User {
469 2
        $this->enabledUntil = $enabledUntil;
470 2
        return $this;
471
    }
472
473
    public function getData(string $key, $default = null) {
474 2
        return $this->data[$key] ?? $default;
475
    }
476
477
    public function setData(string $key, $value): void {
478
        $this->data[$key] = $value;
479
    }
480 3
481 3
    public function getPrivacyPolicyConfirmedAt(): ?DateTime {
482 3
        return $this->privacyPolicyConfirmedAt;
483
    }
484
485
    public function setPrivacyPolicyConfirmedAt(?DateTime $privacyPolicyConfirmedAt): User {
486
        $this->privacyPolicyConfirmedAt = $privacyPolicyConfirmedAt;
487
        return $this;
488
    }
489
490
    public function isProvisioned(): bool {
491
        return $this->isProvisioned;
492
    }
493
494
    public function setIsProvisioned(bool $isProvisioned): User {
495
        $this->isProvisioned = $isProvisioned;
496
        return $this;
497
    }
498
499
    public function isMustChangePassword(): bool {
500
        return $this->mustChangePassword;
501
    }
502
503
    public function setMustChangePassword(bool $mustChangePassword): User {
504
        $this->mustChangePassword = $mustChangePassword;
505
        return $this;
506
    }
507
508
    public function canChangePassword(): bool {
509
        return $this->canChangePassword;
510
    }
511
512
    public function setCanChangePassword(bool $canChangePassword): User {
513
        $this->canChangePassword = $canChangePassword;
514
        return $this;
515
    }
516
517
    public function addLinkedStudent(User $user): void {
518
        $this->linkedStudents->add($user);
519
    }
520
521
    public function removeLinkedStudent(User $user): void {
522
        $this->linkedStudents->removeElement($user);
523
    }
524 8
525 8
    public function getLinkedStudents(): Collection {
526
        return $this->linkedStudents;
527
    }
528
529
    public function getParents(): Collection {
530
        return $this->parents;
531
    }
532
533
    public function getTypeString(): string {
534
        return (string)$this->getType()->getUuid();
535
    }
536
}