Completed
Push — master ( 8f2b16...c80f65 )
by Marcel
03:24
created

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