Failed Conditions
Push — master ( 2c5639...f04b0f )
by Adrien
08:44
created

User::getOwnerForCreation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\DBAL\Types\BillingTypeType;
8
use Application\DBAL\Types\RelationshipType;
9
use Application\Repository\LogRepository;
10
use Application\Repository\UserRepository;
11
use Application\Traits\HasAddress;
12
use Application\Traits\HasDoorAccess;
13
use Application\Traits\HasIban;
14
use Application\Traits\HasRemarks;
15
use Cake\Chronos\Chronos;
16
use Cake\Chronos\Date;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Doctrine\Common\Collections\Collection;
19
use Doctrine\ORM\Mapping as ORM;
20
use Ecodev\Felix\Api\Exception;
21
use Ecodev\Felix\Model\CurrentUser;
22
use Ecodev\Felix\Model\Traits\HasInternalRemarks;
23
use Ecodev\Felix\Model\Traits\HasPassword;
24
use GraphQL\Doctrine\Annotation as API;
25
26
/**
27
 * User
28
 *
29
 * @ORM\Entity(repositoryClass="Application\Repository\UserRepository")
30
 * @ORM\HasLifecycleCallbacks
31
 * @ORM\AssociationOverrides({
32
 *     @ORM\AssociationOverride(name="owner", inversedBy="users")
33
 * })
34
 * @API\Sorting({
35
 *     "Application\Api\Input\Sorting\Age",
36
 *     "Application\Api\Input\Sorting\Balance",
37
 * })
38
 * @API\Filters({
39
 *     @API\Filter(field="custom", operator="Application\Api\Input\Operator\HasBookingWithTaggedBookableOperatorType", type="id"),
40
 *     @API\Filter(field="custom", operator="Application\Api\Input\Operator\HasBookingWithBookableOperatorType", type="id"),
41
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\EqualOperatorType", type="Money"),
42
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\GreaterOperatorType", type="Money"),
43
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\GreaterOrEqualOperatorType", type="Money"),
44
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\LessOperatorType", type="Money"),
45
 *     @API\Filter(field="balance", operator="Application\Api\Input\Operator\AccountBalance\LessOrEqualOperatorType", type="Money"),
46
 * })
47
 */
48
class User extends AbstractModel implements \Ecodev\Felix\Model\User
49
{
50
    const ROLE_ANONYMOUS = 'anonymous';
51
    const ROLE_BOOKING_ONLY = 'booking_only';
52
    const ROLE_INDIVIDUAL = 'individual';
53
    const ROLE_MEMBER = 'member';
54
    const ROLE_TRAINER = 'trainer';
55
    const ROLE_RESPONSIBLE = 'responsible';
56
    const ROLE_ADMINISTRATOR = 'administrator';
57
58
    const STATUS_INACTIVE = 'inactive';
59
    const STATUS_NEW = 'new';
60
    const STATUS_ACTIVE = 'active';
61
    const STATUS_ARCHIVED = 'archived';
62
63
    use HasDoorAccess;
64
    use HasRemarks;
65
    use HasInternalRemarks;
66
    use HasAddress;
67
    use HasIban;
68
    use HasPassword;
69
70
    /**
71
     * @var User
72
     */
73
    private static $currentUser;
74
75
    /**
76
     * Set currently logged in user
77
     * WARNING: this method should only be called from \Application\Authentication\AuthenticationListener
78
     *
79
     * @param \Application\Model\User $user
80
     */
81 195
    public static function setCurrent(?self $user): void
82
    {
83 195
        self::$currentUser = $user;
84
85
        // Initalize ACL filter with current user if a logged in one exists
86
        /** @var UserRepository $userRepository */
87 195
        $userRepository = _em()->getRepository(self::class);
88 195
        $aclFilter = $userRepository->getAclFilter();
89 195
        $aclFilter->setUser($user);
90
91 195
        CurrentUser::set($user);
92 195
    }
93
94
    /**
95
     * Returns currently logged user or null
96
     */
97 72
    public static function getCurrent(): ?self
98
    {
99 72
        return self::$currentUser;
100
    }
101
102
    /**
103
     * @var null|string
104
     *
105
     * @ORM\Column(type="string", length=50, nullable=true, unique=true)
106
     */
107
    private $login;
108
109
    /**
110
     * @var string
111
     * @ORM\Column(type="string", length=191)
112
     */
113
    private $firstName = '';
114
115
    /**
116
     * @var string
117
     * @ORM\Column(type="string", length=191)
118
     */
119
    private $lastName = '';
120
121
    /**
122
     * @var null|string
123
     * @ORM\Column(type="string", length=191, nullable=true, unique=true)
124
     */
125
    private $email;
126
127
    /**
128
     * @var string
129
     * @ORM\Column(type="UserRole", options={"default" = User::ROLE_INDIVIDUAL})
130
     */
131
    private $role = self::ROLE_INDIVIDUAL;
132
133
    /**
134
     * @var string
135
     * @ORM\Column(type="UserStatus", options={"default" = User::STATUS_NEW})
136
     */
137
    private $status = self::STATUS_NEW;
138
139
    /**
140
     * @var null|Chronos
141
     * @ORM\Column(type="datetime", nullable=true)
142
     */
143
    private $welcomeSessionDate;
144
145
    /**
146
     * @var null|Chronos
147
     * @ORM\Column(type="datetime", nullable=true)
148
     */
149
    private $resignDate;
150
151
    /**
152
     * @var int sex
153
     * @ORM\Column(type="smallint", options={"default" = 0}))
154
     */
155
    private $sex = 0;
156
157
    /**
158
     * @var string
159
     * @ORM\Column(type="string", length=25, options={"default" = ""})
160
     */
161
    private $phone = '';
162
163
    /**
164
     * @var string
165
     * @ORM\Column(type="string", length=25, options={"default" = ""})
166
     */
167
    private $mobilePhone = '';
168
169
    /**
170
     * @var string
171
     * @ORM\Column(type="string", length=25, options={"default" = ""})
172
     */
173
    private $swissSailing = '';
174
175
    /**
176
     * @var string
177
     * @ORM\Column(type="SwissSailingType", nullable=true)
178
     */
179
    private $swissSailingType;
180
181
    /**
182
     * @var string
183
     * @ORM\Column(type="SwissWindsurfType", nullable=true)
184
     */
185
    private $swissWindsurfType;
186
187
    /**
188
     * @var null|Date
189
     * @ORM\Column(type="date", nullable=true)
190
     */
191
    private $birthday;
192
193
    /**
194
     * @var bool
195
     * @ORM\Column(type="boolean", options={"default" = 0})
196
     */
197
    private $termsAgreement = false;
198
199
    /**
200
     * @var bool
201
     * @ORM\Column(type="boolean", options={"default" = 0})
202
     */
203
    private $hasInsurance = false;
204
205
    /**
206
     * @var bool
207
     * @ORM\Column(type="boolean", options={"default" = 0})
208
     */
209
    private $receivesNewsletter = false;
210
211
    /**
212
     * @var string
213
     * @ORM\Column(type="Relationship", options={"default" = RelationshipType::HOUSEHOLDER})
214
     */
215
    private $familyRelationship = RelationshipType::HOUSEHOLDER;
216
217
    /**
218
     * @var string
219
     * @ORM\Column(type="BillingType", options={"default" = BillingTypeType::ELECTRONIC})
220
     */
221
    private $billingType = BillingTypeType::ELECTRONIC;
222
223
    /**
224
     * @var Collection
225
     * @ORM\OneToMany(targetEntity="Booking", mappedBy="owner")
226
     */
227
    private $bookings;
228
229
    /**
230
     * @var Collection
231
     * @ORM\ManyToMany(targetEntity="License", mappedBy="users")
232
     */
233
    private $licenses;
234
235
    /**
236
     * @var Collection
237
     * @ORM\ManyToMany(targetEntity="UserTag", mappedBy="users")
238
     */
239
    private $userTags;
240
241
    /**
242
     * @var Collection
243
     * @ORM\OneToMany(targetEntity="Message", mappedBy="recipient")
244
     */
245
    private $messages;
246
247
    /**
248
     * There is actually 0 to 1 account, never more. And this is
249
     * enforced by DB unique constraints
250
     *
251
     * @var Collection
252
     * @ORM\OneToMany(targetEntity="Account", mappedBy="owner")
253
     */
254
    private $accounts;
255
256
    /**
257
     * @var Collection
258
     * @ORM\OneToMany(targetEntity="User", mappedBy="owner")
259
     */
260
    private $users;
261
262
    /**
263
     * Constructor
264
     *
265
     * @param string $role role for new user
266
     */
267 54
    public function __construct(string $role = self::ROLE_INDIVIDUAL)
268
    {
269 54
        $this->role = $role;
270 54
        $this->bookings = new ArrayCollection();
271 54
        $this->accounts = new ArrayCollection();
272 54
        $this->licenses = new ArrayCollection();
273 54
        $this->userTags = new ArrayCollection();
274 54
        $this->messages = new ArrayCollection();
275 54
        $this->users = new ArrayCollection();
276 54
    }
277
278
    /**
279
     * Set login (eg: johndoe)
280
     *
281
     * @API\Input(type="Login")
282
     */
283 4
    public function setLogin(string $login): void
284
    {
285 4
        $this->login = $login;
286 4
    }
287
288
    /**
289
     * Get login (eg: johndoe)
290
     *
291
     * @API\Field(type="?Login")
292
     */
293 14
    public function getLogin(): ?string
294
    {
295 14
        return $this->login;
296
    }
297
298
    /**
299
     * Set first name
300
     *
301
     * @param string $firstName
302
     */
303 10
    public function setFirstName($firstName): void
304
    {
305 10
        $this->firstName = $firstName;
306 10
    }
307
308
    /**
309
     * Get first name
310
     */
311 18
    public function getFirstName(): string
312
    {
313 18
        return (string) $this->firstName;
314
    }
315
316
    /**
317
     * Set last name
318
     *
319
     * @param string $lastName
320
     */
321 10
    public function setLastName($lastName): void
322
    {
323 10
        $this->lastName = $lastName;
324 10
    }
325
326
    /**
327
     * Get last name
328
     */
329 12
    public function getLastName(): string
330
    {
331 12
        return (string) $this->lastName;
332
    }
333
334
    /**
335
     * Get full name
336
     */
337 12
    public function getName(): string
338
    {
339 12
        return implode(' ', [$this->getFirstName(), $this->getLastName()]);
340
    }
341
342
    /**
343
     * Set email
344
     *
345
     * @API\Input(type="?Email")
346
     */
347 5
    public function setEmail(?string $email): void
348
    {
349 5
        $this->email = $email;
350 5
    }
351
352
    /**
353
     * Get email
354
     *
355
     * @API\Field(type="?Email")
356
     */
357 11
    public function getEmail(): ?string
358
    {
359 11
        return $this->email;
360
    }
361
362
    /**
363
     * Returns whether the user is administrator and thus have can do anything.
364
     *
365
     * @API\Field(type="Application\Api\Enum\UserRoleType")
366
     */
367 78
    public function getRole(): string
368
    {
369 78
        return $this->role;
370
    }
371
372
    /**
373
     * Sets the user role
374
     *
375
     * The current user is allowed to promote another user up to the same role as himself. So
376
     * a Responsible can promote a Member to Responsible. Or an Admin can promote a Individual to Admin.
377
     *
378
     * But the current user is **not** allowed to demote a user who has a higher role than himself.
379
     * That means that a Responsible cannot demote an Admin to Individual.
380
     */
381 7
    public function setRole(string $role): void
382
    {
383 7
        if ($role === $this->role) {
384 2
            return;
385
        }
386
387 5
        $currentRole = self::getCurrent() ? self::getCurrent()->getRole() : self::ROLE_ANONYMOUS;
388
        $orderedRoles = [
389 5
            self::ROLE_ANONYMOUS,
390 5
            self::ROLE_INDIVIDUAL,
391 5
            self::ROLE_MEMBER,
392 5
            self::ROLE_TRAINER,
393 5
            self::ROLE_BOOKING_ONLY,
394 5
            self::ROLE_RESPONSIBLE,
395 5
            self::ROLE_ADMINISTRATOR,
396
        ];
397
398 5
        $newFound = false;
399 5
        $oldFound = false;
400 5
        foreach ($orderedRoles as $r) {
401 5
            if ($r === $this->role) {
402 3
                $oldFound = true;
403
            }
404 5
            if ($r === $role) {
405 2
                $newFound = true;
406
            }
407
408 5
            if ($r === $currentRole) {
409 5
                break;
410
            }
411
        }
412
413 5
        if (!$newFound || !$oldFound) {
414 3
            throw new Exception($currentRole . ' is not allowed to change role from ' . $this->role . ' to ' . $role);
415
        }
416
417 2
        $this->role = $role;
418 2
    }
419
420 5
    public function setOwner(?self $owner = null): void
421
    {
422 5
        if ($owner && $owner !== $this) {
423 1
            if ($owner->getOwner() && $owner !== $owner->getOwner()) {
424
                throw new Exception('This user cannot be owned by a user who is himself owned by somebody else');
425
            }
426
427 1
            if ($this->users->count()) {
428
                throw new Exception('This user owns other users, so he cannot himself be owned by somebody else');
429
            }
430
        }
431
432 5
        if ($this->getOwner()) {
433 2
            $this->getOwner()->getEmail(); // Trigger lazy loading
434 2
            $this->getOwner()->users->removeElement($this);
435
        }
436
437 5
        parent::setOwner($owner);
438
439 5
        if ($this->getOwner()) {
440 1
            $this->getOwner()->getEmail(); // Trigger lazy loading
441 1
            $this->getOwner()->users->add($this);
442 1
            $this->setStatus($this->getOwner()->getStatus());
443
        }
444 5
    }
445
446
    /**
447
     * @API\Field(type="Application\Api\Enum\UserStatusType")
448
     */
449 3
    public function getStatus(): string
450
    {
451 3
        return $this->status;
452
    }
453
454
    /**
455
     * @API\Input(type="Application\Api\Enum\UserStatusType")
456
     */
457 10
    public function setStatus(string $newStatus): void
458
    {
459 10
        if ($newStatus === self::STATUS_ARCHIVED && $this->status !== self::STATUS_ARCHIVED) {
460 1
            $this->setResignDate(Chronos::NOW());
461 9
        } elseif ($this->status === self::STATUS_ARCHIVED && $newStatus !== self::STATUS_ARCHIVED) {
462
            $this->setResignDate(null);
463
        }
464
465 10
        $this->status = $newStatus;
466 10
        $this->revokeToken();
467
468 10
        foreach ($this->users as $user) {
469 1
            if ($user !== $this) {
470
                $user->setStatus($newStatus);
471
            }
472
        }
473 10
    }
474
475
    /**
476
     * Whether this user is a family owner or not
477
     *
478
     * This is used for our internal logic and should
479
     * NEVER be related to `familyRelationship`. That field
480
     * is purely informative for humans.
481
     */
482 1
    public function isFamilyOwner(): bool
483
    {
484 1
        return !$this->getOwner() || $this->getOwner() === $this;
485
    }
486
487 1
    public function initialize(): void
488
    {
489 1
        $this->role = self::ROLE_MEMBER; // Bypass security
490 1
        $this->setStatus(self::STATUS_NEW);
491 1
    }
492
493
    public function getPhone(): string
494
    {
495
        return $this->phone;
496
    }
497
498
    public function setPhone(string $phone): void
499
    {
500
        $this->phone = $phone;
501
    }
502
503
    public function getMobilePhone(): string
504
    {
505
        return $this->mobilePhone;
506
    }
507
508 1
    public function setMobilePhone(string $mobilePhone): void
509
    {
510 1
        $this->mobilePhone = $mobilePhone;
511 1
    }
512
513
    public function getBirthday(): ?Date
514
    {
515
        return $this->birthday;
516
    }
517
518 1
    public function setBirthday(?Date $birthday): void
519
    {
520 1
        $this->birthday = $birthday;
521 1
    }
522
523
    /**
524
     * return null|int
525
     */
526
    public function getAge(): ?int
527
    {
528
        if ($this->birthday) {
529
            return (new Date())->diffInYears($this->birthday);
530
        }
531
532
        return null;
533
    }
534
535
    /**
536
     * Get bookings
537
     */
538 4
    public function getBookings(): Collection
539
    {
540 4
        return $this->bookings;
541
    }
542
543
    /**
544
     * Notify the user that it has a new booking.
545
     * This should only be called by Booking::setResponsible()
546
     */
547 15
    public function bookingAdded(Booking $booking): void
548
    {
549 15
        $this->bookings->add($booking);
550 15
    }
551
552
    /**
553
     * Notify the user that it has a booking was removed.
554
     * This should only be called by Booking::setResponsible()
555
     */
556 4
    public function bookingRemoved(Booking $booking): void
557
    {
558 4
        $this->bookings->removeElement($booking);
559 4
    }
560
561 2
    public function getLicenses(): Collection
562
    {
563 2
        return $this->licenses;
564
    }
565
566 1
    public function getUserTags(): Collection
567
    {
568 1
        return $this->userTags;
569
    }
570
571
    /**
572
     * Notify the user that it has a new license.
573
     * This should only be called by License::addUser()
574
     */
575 1
    public function licenseAdded(License $license): void
576
    {
577 1
        $this->licenses->add($license);
578 1
    }
579
580
    /**
581
     * Notify the user that it a license was removed.
582
     * This should only be called by License::removeUser()
583
     */
584 1
    public function licenseRemoved(License $license): void
585
    {
586 1
        $this->licenses->removeElement($license);
587 1
    }
588
589
    /**
590
     * Notify the user that it has a new userTag.
591
     * This should only be called by UserTag::addUser()
592
     */
593 1
    public function userTagAdded(UserTag $userTag): void
594
    {
595 1
        $this->userTags->add($userTag);
596 1
    }
597
598
    /**
599
     * Notify the user that a userTag was removed.
600
     * This should only be called by UserTag::removeUser()
601
     */
602 1
    public function userTagRemoved(UserTag $userTag): void
603
    {
604 1
        $this->userTags->removeElement($userTag);
605 1
    }
606
607
    public function isTermsAgreement(): bool
608
    {
609
        return $this->termsAgreement;
610
    }
611
612 1
    public function setTermsAgreement(bool $termsAgreement): void
613
    {
614 1
        $this->termsAgreement = $termsAgreement;
615 1
    }
616
617
    public function hasInsurance(): bool
618
    {
619
        return $this->hasInsurance;
620
    }
621
622 1
    public function setHasInsurance(bool $hasInsurance): void
623
    {
624 1
        $this->hasInsurance = $hasInsurance;
625 1
    }
626
627
    public function getWelcomeSessionDate(): ?Chronos
628
    {
629
        return $this->welcomeSessionDate;
630
    }
631
632
    public function setWelcomeSessionDate(?Chronos $welcomeSessionDate): void
633
    {
634
        $this->welcomeSessionDate = $welcomeSessionDate;
635
    }
636
637
    public function getResignDate(): ?Chronos
638
    {
639
        return $this->resignDate;
640
    }
641
642 1
    public function setResignDate(?Chronos $resignDate): void
643
    {
644 1
        $this->resignDate = $resignDate;
645 1
    }
646
647
    public function getReceivesNewsletter(): bool
648
    {
649
        return $this->receivesNewsletter;
650
    }
651
652
    public function setReceivesNewsletter(bool $receivesNewsletter): void
653
    {
654
        $this->receivesNewsletter = $receivesNewsletter;
655
    }
656
657
    /**
658
     * Get the sex
659
     *
660
     * @API\Field(type="Sex")
661
     */
662
    public function getSex(): int
663
    {
664
        return $this->sex;
665
    }
666
667
    /**
668
     * Set the sex
669
     *
670
     * @API\Input(type="Sex")
671
     */
672
    public function setSex(int $sex): void
673
    {
674
        $this->sex = $sex;
675
    }
676
677
    /**
678
     * Get the Swiss Sailing licence number
679
     */
680
    public function getSwissSailing(): string
681
    {
682
        return $this->swissSailing;
683
    }
684
685
    public function setSwissSailing(string $swissSailing): void
686
    {
687
        $this->swissSailing = $swissSailing;
688
    }
689
690
    /**
691
     * Get the Swiss Sailing licence type
692
     *
693
     * @API\Field(type="?SwissSailingType")
694
     */
695
    public function getSwissSailingType(): ?string
696
    {
697
        return $this->swissSailingType;
698
    }
699
700
    /**
701
     * Set the Swiss Sailing licence type
702
     *
703
     * @API\Input(type="?SwissSailingType")
704
     */
705
    public function setSwissSailingType(?string $swissSailingType): void
706
    {
707
        $this->swissSailingType = $swissSailingType;
708
    }
709
710
    /**
711
     * Get the Swiss Windsurf licence type
712
     *
713
     * @API\Field(type="?SwissWindsurfType")
714
     */
715
    public function getSwissWindsurfType(): ?string
716
    {
717
        return $this->swissWindsurfType;
718
    }
719
720
    /**
721
     * Set the Swiss Windsurf licence type
722
     *
723
     * @API\Input(type="?SwissWindsurfType")
724
     */
725
    public function setSwissWindsurfType(?string $swissWindsurfType): void
726
    {
727
        $this->swissWindsurfType = $swissWindsurfType;
728
    }
729
730
    /**
731
     * Get the first login date
732
     */
733
    public function getFirstLogin(): ?Chronos
734
    {
735
        /** @var LogRepository $logRepository */
736
        $logRepository = _em()->getRepository(Log::class);
737
738
        return $logRepository->getLoginDate($this, true);
739
    }
740
741
    /**
742
     * Get the last login date
743
     */
744
    public function getLastLogin(): ?Chronos
745
    {
746
        /** @var LogRepository $logRepository */
747
        $logRepository = _em()->getRepository(Log::class);
748
749
        return $logRepository->getLoginDate($this, false);
750
    }
751
752
    /**
753
     * @API\Field(type="Relationship")
754
     */
755 1
    public function getFamilyRelationship(): string
756
    {
757 1
        return $this->familyRelationship;
758
    }
759
760
    /**
761
     * @API\Input(type="Relationship")
762
     */
763 1
    public function setFamilyRelationship(string $familyRelationship): void
764
    {
765 1
        $this->familyRelationship = $familyRelationship;
766 1
    }
767
768
    /**
769
     * @API\Field(type="BillingType")
770
     */
771
    public function getBillingType(): string
772
    {
773
        return $this->billingType;
774
    }
775
776
    /**
777
     * @API\Input(type="BillingType")
778
     */
779
    public function setBillingType(string $billingType): void
780
    {
781
        $this->billingType = $billingType;
782
    }
783
784
    /**
785
     * Get the user transaction account
786
     */
787 27
    public function getAccount(): ?Account
788
    {
789 27
        if ($this->getOwner() && $this->getOwner() !== $this) {
790 7
            return $this->getOwner()->getAccount();
791
        }
792
793 27
        return $this->accounts->count() ? $this->accounts->first() : null;
794
    }
795
796
    /**
797
     * Notify the user that it has a new account
798
     * This should only be called by Account::setOwner()
799
     */
800 13
    public function accountAdded(Account $account): void
801
    {
802 13
        $this->accounts->clear();
803 13
        $this->accounts->add($account);
804 13
    }
805
806
    /**
807
     * Notify the user that a account was removed
808
     * This should only be called by Account::setOwner()
809
     */
810 1
    public function accountRemoved(): void
811
    {
812 1
        $this->accounts->clear();
813 1
    }
814
815
    /**
816
     * Get messages sent to the user
817
     */
818 1
    public function getMessages(): Collection
819
    {
820 1
        return $this->messages;
821
    }
822
823
    /**
824
     * Notify the user that it has a new message
825
     * This should only be called by Message::setRecipient()
826
     */
827 8
    public function messageAdded(Message $message): void
828
    {
829 8
        $this->messages->add($message);
830 8
    }
831
832
    /**
833
     * Notify the user that a message was removed
834
     * This should only be called by Message::setRecipient()
835
     */
836 1
    public function messageRemoved(Message $message): void
837
    {
838 1
        $this->messages->removeElement($message);
839 1
    }
840
841
    /**
842
     * Check if the user can *really* open a door
843
     * This also takes into account the user status and role
844
     *
845
     * @API\Field(args={@API\Argument(name="door", type="?Application\Api\Enum\DoorType")})
846
     *
847
     * @param null|string $door a particular door, or null for any
848
     */
849 6
    public function getCanOpenDoor(?string $door = null): bool
850
    {
851 6
        $allowedStatus = [self::STATUS_ACTIVE];
852 6
        $allowedRoles = [self::ROLE_INDIVIDUAL, self::ROLE_MEMBER, self::ROLE_RESPONSIBLE, self::ROLE_ADMINISTRATOR];
853
854 6
        if ($door && !$this->$door) {
855 3
            return false;
856
        }
857
858 6
        if (!in_array($this->status, $allowedStatus, true) || !in_array($this->role, $allowedRoles, true)) {
859 2
            return false;
860
        }
861
862 4
        return true;
863
    }
864
865
    /**
866
     * Override parent to prevents users created from administration to be family of the administrator
867
     *
868
     * The owner must be explicitly set for all users.
869
     */
870 3
    protected function getOwnerForCreation(): ?self
871
    {
872 3
        return null;
873
    }
874
}
875