Completed
Push — master ( eaf36c...f92548 )
by Marcel
03:19
created

User::isProvisioned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
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 R\U2FTwoFactorBundle\Model\U2F\TwoFactorInterface as U2FTwoFactorInterface;
13
use Ramsey\Uuid\Uuid;
14
use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
15
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface as GoogleTwoFactorInterface;
16
use Scheb\TwoFactorBundle\Model\PreferredProviderInterface;
17
use Scheb\TwoFactorBundle\Model\TrustedDeviceInterface;
18
use Swagger\Annotations as SWG;
19
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
use Symfony\Component\Validator\Constraints as Assert;
22
23
/**
24
 * @ORM\Entity()
25
 * @ORM\InheritanceType("SINGLE_TABLE")
26
 * @ORM\DiscriminatorColumn(name="class", type="string")
27
 * @ORM\DiscriminatorMap({"user" = "User", "ad" = "ActiveDirectoryUser"})
28
 * @UniqueEntity(fields={"username"})
29
 * @Serializer\Discriminator(disabled=true)
30
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=true)
31
 */
32
class User implements UserInterface, GoogleTwoFactorInterface, TrustedDeviceInterface, BackupCodeInterface, U2FTwoFactorInterface, PreferredProviderInterface {
33
34
    use IdTrait;
35
    use UuidTrait;
36
    use SoftDeleteableEntity;
37
38
    /**
39
     * @ORM\Column(type="string", unique=true)
40
     * @Assert\NotBlank()
41
     * @Assert\Email()
42
     * @ORM\OrderBy()
43
     * @Assert\Length(max="128", min="4")
44
     */
45
    private $username;
46
47
    /**
48
     * @ORM\Column(type="string", nullable=true)
49
     * @Assert\NotBlank(allowNull=true)
50
     * @var string|null
51
     */
52
    private $firstname;
53
54
    /**
55
     * @ORM\Column(type="string", nullable=true)
56
     * @Assert\NotBlank(allowNull=true)
57
     * @var string|null
58
     */
59
    private $lastname;
60
61
    /**
62
     * @ORM\Column(type="string", length=62, nullable=true)
63
     * @Serializer\Exclude()
64
     */
65
    private $password;
66
67
    /**
68
     * @ORM\Column(type="string", nullable=true)
69
     * @Assert\NotBlank(allowNull=true)
70
     * @Assert\Length(max="191")
71
     * @Assert\Email()
72
     * @var string|null
73
     */
74
    private $email;
75
76
    /**
77
     * @ORM\Column(type="string", nullable=true)
78
     * @var string|null
79
     */
80
    private $grade;
81
82
    /**
83
     * @ORM\ManyToOne(targetEntity="UserType", inversedBy="users")
84
     * @ORM\JoinColumn(onDelete="SET NULL")
85
     * @Serializer\ReadOnly()
86
     * @Serializer\Accessor(getter="getTypeString")
87
     * @Serializer\Type("string")
88
     * @SWG\Property(description="UUID of the usertype")
89
     * @Assert\NotNull()
90
     * @var UserType|null
91
     */
92
    private $type;
93
94
    /**
95
     * @ORM\Column(type="json")
96
     * @Serializer\Exclude()
97
     */
98
    private $roles = [ 'ROLE_USER' ];
99
100
    /**
101
     * @ORM\Column(type="string", nullable=true)
102
     */
103
    private $externalId;
104
105
    /**
106
     * @ORM\Column(type="boolean")
107
     */
108
    private $isActive = true;
109
110
    /**
111
     * @ORM\Column(type="boolean")
112
     * @Serializer\Exclude()
113
     * @var bool
114
     */
115
    private $isEmailConfirmationPending = false;
116
117
    /**
118
     * @ORM\ManyToMany(targetEntity="ServiceProvider")
119
     * @ORM\JoinTable(
120
     *  joinColumns={@ORM\JoinColumn(onDelete="CASCADE")},
121
     *  inverseJoinColumns={@ORM\JoinColumn(onDelete="CASCADE")}
122
     * )
123
     * @Serializer\Exclude()
124
     */
125
    private $enabledServices;
126
127
    /**
128
     * @ORM\OneToMany(targetEntity="ServiceAttributeValue", mappedBy="user")
129
     * @Serializer\Exclude()
130
     */
131
    private $attributes;
132
133
    /**
134
     * @ORM\ManyToMany(targetEntity="UserRole", inversedBy="users")
135
     * @Serializer\Exclude()
136
     */
137
    private $userRoles;
138
139
    /**
140
     * @ORM\Column(type="string", nullable=true)
141
     * @Serializer\Exclude()
142
     */
143
    private $googleAuthenticatorSecret;
144
145
    /**
146
     * @ORM\Column(type="json")
147
     * @Serializer\Exclude()
148
     */
149
    private $backupCodes = [ ];
150
151
    /**
152
     * @ORM\Column(type="integer", name="trusted_version")
153
     * @Serializer\Exclude()
154
     */
155
    private $trustedVersion = 0;
156
157
    /**
158
     * @ORM\Column(type="datetime")
159
     * @Gedmo\Timestampable(on="create")
160
     * @Serializer\Exclude()
161
     */
162
    private $createdAt;
163
164
    /**
165
     * @ORM\Column(type="datetime", nullable=true)
166
     * @Gedmo\Timestampable(on="update", field={"firstname", "lastname", "email", "type", "userRoles"})
167
     * @Serializer\Exclude()
168
     */
169
    private $updatedAt;
170
171
    /**
172
     * @ORM\OneToMany(targetEntity="U2fKey", mappedBy="user")
173
     * @Serializer\Exclude()
174
     */
175
    private $u2fKeys;
176
177
    /**
178
     * @ORM\Column(type="datetime", nullable=true)
179
     */
180
    private $enabledFrom;
181
182
    /**
183
     * @ORM\Column(type="datetime", nullable=true)
184
     */
185
    private $enabledUntil;
186
187
    /**
188
     * @ORM\Column(type="json")
189
     * @Serializer\Exclude()
190
     * @var array
191
     */
192
    private $data = [ ];
193
194
    /**
195
     * @ORM\Column(type="datetime", nullable=true)
196
     * @var DateTime|null
197
     */
198
    private $privacyPolicyConfirmedAt = null;
199
200
    /**
201
     * @ORM\Column(type="boolean")
202
     * @var bool
203
     */
204
    private $isProvisioned = true;
205
206
    /**
207
     * @ORM\Column(type="boolean")
208
     * @var bool
209
     */
210
    private $mustChangePassword = false;
211
212 15
    public function __construct() {
213 15
        $this->uuid = Uuid::uuid4();
214
215 15
        $this->enabledServices = new ArrayCollection();
216 15
        $this->attributes = new ArrayCollection();
217 15
        $this->userRoles = new ArrayCollection();
218 15
        $this->u2fKeys = new ArrayCollection();
219 15
    }
220
221
    /**
222
     * @return string
223
     */
224 6
    public function getUsername() {
225 6
        return $this->username;
226
    }
227
228
    /**
229
     * @param string $username
230
     * @return User
231
     */
232 6
    public function setUsername($username) {
233 6
        $this->username = $username;
234 6
        return $this;
235
    }
236
237
    /**
238
     * @return string|null
239
     */
240 4
    public function getFirstname() {
241 4
        return $this->firstname;
242
    }
243
244
    /**
245
     * @param string|null $firstname
246
     * @return User
247
     */
248 6
    public function setFirstname($firstname) {
249 6
        $this->firstname = $firstname;
250 6
        return $this;
251
    }
252
253
    /**
254
     * @return string|null
255
     */
256 4
    public function getLastname() {
257 4
        return $this->lastname;
258
    }
259
260
    /**
261
     * @param string|null $lastname
262
     * @return User
263
     */
264 6
    public function setLastname($lastname) {
265 6
        $this->lastname = $lastname;
266 6
        return $this;
267
    }
268
269
    /**
270
     * @param string $password
271
     * @return User
272
     */
273 7
    public function setPassword($password) {
274 7
        $this->password = $password;
275 7
        return $this;
276
    }
277
278
    /**
279
     * @param string|null $email
280
     * @return User
281
     */
282 6
    public function setEmail($email) {
283 6
        $this->email = $email;
284 6
        return $this;
285
    }
286
287
    /**
288
     * @return string|null
289
     */
290 2
    public function getEmail() {
291 2
        return $this->email;
292
    }
293
294
    /**
295
     * @return string|null
296
     */
297
    public function getGrade() {
298
        return $this->grade;
299
    }
300
301
    /**
302
     * @param string $grade
303
     * @return User
304
     */
305
    public function setGrade($grade) {
306
        $this->grade = $grade;
307
        return $this;
308
    }
309
310
    /**
311
     * @return UserType
312
     */
313 4
    public function getType() {
314 4
        return $this->type;
315
    }
316
317
    /**
318
     * @param UserType $userType
319
     * @return User
320
     */
321 5
    public function setType(UserType $userType) {
322 5
        $this->type = $userType;
323 5
        return $this;
324
    }
325
326
    /**
327
     * @return string|int
328
     */
329 4
    public function getExternalId() {
330 4
        return $this->externalId;
331
    }
332
333
    /**
334
     * @param string|int $externalId
335
     * @return User
336
     */
337
    public function setExternalId($externalId) {
338
        $this->externalId = $externalId;
339
        return $this;
340
    }
341
342
    /**
343
     * @return bool
344
     */
345 10
    public function isActive() {
346 10
        return $this->isActive;
347
    }
348
349
    /**
350
     * @param bool $active
351
     * @return User
352
     */
353 11
    public function setIsActive($active) {
354 11
        $this->isActive = $active;
355 11
        return $this;
356
    }
357
358
    /**
359
     * @return bool
360
     */
361 9
    public function isEmailConfirmationPending(): bool {
362 9
        return $this->isEmailConfirmationPending;
363
    }
364
365
    /**
366
     * @param bool $isEmailConfirmationPending
367
     * @return User
368
     */
369
    public function setIsEmailConfirmationPending(bool $isEmailConfirmationPending): User {
370
        $this->isEmailConfirmationPending = $isEmailConfirmationPending;
371
        return $this;
372
    }
373
374
    /**
375
     * @param ServiceProvider $serviceProvider
376
     */
377
    public function addEnabledService(ServiceProvider $serviceProvider) {
378
        $this->enabledServices->add($serviceProvider);
379
    }
380
381
    /**
382
     * @param ServiceProvider $serviceProvider
383
     */
384
    public function removeEnabledService(ServiceProvider $serviceProvider) {
385
        $this->enabledServices->removeElement($serviceProvider);
386
    }
387
388
    /**
389
     * @return Collection
390
     */
391 4
    public function getEnabledServices(): Collection {
392 4
        return $this->enabledServices;
393
    }
394
395
    /**
396
     * @return Collection
397
     */
398 1
    public function getAttributes(): Collection {
399 1
        return $this->attributes;
400
    }
401
402
    /**
403
     * @return Collection
404
     */
405 4
    public function getUserRoles(): Collection {
406 4
        return $this->userRoles;
407
    }
408
409
    /**
410
     * @param UserRole $role
411
     */
412
    public function addUserRole(UserRole $role) {
413
        $this->userRoles->add($role);
414
    }
415
416
    /**
417
     * @param UserRole $role
418
     */
419
    public function removeUserRole(UserRole $role) {
420
        $this->userRoles->removeElement($role);
421
    }
422
423
    /**
424
     * @return string[]
425
     */
426 5
    public function getRoles() {
427 5
        return $this->roles;
428
    }
429
430
    /**
431
     * @param string[] $roles
432
     * @return User
433
     */
434
    public function setRoles(array $roles) {
435
        $this->roles = $roles;
436
        return $this;
437
    }
438
439
    /**
440
     * @return string
441
     */
442 7
    public function getPassword() {
443 7
        return $this->password;
444
    }
445
446
    /**
447
     * @return null
448
     */
449 6
    public function getSalt() {
450 6
        return null;
451
    }
452
453
    /**
454
     * @return null
455
     */
456 4
    public function eraseCredentials() {
457 4
        return null;
458
    }
459
460
    /**
461
     * @inheritDoc
462
     */
463 2
    public function getGoogleAuthenticatorSecret(): string {
464 2
        if($this->googleAuthenticatorSecret === null) {
465
            return ''; // dirty hack
466
        }
467
468 2
        return $this->googleAuthenticatorSecret;
469
    }
470
471
    /**
472
     * @inheritDoc
473
     */
474 3
    public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void {
475 3
        $this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
476 3
    }
477
478
    /**
479
     * @return string[]
480
     */
481
    public function getBackupCodes() {
482
        return $this->backupCodes;
483
    }
484
485
    public function emptyBackupCodes() {
486
        $this->backupCodes = [ ];
487
    }
488
489
    /**
490
     * @param string[] $backupCodes
491
     * @return User
492
     */
493
    public function setBackupCodes(array $backupCodes) {
494
        $this->backupCodes = $backupCodes;
495
        return $this;
496
    }
497
498
    /**
499
     * @inheritDoc
500
     */
501
    public function isBackupCode(string $code): bool {
502
        return in_array($code, $this->backupCodes);
503
    }
504
505
    /**
506
     * @inheritDoc
507
     */
508
    public function invalidateBackupCode(string $code): void {
509
        $key = array_search($code, $this->backupCodes);
510
        if ($key !== false){
511
            unset($this->backupCodes[$key]);
512
        }
513
    }
514
515
    /**
516
     * @return bool
517
     */
518 5
    public function isGoogleAuthenticatorEnabled(): bool {
519 5
        return $this->googleAuthenticatorSecret !== null;
520
    }
521
522
    /**
523
     * @return string
524
     */
525
    public function getGoogleAuthenticatorUsername(): string {
526
        return $this->getUsername();
527
    }
528
529
    /**
530
     * @return int
531
     */
532 5
    public function getTrustedTokenVersion(): int {
533 5
        return $this->trustedVersion;
534
    }
535
536
    /**
537
     * @return \DateTime
538
     */
539
    public function getCreatedAt(): \DateTime {
540
        return $this->createdAt;
541
    }
542
543
    /**
544
     * @return \DateTime|null
545
     */
546
    public function getUpdatedAt(): ?\DateTime {
547
        return $this->updatedAt;
548
    }
549
550
    /**
551
     * @inheritDoc
552
     */
553 5
    public function isU2FAuthEnabled(): bool {
554 5
        return count($this->u2fKeys) > 0;
555
    }
556
557
    /**
558
     * @return Collection
559
     */
560
    public function getU2FKeys(): Collection {
561
        return $this->u2fKeys;
562
    }
563
564
    /**
565
     * @inheritDoc
566
     */
567
    public function addU2FKey($key): void {
568
        $this->u2fKeys->add($key);
569
    }
570
571
    /**
572
     * @inheritDoc
573
     */
574
    public function removeU2FKey($key): void {
575
        $this->u2fKeys->removeElement($key);
576
    }
577
578
    /**
579
     * @inheritDoc
580
     */
581 2
    public function getPreferredTwoFactorProvider(): ?string {
582 2
        if($this->isU2FAuthEnabled()) {
583
            return 'u2f_two_factor';
584 2
        } else if($this->isGoogleAuthenticatorEnabled()) {
585 2
            return 'google';
586
        }
587
588
        return null;
589
    }
590
591
    /**
592
     * @return \DateTime|null
593
     */
594 9
    public function getEnabledFrom(): ?\DateTime {
595 9
        return $this->enabledFrom;
596
    }
597
598
    /**
599
     * @param \DateTime|null $enabledFrom
600
     * @return User
601
     */
602 5
    public function setEnabledFrom(?\DateTime $enabledFrom): User {
603 5
        $this->enabledFrom = $enabledFrom;
604 5
        return $this;
605
    }
606
607
    /**
608
     * @return \DateTime|null
609
     */
610 8
    public function getEnabledUntil(): ?\DateTime {
611 8
        return $this->enabledUntil;
612
    }
613
614
    /**
615
     * @param \DateTime|null $enabledUntil
616
     * @return User
617
     */
618 5
    public function setEnabledUntil(?\DateTime$enabledUntil): User {
619 5
        $this->enabledUntil = $enabledUntil;
620 5
        return $this;
621
    }
622
623 4
    public function getData(string $key, $default = null) {
624 4
        return $this->data[$key] ?? $default;
625
    }
626
627
    public function setData(string $key, $value): void {
628
        $this->data[$key] = $value;
629
    }
630
631
    /**
632
     * @return DateTime|null
633
     */
634 5
    public function getPrivacyPolicyConfirmedAt(): ?DateTime {
635 5
        return $this->privacyPolicyConfirmedAt;
636
    }
637
638
    /**
639
     * @param DateTime|null $privacyPolicyConfirmedAt
640
     * @return User
641
     */
642
    public function setPrivacyPolicyConfirmedAt(?DateTime $privacyPolicyConfirmedAt): User {
643
        $this->privacyPolicyConfirmedAt = $privacyPolicyConfirmedAt;
644
        return $this;
645
    }
646
647
    /**
648
     * @return bool
649
     */
650 5
    public function isProvisioned(): bool {
651 5
        return $this->isProvisioned;
652
    }
653
654
    /**
655
     * @param bool $isProvisioned
656
     * @return User
657
     */
658
    public function setIsProvisioned(bool $isProvisioned): User {
659
        $this->isProvisioned = $isProvisioned;
660
        return $this;
661
    }
662
663
    /**
664
     * @return bool
665
     */
666 5
    public function isMustChangePassword(): bool {
667 5
        return $this->mustChangePassword;
668
    }
669
670
    /**
671
     * @param bool $mustChangePassword
672
     * @return User
673
     */
674 2
    public function setMustChangePassword(bool $mustChangePassword): User {
675 2
        $this->mustChangePassword = $mustChangePassword;
676 2
        return $this;
677
    }
678
679
    public function getTypeString(): string {
680
        return (string)$this->getType()->getUuid();
681
    }
682
}