1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Model; |
6
|
|
|
|
7
|
|
|
use Application\Api\Exception; |
8
|
|
|
use Application\DBAL\Types\BillingTypeType; |
9
|
|
|
use Application\DBAL\Types\RelationshipType; |
10
|
|
|
use Application\ORM\Query\Filter\AclFilter; |
11
|
|
|
use Application\Traits\HasAddress; |
12
|
|
|
use Application\Traits\HasDoorAccess; |
13
|
|
|
use Application\Traits\HasRemarks; |
14
|
|
|
use Cake\Chronos\Chronos; |
15
|
|
|
use Cake\Chronos\Date; |
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Collections\Collection; |
18
|
|
|
use Doctrine\ORM\Mapping as ORM; |
19
|
|
|
use GraphQL\Doctrine\Annotation as API; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* User |
23
|
|
|
* |
24
|
|
|
* @ORM\Entity(repositoryClass="Application\Repository\UserRepository") |
25
|
|
|
* @ORM\AssociationOverrides({ |
26
|
|
|
* @ORM\AssociationOverride(name="owner", inversedBy="users") |
27
|
|
|
* }) |
28
|
|
|
*/ |
29
|
|
|
class User extends AbstractModel |
30
|
|
|
{ |
31
|
|
|
const ROLE_ANONYMOUS = 'anonymous'; |
32
|
|
|
const ROLE_BOOKING_ONLY = 'booking_only'; |
33
|
|
|
const ROLE_INDIVIDUAL = 'individual'; |
34
|
|
|
const ROLE_MEMBER = 'member'; |
35
|
|
|
const ROLE_RESPONSIBLE = 'responsible'; |
36
|
|
|
const ROLE_ADMINISTRATOR = 'administrator'; |
37
|
|
|
|
38
|
|
|
const STATUS_INACTIVE = 'inactive'; |
39
|
|
|
const STATUS_NEW = 'new'; |
40
|
|
|
const STATUS_ACTIVE = 'active'; |
41
|
|
|
const STATUS_ARCHIVED = 'archived'; |
42
|
|
|
|
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
|
66 |
|
public static function setCurrent(?self $user): void |
59
|
|
|
{ |
60
|
66 |
|
self::$currentUser = $user; |
61
|
|
|
|
62
|
|
|
// Initalize ACL filter with current user if a logged in one exists |
63
|
66 |
|
_em()->getFilters()->getFilter(AclFilter::class)->setUser($user); |
64
|
66 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns currently logged user or null |
68
|
|
|
* |
69
|
|
|
* @return null|self |
70
|
|
|
*/ |
71
|
48 |
|
public static function getCurrent(): ?self |
72
|
|
|
{ |
73
|
48 |
|
return self::$currentUser; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var null|string |
78
|
|
|
* |
79
|
|
|
* @ORM\Column(type="string", length=50, nullable=true, 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 string |
97
|
|
|
* |
98
|
|
|
* @ORM\Column(type="string", length=255) |
99
|
|
|
*/ |
100
|
|
|
private $password = ''; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var null|string |
104
|
|
|
* @ORM\Column(type="string", length=191, nullable=true, unique=true) |
105
|
|
|
*/ |
106
|
|
|
private $email; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var string |
110
|
|
|
* @ORM\Column(type="UserRole", options={"default" = User::ROLE_INDIVIDUAL}) |
111
|
|
|
*/ |
112
|
|
|
private $role = self::ROLE_INDIVIDUAL; |
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 null|string |
194
|
|
|
* @ORM\Column(type="string", length=32, nullable=true, unique=true) |
195
|
|
|
*/ |
196
|
|
|
private $token; |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @var null|Chronos |
200
|
|
|
* |
201
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
202
|
|
|
*/ |
203
|
|
|
private $tokenCreationDate; |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @var Collection |
207
|
|
|
* @ORM\OneToMany(targetEntity="Booking", mappedBy="owner") |
208
|
|
|
*/ |
209
|
|
|
private $bookings; |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @var Collection |
213
|
|
|
* @ORM\ManyToMany(targetEntity="License", mappedBy="users") |
214
|
|
|
*/ |
215
|
|
|
private $licenses; |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @var Collection |
219
|
|
|
* @ORM\ManyToMany(targetEntity="UserTag", mappedBy="users") |
220
|
|
|
*/ |
221
|
|
|
private $userTags; |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @var Collection |
225
|
|
|
* @ORM\OneToMany(targetEntity="Message", mappedBy="recipient") |
226
|
|
|
*/ |
227
|
|
|
private $messages; |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* There is actually 0 to 1 account, never more. And this is |
231
|
|
|
* enforced by DB unique constraints |
232
|
|
|
* |
233
|
|
|
* @var Collection |
234
|
|
|
* @ORM\OneToMany(targetEntity="Account", mappedBy="owner") |
235
|
|
|
*/ |
236
|
|
|
private $accounts; |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @var Collection |
240
|
|
|
* @ORM\OneToMany(targetEntity="User", mappedBy="owner") |
241
|
|
|
*/ |
242
|
|
|
private $users; |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Constructor |
246
|
|
|
* |
247
|
|
|
* @param string $role role for new user |
248
|
|
|
*/ |
249
|
35 |
|
public function __construct(string $role = self::ROLE_INDIVIDUAL) |
250
|
|
|
{ |
251
|
35 |
|
$this->role = $role; |
252
|
35 |
|
$this->bookings = new ArrayCollection(); |
253
|
35 |
|
$this->accounts = new ArrayCollection(); |
254
|
35 |
|
$this->licenses = new ArrayCollection(); |
255
|
35 |
|
$this->userTags = new ArrayCollection(); |
256
|
35 |
|
$this->messages = new ArrayCollection(); |
257
|
35 |
|
$this->users = new ArrayCollection(); |
258
|
35 |
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set login (eg: johndoe) |
262
|
|
|
* |
263
|
|
|
* @API\Input(type="Login") |
264
|
|
|
* |
265
|
|
|
* @param string $login |
266
|
|
|
*/ |
267
|
2 |
|
public function setLogin(string $login): void |
268
|
|
|
{ |
269
|
2 |
|
$this->login = $login; |
270
|
2 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get login (eg: johndoe) |
274
|
|
|
* |
275
|
|
|
* @API\Field(type="?Login") |
276
|
|
|
* |
277
|
|
|
* @return null|string |
278
|
|
|
*/ |
279
|
11 |
|
public function getLogin(): ?string |
280
|
|
|
{ |
281
|
11 |
|
return $this->login; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Hash and change the user password |
286
|
|
|
* |
287
|
|
|
* @param string $password |
288
|
|
|
*/ |
289
|
6 |
|
public function setPassword(string $password): void |
290
|
|
|
{ |
291
|
|
|
// Ignore empty password that could be sent "by mistake" by the client |
292
|
|
|
// when agreeing to terms |
293
|
6 |
|
if ($password === '') { |
294
|
1 |
|
return; |
295
|
|
|
} |
296
|
|
|
|
297
|
6 |
|
$this->revokeToken(); |
298
|
|
|
|
299
|
6 |
|
$this->password = password_hash($password, PASSWORD_DEFAULT); |
300
|
6 |
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns the hashed password |
304
|
|
|
* |
305
|
|
|
* @API\Exclude |
306
|
|
|
* |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
3 |
|
public function getPassword(): string |
310
|
|
|
{ |
311
|
3 |
|
return $this->password; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Set first name |
316
|
|
|
* |
317
|
|
|
* @param string $firstName |
318
|
|
|
*/ |
319
|
2 |
|
public function setFirstName($firstName): void |
320
|
|
|
{ |
321
|
2 |
|
$this->firstName = $firstName; |
322
|
2 |
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get first name |
326
|
|
|
* |
327
|
|
|
* @return string |
328
|
|
|
*/ |
329
|
5 |
|
public function getFirstName(): string |
330
|
|
|
{ |
331
|
5 |
|
return (string) $this->firstName; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Set last name |
336
|
|
|
* |
337
|
|
|
* @param string $lastName |
338
|
|
|
*/ |
339
|
2 |
|
public function setLastName($lastName): void |
340
|
|
|
{ |
341
|
2 |
|
$this->lastName = $lastName; |
342
|
2 |
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Get last name |
346
|
|
|
* |
347
|
|
|
* @return string |
348
|
|
|
*/ |
349
|
2 |
|
public function getLastName(): string |
350
|
|
|
{ |
351
|
2 |
|
return (string) $this->lastName; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Get full name |
356
|
|
|
* |
357
|
|
|
* @return string |
358
|
|
|
*/ |
359
|
2 |
|
public function getName(): string |
360
|
|
|
{ |
361
|
2 |
|
return implode(' ', [$this->getFirstName(), $this->getLastName()]); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Set email |
366
|
|
|
* |
367
|
|
|
* @API\Input(type="?Email") |
368
|
|
|
* |
369
|
|
|
* @param string $email |
370
|
|
|
*/ |
371
|
3 |
|
public function setEmail(string $email): void |
372
|
|
|
{ |
373
|
3 |
|
$this->email = $email; |
374
|
3 |
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Get email |
378
|
|
|
* |
379
|
|
|
* @API\Field(type="?Email") |
380
|
|
|
* |
381
|
|
|
* @return null|string |
382
|
|
|
*/ |
383
|
11 |
|
public function getEmail(): ?string |
384
|
|
|
{ |
385
|
11 |
|
return $this->email; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Returns whether the user is administrator and thus have can do anything. |
390
|
|
|
* |
391
|
|
|
* @API\Field(type="Application\Api\Enum\UserRoleType") |
392
|
|
|
*/ |
393
|
32 |
|
public function getRole(): string |
394
|
|
|
{ |
395
|
32 |
|
return $this->role; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Sets the user role |
400
|
|
|
* |
401
|
|
|
* The current user is allowed to promote another user up to the same role as himself. So |
402
|
|
|
* a Responsible can promote a Member to Responsible. Or an Admin can promote a Individual to Admin. |
403
|
|
|
* |
404
|
|
|
* But the current user is **not** allowed to demote a user who has a higher role than himself. |
405
|
|
|
* That means that a Responsible cannot demote an Admin to Individual. |
406
|
|
|
* |
407
|
|
|
* @param string $role |
408
|
|
|
*/ |
409
|
7 |
|
public function setRole(string $role): void |
410
|
|
|
{ |
411
|
7 |
|
if ($role === $this->role) { |
412
|
2 |
|
return; |
413
|
|
|
} |
414
|
|
|
|
415
|
5 |
|
$currentRole = self::getCurrent() ? self::getCurrent()->getRole() : self::ROLE_ANONYMOUS; |
416
|
|
|
$orderedRoles = [ |
417
|
5 |
|
self::ROLE_ANONYMOUS, |
418
|
5 |
|
self::ROLE_INDIVIDUAL, |
419
|
5 |
|
self::ROLE_MEMBER, |
420
|
5 |
|
self::ROLE_RESPONSIBLE, |
421
|
5 |
|
self::ROLE_ADMINISTRATOR, |
422
|
|
|
]; |
423
|
|
|
|
424
|
5 |
|
$newFound = false; |
425
|
5 |
|
$oldFound = false; |
426
|
5 |
|
foreach ($orderedRoles as $r) { |
427
|
5 |
|
if ($r === $this->role) { |
428
|
3 |
|
$oldFound = true; |
429
|
|
|
} |
430
|
5 |
|
if ($r === $role) { |
431
|
2 |
|
$newFound = true; |
432
|
|
|
} |
433
|
|
|
|
434
|
5 |
|
if ($r === $currentRole) { |
435
|
5 |
|
break; |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
5 |
|
if (!$newFound || !$oldFound) { |
440
|
3 |
|
throw new Exception($currentRole . ' is not allowed to change role from ' . $this->role . ' to ' . $role); |
441
|
|
|
} |
442
|
|
|
|
443
|
2 |
|
$this->role = $role; |
444
|
2 |
|
} |
445
|
|
|
|
446
|
6 |
|
public function setOwner(self $owner = null): void |
447
|
|
|
{ |
448
|
6 |
|
if ($owner && $owner !== $this) { |
449
|
4 |
|
if ($owner->getOwner() && $owner !== $owner->getOwner()) { |
450
|
1 |
|
throw new Exception('This user cannot be owned by a user who is himself owned by somebody else'); |
451
|
|
|
} |
452
|
|
|
|
453
|
4 |
|
if ($this->users->count()) { |
454
|
1 |
|
throw new Exception('This user owns other users, so he cannot himself be owned by somebody else'); |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
6 |
|
if ($this->getOwner()) { |
459
|
1 |
|
$this->getOwner()->getEmail(); // Trigger lazy loading |
460
|
1 |
|
$this->getOwner()->users->removeElement($this); |
461
|
|
|
} |
462
|
|
|
|
463
|
6 |
|
parent::setOwner($owner); |
464
|
|
|
|
465
|
6 |
|
if ($this->getOwner()) { |
466
|
5 |
|
$this->getOwner()->getEmail(); // Trigger lazy loading |
467
|
5 |
|
$this->getOwner()->users->add($this); |
468
|
5 |
|
$this->setStatus($this->getOwner()->getStatus()); |
469
|
|
|
} |
470
|
6 |
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* @API\Field(type="Application\Api\Enum\UserStatusType") |
474
|
|
|
* |
475
|
|
|
* @return string |
476
|
|
|
*/ |
477
|
7 |
|
public function getStatus(): string |
478
|
|
|
{ |
479
|
7 |
|
return $this->status; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @API\Input(type="Application\Api\Enum\UserStatusType") |
484
|
|
|
* |
485
|
|
|
* @param string $status |
486
|
|
|
*/ |
487
|
14 |
|
public function setStatus(string $status): void |
488
|
|
|
{ |
489
|
14 |
|
$this->status = $status; |
490
|
14 |
|
$this->revokeToken(); |
491
|
|
|
|
492
|
14 |
|
foreach ($this->users as $user) { |
493
|
4 |
|
if ($user !== $this) { |
494
|
1 |
|
$user->setStatus($status); |
495
|
|
|
} |
496
|
|
|
} |
497
|
14 |
|
} |
498
|
|
|
|
499
|
1 |
|
public function activate(): void |
500
|
|
|
{ |
501
|
1 |
|
$this->role = self::ROLE_MEMBER; // Bypass security |
502
|
1 |
|
$this->setStatus(self::STATUS_ACTIVE); |
503
|
1 |
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* @return string |
507
|
|
|
*/ |
508
|
|
|
public function getPhone(): string |
509
|
|
|
{ |
510
|
|
|
return $this->phone; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* @param string $phone |
515
|
|
|
*/ |
516
|
|
|
public function setPhone(string $phone): void |
517
|
|
|
{ |
518
|
|
|
$this->phone = $phone; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* @return string |
523
|
|
|
*/ |
524
|
|
|
public function getMobilePhone(): string |
525
|
|
|
{ |
526
|
|
|
return $this->mobilePhone; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* @param string $mobilePhone |
531
|
|
|
*/ |
532
|
|
|
public function setMobilePhone(string $mobilePhone): void |
533
|
|
|
{ |
534
|
|
|
$this->mobilePhone = $mobilePhone; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* @return null|Date |
539
|
|
|
*/ |
540
|
|
|
public function getBirthday(): ?Date |
541
|
|
|
{ |
542
|
|
|
return $this->birthday; |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* @param null|Date $birthday |
547
|
|
|
*/ |
548
|
|
|
public function setBirthday(?Date $birthday): void |
549
|
|
|
{ |
550
|
|
|
$this->birthday = $birthday; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Get bookings |
555
|
|
|
* |
556
|
|
|
* @return Collection |
557
|
|
|
*/ |
558
|
2 |
|
public function getBookings(): Collection |
559
|
|
|
{ |
560
|
2 |
|
return $this->bookings; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Notify the user that it has a new booking. |
565
|
|
|
* This should only be called by Booking::setResponsible() |
566
|
|
|
* |
567
|
|
|
* @param Booking $booking |
568
|
|
|
*/ |
569
|
8 |
|
public function bookingAdded(Booking $booking): void |
570
|
|
|
{ |
571
|
8 |
|
$this->bookings->add($booking); |
572
|
8 |
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Notify the user that it has a booking was removed. |
576
|
|
|
* This should only be called by Booking::setResponsible() |
577
|
|
|
* |
578
|
|
|
* @param Booking $booking |
579
|
|
|
*/ |
580
|
4 |
|
public function bookingRemoved(Booking $booking): void |
581
|
|
|
{ |
582
|
4 |
|
$this->bookings->removeElement($booking); |
583
|
4 |
|
} |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* @return Collection |
587
|
|
|
*/ |
588
|
1 |
|
public function getLicenses(): Collection |
589
|
|
|
{ |
590
|
1 |
|
return $this->licenses; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* @return Collection |
595
|
|
|
*/ |
596
|
1 |
|
public function getUserTags(): Collection |
597
|
|
|
{ |
598
|
1 |
|
return $this->userTags; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Notify the user that it has a new license. |
603
|
|
|
* This should only be called by License::addUser() |
604
|
|
|
* |
605
|
|
|
* @param License $license |
606
|
|
|
*/ |
607
|
1 |
|
public function licenseAdded(License $license): void |
608
|
|
|
{ |
609
|
1 |
|
$this->licenses->add($license); |
610
|
1 |
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Notify the user that it a license was removed. |
614
|
|
|
* This should only be called by License::removeUser() |
615
|
|
|
* |
616
|
|
|
* @param License $license |
617
|
|
|
*/ |
618
|
1 |
|
public function licenseRemoved(License $license): void |
619
|
|
|
{ |
620
|
1 |
|
$this->licenses->removeElement($license); |
621
|
1 |
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* Notify the user that it has a new userTag. |
625
|
|
|
* This should only be called by UserTag::addUser() |
626
|
|
|
* |
627
|
|
|
* @param UserTag $userTag |
628
|
|
|
*/ |
629
|
1 |
|
public function userTagAdded(UserTag $userTag): void |
630
|
|
|
{ |
631
|
1 |
|
$this->userTags->add($userTag); |
632
|
1 |
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Notify the user that a userTag was removed. |
636
|
|
|
* This should only be called by UserTag::removeUser() |
637
|
|
|
* |
638
|
|
|
* @param UserTag $userTag |
639
|
|
|
*/ |
640
|
1 |
|
public function userTagRemoved(UserTag $userTag): void |
641
|
|
|
{ |
642
|
1 |
|
$this->userTags->removeElement($userTag); |
643
|
1 |
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* @return bool |
647
|
|
|
*/ |
648
|
|
|
public function isTermsAgreement(): bool |
649
|
|
|
{ |
650
|
|
|
return $this->termsAgreement; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* @param bool $termsAgreement |
655
|
|
|
*/ |
656
|
1 |
|
public function setTermsAgreement(bool $termsAgreement): void |
657
|
|
|
{ |
658
|
1 |
|
$this->termsAgreement = $termsAgreement; |
659
|
1 |
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* @return bool |
663
|
|
|
*/ |
664
|
|
|
public function getHasInsurance(): bool |
665
|
|
|
{ |
666
|
|
|
return $this->hasInsurance; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* @param bool $hasInsurance |
671
|
|
|
*/ |
672
|
1 |
|
public function setHasInsurance(bool $hasInsurance): void |
673
|
|
|
{ |
674
|
1 |
|
$this->hasInsurance = $hasInsurance; |
675
|
1 |
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* @return null|Chronos |
679
|
|
|
*/ |
680
|
|
|
public function getWelcomeSessionDate(): ?Chronos |
681
|
|
|
{ |
682
|
|
|
return $this->welcomeSessionDate; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* @param null|Chronos $welcomeSessionDate |
687
|
|
|
*/ |
688
|
|
|
public function setWelcomeSessionDate(?Chronos $welcomeSessionDate): void |
689
|
|
|
{ |
690
|
|
|
$this->welcomeSessionDate = $welcomeSessionDate; |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* @return bool |
695
|
|
|
*/ |
696
|
|
|
public function isReceivesNewsletter(): bool |
697
|
|
|
{ |
698
|
|
|
return $this->receivesNewsletter; |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
/** |
702
|
|
|
* @param bool $receivesNewsletter |
703
|
|
|
*/ |
704
|
|
|
public function setReceivesNewsletter(bool $receivesNewsletter): void |
705
|
|
|
{ |
706
|
|
|
$this->receivesNewsletter = $receivesNewsletter; |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* Get the ISO/IEC 5218 sex |
711
|
|
|
* |
712
|
|
|
* @API\Field(type="Sex") |
713
|
|
|
* |
714
|
|
|
* @return int |
715
|
|
|
*/ |
716
|
|
|
public function getSex(): int |
717
|
|
|
{ |
718
|
|
|
return $this->sex; |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* Set the ISO/IEC 5218 sex |
723
|
|
|
* |
724
|
|
|
* @API\Input(type="Sex") |
725
|
|
|
* |
726
|
|
|
* @param int $sex |
727
|
|
|
*/ |
728
|
|
|
public function setSex(int $sex): void |
729
|
|
|
{ |
730
|
|
|
$this->sex = $sex; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* @return string |
735
|
|
|
*/ |
736
|
|
|
public function getIchtusSwissSailing(): string |
737
|
|
|
{ |
738
|
|
|
return $this->ichtusSwissSailing; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
/** |
742
|
|
|
* @param string $ichtusSwissSailing |
743
|
|
|
*/ |
744
|
|
|
public function setIchtusSwissSailing(string $ichtusSwissSailing): void |
745
|
|
|
{ |
746
|
|
|
$this->ichtusSwissSailing = $ichtusSwissSailing; |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Get the last login |
751
|
|
|
* |
752
|
|
|
* @return null|Chronos |
753
|
|
|
*/ |
754
|
|
|
public function getLastLogin(): ?Chronos |
755
|
|
|
{ |
756
|
|
|
return $this->lastLogin; |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
|
|
* @param null|Chronos $lastLogin |
761
|
|
|
*/ |
762
|
3 |
|
public function setLastLogin(?Chronos $lastLogin): void |
763
|
|
|
{ |
764
|
3 |
|
$this->lastLogin = $lastLogin; |
765
|
3 |
|
$this->revokeToken(); |
766
|
3 |
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* @API\Field(type="Relationship") |
770
|
|
|
* |
771
|
|
|
* @return string |
772
|
|
|
*/ |
773
|
1 |
|
public function getFamilyRelationship(): string |
774
|
|
|
{ |
775
|
1 |
|
return $this->familyRelationship; |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* @API\Input(type="Relationship") |
780
|
|
|
* |
781
|
|
|
* @param string $familyRelationship |
782
|
|
|
*/ |
783
|
1 |
|
public function setFamilyRelationship(string $familyRelationship): void |
784
|
|
|
{ |
785
|
1 |
|
$this->familyRelationship = $familyRelationship; |
786
|
1 |
|
} |
787
|
|
|
|
788
|
|
|
/** |
789
|
|
|
* @return string |
790
|
|
|
*/ |
791
|
|
|
public function getBillingType(): string |
792
|
|
|
{ |
793
|
|
|
return $this->billingType; |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
/** |
797
|
|
|
* @param string $billingType |
798
|
|
|
*/ |
799
|
|
|
public function setBillingType(string $billingType): void |
800
|
|
|
{ |
801
|
|
|
$this->billingType = $billingType; |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* Get the user transaction account |
806
|
|
|
* |
807
|
|
|
* @return null|Account |
808
|
|
|
*/ |
809
|
1 |
|
public function getAccount(): ?Account |
810
|
|
|
{ |
811
|
1 |
|
return $this->accounts->count() ? $this->accounts->first() : null; |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
/** |
815
|
|
|
* Notify the user that it has a new account |
816
|
|
|
* This should only be called by Account::setOwner() |
817
|
|
|
* |
818
|
|
|
* @param Account $account |
819
|
|
|
*/ |
820
|
1 |
|
public function accountAdded(Account $account): void |
821
|
|
|
{ |
822
|
1 |
|
$this->accounts->clear(); |
823
|
1 |
|
$this->accounts->add($account); |
824
|
1 |
|
} |
825
|
|
|
|
826
|
|
|
/** |
827
|
|
|
* Notify the user that a account was removed |
828
|
|
|
* This should only be called by Account::setOwner() |
829
|
|
|
*/ |
830
|
1 |
|
public function accountRemoved(): void |
831
|
|
|
{ |
832
|
1 |
|
$this->accounts->clear(); |
833
|
1 |
|
} |
834
|
|
|
|
835
|
|
|
/** |
836
|
|
|
* Get messages sent to the user |
837
|
|
|
* |
838
|
|
|
* @return Collection |
839
|
|
|
*/ |
840
|
1 |
|
public function getMessages(): Collection |
841
|
|
|
{ |
842
|
1 |
|
return $this->messages; |
843
|
|
|
} |
844
|
|
|
|
845
|
|
|
/** |
846
|
|
|
* Notify the user that it has a new message |
847
|
|
|
* This should only be called by Message::setRecipient() |
848
|
|
|
* |
849
|
|
|
* @param Message $message |
850
|
|
|
*/ |
851
|
5 |
|
public function messageAdded(Message $message): void |
852
|
|
|
{ |
853
|
5 |
|
$this->messages->add($message); |
854
|
5 |
|
} |
855
|
|
|
|
856
|
|
|
/** |
857
|
|
|
* Notify the user that a message was removed |
858
|
|
|
* This should only be called by Message::setRecipient() |
859
|
|
|
* |
860
|
|
|
* @param Message $message |
861
|
|
|
*/ |
862
|
1 |
|
public function messageRemoved(Message $message): void |
863
|
|
|
{ |
864
|
1 |
|
$this->messages->removeElement($message); |
865
|
1 |
|
} |
866
|
|
|
|
867
|
|
|
/** |
868
|
|
|
* Generate a new random token to reset password |
869
|
|
|
*/ |
870
|
4 |
|
public function createToken(): string |
871
|
|
|
{ |
872
|
4 |
|
$this->token = bin2hex(random_bytes(16)); |
873
|
4 |
|
$this->tokenCreationDate = new Chronos(); |
874
|
|
|
|
875
|
4 |
|
return $this->token; |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
/** |
879
|
|
|
* Destroy existing token |
880
|
|
|
*/ |
881
|
18 |
|
private function revokeToken(): void |
882
|
|
|
{ |
883
|
18 |
|
$this->token = null; |
884
|
18 |
|
$this->tokenCreationDate = null; |
885
|
18 |
|
} |
886
|
|
|
|
887
|
|
|
/** |
888
|
|
|
* Check if token is valid. |
889
|
|
|
* |
890
|
|
|
* @API\Exclude |
891
|
|
|
* |
892
|
|
|
* @return bool |
893
|
|
|
*/ |
894
|
3 |
|
public function isTokenValid(): bool |
895
|
|
|
{ |
896
|
3 |
|
if (!$this->tokenCreationDate) { |
897
|
1 |
|
return false; |
898
|
|
|
} |
899
|
|
|
|
900
|
3 |
|
$timeLimit = $this->tokenCreationDate->addMinutes(30); |
901
|
|
|
|
902
|
3 |
|
return $timeLimit->isFuture(); |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
/** |
906
|
|
|
* Check if the user can *really* open a door |
907
|
|
|
* This also takes into account the user status, role and IP address |
908
|
|
|
* |
909
|
|
|
* @API\Field(args={@API\Argument(name="door", type="?Application\Api\Enum\DoorType")}) |
910
|
|
|
* |
911
|
|
|
* @param null|string $door a particular door, or null for any |
912
|
|
|
* |
913
|
|
|
* @return bool |
914
|
|
|
*/ |
915
|
6 |
|
public function getCanOpenDoor(?string $door = null): bool |
916
|
|
|
{ |
917
|
6 |
|
global $container; |
918
|
|
|
|
919
|
6 |
|
$allowedStatus = [self::STATUS_ACTIVE]; |
920
|
6 |
|
$allowedRoles = [self::ROLE_MEMBER, self::ROLE_RESPONSIBLE, self::ROLE_ADMINISTRATOR]; |
921
|
6 |
|
if ($door && !$this->$door) { |
922
|
3 |
|
return false; |
923
|
|
|
} |
924
|
6 |
|
if (!in_array($this->status, $allowedStatus, true) || !in_array($this->role, $allowedRoles, true)) { |
925
|
2 |
|
return false; |
926
|
|
|
} |
927
|
4 |
|
$apiConfig = $container->get('config')['doorsApi']; |
928
|
4 |
|
foreach ($apiConfig['authorizedIps'] as $host) { |
929
|
4 |
|
if (filter_var($host, FILTER_VALIDATE_DOMAIN) && in_array($_SERVER['REMOTE_ADDR'], gethostbynamel($host), true)) { |
930
|
3 |
|
return true; |
931
|
|
|
} |
932
|
1 |
|
if (filter_var($host, FILTER_VALIDATE_IP) && $_SERVER['REMOTE_ADDR'] === $host) { |
933
|
|
|
return true; |
934
|
|
|
} |
935
|
|
|
} |
936
|
|
|
|
937
|
1 |
|
return false; |
938
|
|
|
} |
939
|
|
|
} |
940
|
|
|
|