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