Completed
Push — master ( 0a0c4a...11fa4c )
by Adrien
11:33
created

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