Failed Conditions
Push — master ( cde8a4...83021f )
by Sam
06:52
created

User::getActiveFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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