1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Model; |
6
|
|
|
|
7
|
|
|
use Application\Api\Exception; |
8
|
|
|
use Application\ORM\Query\Filter\AclFilter; |
9
|
|
|
use Application\Repository\LogRepository; |
10
|
|
|
use Application\Traits\HasAddress; |
11
|
|
|
use Application\Traits\HasInternalRemarks; |
12
|
|
|
use Application\Traits\HasNumericCode; |
13
|
|
|
use Application\Traits\HasUrl; |
14
|
|
|
use Application\Utility; |
15
|
|
|
use Cake\Chronos\Chronos; |
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\HasLifecycleCallbacks |
26
|
|
|
* @ORM\AssociationOverrides({ |
27
|
|
|
* @ORM\AssociationOverride(name="owner", inversedBy="users") |
28
|
|
|
* }) |
29
|
|
|
*/ |
30
|
|
|
class User extends AbstractModel |
31
|
|
|
{ |
32
|
|
|
const ROLE_ANONYMOUS = 'anonymous'; |
33
|
|
|
const ROLE_MEMBER = 'member'; |
34
|
|
|
const ROLE_FACILITATOR = 'facilitator'; |
35
|
|
|
const ROLE_ADMINISTRATOR = 'administrator'; |
36
|
|
|
|
37
|
|
|
use HasInternalRemarks; |
38
|
|
|
use HasAddress; |
39
|
|
|
use HasNumericCode; |
40
|
|
|
use HasUrl; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var User |
44
|
|
|
*/ |
45
|
|
|
private static $currentUser; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set currently logged in user |
49
|
|
|
* WARNING: this method should only be called from \Application\Authentication\AuthenticationListener |
50
|
|
|
* |
51
|
|
|
* @param \Application\Model\User $user |
52
|
|
|
*/ |
53
|
91 |
|
public static function setCurrent(?self $user): void |
54
|
|
|
{ |
55
|
91 |
|
self::$currentUser = $user; |
56
|
|
|
|
57
|
|
|
// Initalize ACL filter with current user if a logged in one exists |
58
|
|
|
/** @var AclFilter $aclFilter */ |
59
|
91 |
|
$aclFilter = _em()->getFilters()->getFilter(AclFilter::class); |
60
|
91 |
|
$aclFilter->setUser($user); |
61
|
91 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns currently logged user or null |
65
|
|
|
* |
66
|
|
|
* @return null|self |
67
|
|
|
*/ |
68
|
54 |
|
public static function getCurrent(): ?self |
69
|
|
|
{ |
70
|
54 |
|
return self::$currentUser; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var null|string |
75
|
|
|
* |
76
|
|
|
* @ORM\Column(type="string", length=50, nullable=true, unique=true) |
77
|
|
|
*/ |
78
|
|
|
private $login; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
* @ORM\Column(type="string", length=191, options={"default" = ""}) |
83
|
|
|
*/ |
84
|
|
|
private $firstName = ''; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var string |
88
|
|
|
* @ORM\Column(type="string", length=191, options={"default" = ""}) |
89
|
|
|
*/ |
90
|
|
|
private $lastName = ''; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var string |
94
|
|
|
* |
95
|
|
|
* @ORM\Column(type="string", length=255) |
96
|
|
|
*/ |
97
|
|
|
private $password = ''; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var null|string |
101
|
|
|
* @ORM\Column(type="string", length=191, nullable=true, unique=true) |
102
|
|
|
*/ |
103
|
|
|
private $email; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var string |
107
|
|
|
* @ORM\Column(type="UserRole", options={"default" = User::ROLE_MEMBER}) |
108
|
|
|
*/ |
109
|
|
|
private $role = self::ROLE_MEMBER; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var null|Chronos |
113
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
114
|
|
|
*/ |
115
|
|
|
private $membershipBegin; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var null|Chronos |
119
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
120
|
|
|
*/ |
121
|
|
|
private $membershipEnd; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @var null|Chronos |
125
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
126
|
|
|
*/ |
127
|
|
|
private $subscriptionBegin; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var null|Product |
131
|
|
|
* |
132
|
|
|
* @ORM\ManyToOne(targetEntity="Product") |
133
|
|
|
* @ORM\JoinColumns({ |
134
|
|
|
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL") |
135
|
|
|
* }) |
136
|
|
|
*/ |
137
|
|
|
private $subscriptionLastNumber; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @var string |
141
|
|
|
* @ORM\Column(type="ProductType", nullable=true) |
142
|
|
|
*/ |
143
|
|
|
private $subscriptionType; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @var string |
147
|
|
|
* @ORM\Column(type="string", length=25, options={"default" = ""}) |
148
|
|
|
*/ |
149
|
|
|
private $phone = ''; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @var bool |
153
|
|
|
* @ORM\Column(type="boolean", options={"default" = 0}) |
154
|
|
|
*/ |
155
|
|
|
private $webTemporaryAccess = false; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @var null|string |
159
|
|
|
* @ORM\Column(type="string", length=32, nullable=true, unique=true) |
160
|
|
|
*/ |
161
|
|
|
private $token; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @var null|Chronos |
165
|
|
|
* |
166
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
167
|
|
|
*/ |
168
|
|
|
private $tokenCreationDate; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @var Collection |
172
|
|
|
* @ORM\ManyToMany(targetEntity="UserTag", mappedBy="users") |
173
|
|
|
*/ |
174
|
|
|
private $userTags; |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @var Collection |
178
|
|
|
* @ORM\ManyToMany(targetEntity="Session", mappedBy="facilitators") |
179
|
|
|
*/ |
180
|
|
|
private $sessions; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @var Collection |
184
|
|
|
* @ORM\OneToMany(targetEntity="Message", mappedBy="recipient") |
185
|
|
|
*/ |
186
|
|
|
private $messages; |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @var Collection |
190
|
|
|
* @ORM\OneToMany(targetEntity="User", mappedBy="owner") |
191
|
|
|
*/ |
192
|
|
|
private $users; |
193
|
21 |
|
|
194
|
|
|
/** |
195
|
21 |
|
* Constructor |
196
|
21 |
|
* |
197
|
21 |
|
* @param string $role role for new user |
198
|
21 |
|
*/ |
199
|
21 |
|
public function __construct(string $role = self::ROLE_MEMBER) |
200
|
|
|
{ |
201
|
|
|
$this->role = $role; |
202
|
|
|
$this->userTags = new ArrayCollection(); |
203
|
|
|
$this->sessions = new ArrayCollection(); |
204
|
|
|
$this->messages = new ArrayCollection(); |
205
|
|
|
$this->users = new ArrayCollection(); |
206
|
|
|
} |
207
|
|
|
|
208
|
2 |
|
/** |
209
|
|
|
* Set login (eg: johndoe) |
210
|
2 |
|
* |
211
|
2 |
|
* @API\Input(type="Login") |
212
|
|
|
* |
213
|
|
|
* @param string $login |
214
|
|
|
*/ |
215
|
|
|
public function setLogin(string $login): void |
216
|
|
|
{ |
217
|
|
|
$this->login = $login; |
218
|
|
|
} |
219
|
|
|
|
220
|
8 |
|
/** |
221
|
|
|
* Get login (eg: johndoe) |
222
|
8 |
|
* |
223
|
|
|
* @API\Field(type="?Login") |
224
|
|
|
* |
225
|
|
|
* @return null|string |
226
|
|
|
*/ |
227
|
|
|
public function getLogin(): ?string |
228
|
|
|
{ |
229
|
|
|
return $this->login; |
230
|
6 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Hash and change the user password |
234
|
6 |
|
* |
235
|
1 |
|
* @param string $password |
236
|
|
|
*/ |
237
|
|
|
public function setPassword(string $password): void |
238
|
6 |
|
{ |
239
|
|
|
// Ignore empty password that could be sent "by mistake" by the client |
240
|
6 |
|
// when agreeing to terms |
241
|
6 |
|
if ($password === '') { |
242
|
|
|
return; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$this->revokeToken(); |
246
|
|
|
|
247
|
|
|
$this->password = password_hash($password, PASSWORD_DEFAULT); |
248
|
|
|
} |
249
|
|
|
|
250
|
3 |
|
/** |
251
|
|
|
* Returns the hashed password |
252
|
3 |
|
* |
253
|
|
|
* @API\Exclude |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function getPassword(): string |
258
|
|
|
{ |
259
|
|
|
return $this->password; |
260
|
2 |
|
} |
261
|
|
|
|
262
|
2 |
|
/** |
263
|
2 |
|
* Set first name |
264
|
|
|
* |
265
|
|
|
* @param string $firstName |
266
|
|
|
*/ |
267
|
|
|
public function setFirstName($firstName): void |
268
|
|
|
{ |
269
|
|
|
$this->firstName = $firstName; |
270
|
4 |
|
} |
271
|
|
|
|
272
|
4 |
|
/** |
273
|
|
|
* Get first name |
274
|
|
|
* |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function getFirstName(): string |
278
|
|
|
{ |
279
|
|
|
return (string) $this->firstName; |
280
|
2 |
|
} |
281
|
|
|
|
282
|
2 |
|
/** |
283
|
2 |
|
* Set last name |
284
|
|
|
* |
285
|
|
|
* @param string $lastName |
286
|
|
|
*/ |
287
|
|
|
public function setLastName($lastName): void |
288
|
|
|
{ |
289
|
|
|
$this->lastName = $lastName; |
290
|
2 |
|
} |
291
|
|
|
|
292
|
2 |
|
/** |
293
|
|
|
* Get last name |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
|
|
public function getLastName(): string |
298
|
|
|
{ |
299
|
|
|
return (string) $this->lastName; |
300
|
2 |
|
} |
301
|
|
|
|
302
|
2 |
|
/** |
303
|
|
|
* Get full name |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function getName(): string |
308
|
|
|
{ |
309
|
|
|
return implode(' ', [$this->getFirstName(), $this->getLastName()]); |
310
|
|
|
} |
311
|
|
|
|
312
|
2 |
|
/** |
313
|
|
|
* Set email |
314
|
2 |
|
* |
315
|
2 |
|
* @API\Input(type="?Email") |
316
|
|
|
* |
317
|
|
|
* @param null|string $email |
318
|
|
|
*/ |
319
|
|
|
public function setEmail(?string $email): void |
320
|
|
|
{ |
321
|
|
|
$this->email = $email; |
322
|
|
|
} |
323
|
|
|
|
324
|
4 |
|
/** |
325
|
|
|
* Get email |
326
|
4 |
|
* |
327
|
|
|
* @API\Field(type="?Email") |
328
|
|
|
* |
329
|
|
|
* @return null|string |
330
|
|
|
*/ |
331
|
|
|
public function getEmail(): ?string |
332
|
|
|
{ |
333
|
|
|
return $this->email; |
334
|
37 |
|
} |
335
|
|
|
|
336
|
37 |
|
/** |
337
|
|
|
* Returns whether the user is administrator and thus have can do anything. |
338
|
|
|
* |
339
|
|
|
* @API\Field(type="Application\Api\Enum\UserRoleType") |
340
|
|
|
*/ |
341
|
|
|
public function getRole(): string |
342
|
|
|
{ |
343
|
|
|
return $this->role; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Sets the user role |
348
|
|
|
* |
349
|
|
|
* The current user is allowed to promote another user up to the same role as himself. So |
350
|
7 |
|
* a Responsible can promote a Member to Responsible. Or an Admin can promote a Individual to Admin. |
351
|
|
|
* |
352
|
7 |
|
* But the current user is **not** allowed to demote a user who has a higher role than himself. |
353
|
2 |
|
* That means that a Responsible cannot demote an Admin to Individual. |
354
|
|
|
* |
355
|
|
|
* @param string $role |
356
|
5 |
|
*/ |
357
|
|
|
public function setRole(string $role): void |
358
|
5 |
|
{ |
359
|
5 |
|
if ($role === $this->role) { |
360
|
5 |
|
return; |
361
|
5 |
|
} |
362
|
|
|
|
363
|
|
|
$currentRole = self::getCurrent() ? self::getCurrent()->getRole() : self::ROLE_ANONYMOUS; |
364
|
5 |
|
$orderedRoles = [ |
365
|
5 |
|
self::ROLE_ANONYMOUS, |
366
|
5 |
|
self::ROLE_MEMBER, |
367
|
5 |
|
self::ROLE_FACILITATOR, |
368
|
3 |
|
self::ROLE_ADMINISTRATOR, |
369
|
|
|
]; |
370
|
5 |
|
|
371
|
2 |
|
$newFound = false; |
372
|
|
|
$oldFound = false; |
373
|
|
|
foreach ($orderedRoles as $r) { |
374
|
5 |
|
if ($r === $this->role) { |
375
|
5 |
|
$oldFound = true; |
376
|
|
|
} |
377
|
|
|
if ($r === $role) { |
378
|
|
|
$newFound = true; |
379
|
5 |
|
} |
380
|
3 |
|
|
381
|
|
|
if ($r === $currentRole) { |
382
|
|
|
break; |
383
|
2 |
|
} |
384
|
2 |
|
} |
385
|
|
|
|
386
|
1 |
|
if (!$newFound || !$oldFound) { |
387
|
|
|
throw new Exception($currentRole . ' is not allowed to change role from ' . $this->role . ' to ' . $role); |
388
|
1 |
|
} |
389
|
1 |
|
|
390
|
|
|
$this->role = $role; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
public function initialize(): void |
394
|
|
|
{ |
395
|
|
|
$this->role = self::ROLE_MEMBER; // Bypass security |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
public function getPhone(): string |
402
|
|
|
{ |
403
|
|
|
return $this->phone; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @param string $phone |
408
|
|
|
*/ |
409
|
|
|
public function setPhone(string $phone): void |
410
|
1 |
|
{ |
411
|
|
|
$this->phone = $phone; |
412
|
1 |
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @return Collection |
416
|
|
|
*/ |
417
|
|
|
public function getUserTags(): Collection |
418
|
|
|
{ |
419
|
|
|
return $this->userTags; |
420
|
|
|
} |
421
|
1 |
|
|
422
|
|
|
/** |
423
|
1 |
|
* Notify the user that it has a new userTag. |
424
|
1 |
|
* This should only be called by UserTag::addUser() |
425
|
|
|
* |
426
|
|
|
* @param UserTag $userTag |
427
|
|
|
*/ |
428
|
|
|
public function userTagAdded(UserTag $userTag): void |
429
|
|
|
{ |
430
|
|
|
$this->userTags->add($userTag); |
431
|
|
|
} |
432
|
1 |
|
|
433
|
|
|
/** |
434
|
1 |
|
* Notify the user that a userTag was removed. |
435
|
1 |
|
* This should only be called by UserTag::removeFacilitator() |
436
|
|
|
* |
437
|
|
|
* @param UserTag $userTag |
438
|
|
|
*/ |
439
|
|
|
public function userTagRemoved(UserTag $userTag): void |
440
|
|
|
{ |
441
|
|
|
$this->userTags->removeElement($userTag); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @return Collection |
446
|
|
|
*/ |
447
|
|
|
public function getSessions(): Collection |
448
|
|
|
{ |
449
|
|
|
return $this->sessions; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Notify the user that it has a new session. |
454
|
|
|
* This should only be called by Session::addFacilitator() |
455
|
|
|
* |
456
|
|
|
* @param Session $session |
457
|
|
|
*/ |
458
|
|
|
public function sessionAdded(Session $session): void |
459
|
|
|
{ |
460
|
|
|
$this->sessions->add($session); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Notify the user that a session was removed. |
465
|
|
|
* This should only be called by Session::removeFacilitator() |
466
|
|
|
* |
467
|
|
|
* @param Session $session |
468
|
|
|
*/ |
469
|
|
|
public function sessionRemoved(Session $session): void |
470
|
|
|
{ |
471
|
|
|
$this->sessions->removeElement($session); |
472
|
1 |
|
} |
473
|
|
|
|
474
|
1 |
|
/** |
475
|
|
|
* @return null|Chronos |
476
|
|
|
*/ |
477
|
|
|
public function getMembershipBegin(): ?Chronos |
478
|
|
|
{ |
479
|
|
|
return $this->membershipBegin; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @param null|Chronos $membershipBegin |
484
|
|
|
*/ |
485
|
|
|
public function setMembershipBegin(?Chronos $membershipBegin): void |
486
|
|
|
{ |
487
|
|
|
$this->membershipBegin = $membershipBegin; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* @return null|Chronos |
492
|
|
|
*/ |
493
|
|
|
public function getMembershipEnd(): ?Chronos |
494
|
|
|
{ |
495
|
|
|
return $this->membershipEnd; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* @param null|Chronos $membershipEnd |
500
|
|
|
*/ |
501
|
|
|
public function setMembershipEnd(?Chronos $membershipEnd): void |
502
|
|
|
{ |
503
|
|
|
$this->membershipEnd = $membershipEnd; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* @return bool |
508
|
|
|
*/ |
509
|
|
|
public function getWebTemporaryAccess(): bool |
510
|
|
|
{ |
511
|
|
|
return $this->webTemporaryAccess; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* @param bool $webTemporaryAccess |
516
|
1 |
|
*/ |
517
|
|
|
public function setWebTemporaryAccess(bool $webTemporaryAccess): void |
518
|
1 |
|
{ |
519
|
|
|
$this->webTemporaryAccess = $webTemporaryAccess; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Get the first login date |
524
|
|
|
* |
525
|
|
|
* @return null|Chronos |
526
|
|
|
*/ |
527
|
4 |
|
public function getFirstLogin(): ?Chronos |
528
|
|
|
{ |
529
|
4 |
|
/** @var LogRepository $logRepository */ |
530
|
4 |
|
$logRepository = _em()->getRepository(Log::class); |
531
|
|
|
|
532
|
|
|
return $logRepository->getLoginDate($this, true); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* Get the last login date |
537
|
|
|
* |
538
|
1 |
|
* @return null|Chronos |
539
|
|
|
*/ |
540
|
1 |
|
public function getLastLogin(): ?Chronos |
541
|
1 |
|
{ |
542
|
|
|
/** @var LogRepository $logRepository */ |
543
|
|
|
$logRepository = _em()->getRepository(Log::class); |
544
|
|
|
|
545
|
|
|
return $logRepository->getLoginDate($this, false); |
546
|
3 |
|
} |
547
|
|
|
|
548
|
3 |
|
/** |
549
|
3 |
|
* Get messages sent to the user |
550
|
|
|
* |
551
|
3 |
|
* @return Collection |
552
|
|
|
*/ |
553
|
|
|
public function getMessages(): Collection |
554
|
|
|
{ |
555
|
|
|
return $this->messages; |
556
|
|
|
} |
557
|
6 |
|
|
558
|
|
|
/** |
559
|
6 |
|
* Notify the user that it has a new message |
560
|
6 |
|
* This should only be called by Message::setRecipient() |
561
|
6 |
|
* |
562
|
|
|
* @param Message $message |
563
|
|
|
*/ |
564
|
|
|
public function messageAdded(Message $message): void |
565
|
|
|
{ |
566
|
|
|
$this->messages->add($message); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
3 |
|
* Notify the user that a message was removed |
571
|
|
|
* This should only be called by Message::setRecipient() |
572
|
3 |
|
* |
573
|
1 |
|
* @param Message $message |
574
|
|
|
*/ |
575
|
|
|
public function messageRemoved(Message $message): void |
576
|
3 |
|
{ |
577
|
|
|
$this->messages->removeElement($message); |
578
|
3 |
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* Generate a new random token to reset password |
582
|
|
|
*/ |
583
|
|
|
public function createToken(): string |
584
|
|
|
{ |
585
|
|
|
$this->token = bin2hex(random_bytes(16)); |
586
|
|
|
$this->tokenCreationDate = new Chronos(); |
587
|
|
|
|
588
|
2 |
|
return $this->token; |
589
|
|
|
} |
590
|
2 |
|
|
591
|
2 |
|
/** |
592
|
2 |
|
* Destroy existing token |
593
|
|
|
*/ |
594
|
|
|
public function revokeToken(): void |
595
|
|
|
{ |
596
|
|
|
$this->token = null; |
597
|
|
|
$this->tokenCreationDate = null; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Check if token is valid. |
602
|
|
|
* |
603
|
|
|
* @API\Exclude |
604
|
|
|
* |
605
|
|
|
* @return bool |
606
|
|
|
*/ |
607
|
|
|
public function isTokenValid(): bool |
608
|
|
|
{ |
609
|
|
|
if (!$this->tokenCreationDate) { |
610
|
|
|
return false; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
$timeLimit = $this->tokenCreationDate->addMinutes(30); |
614
|
|
|
|
615
|
|
|
return $timeLimit->isFuture(); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
/** |
619
|
|
|
* Override parent to prevents users created from administration to be family of the administrator |
620
|
|
|
* |
621
|
|
|
* The owner must be explicitly set for all users. |
622
|
|
|
* |
623
|
|
|
* @ORM\PrePersist |
624
|
|
|
*/ |
625
|
|
|
public function timestampCreation(): void |
626
|
|
|
{ |
627
|
|
|
$this->setCreationDate(Utility::getNow()); |
628
|
|
|
$this->setCreator(self::getCurrent()); |
629
|
1 |
|
} |
630
|
|
|
|
631
|
1 |
|
/** |
632
|
|
|
* @return null|Chronos |
633
|
|
|
*/ |
634
|
|
|
public function getSubscriptionBegin(): ?Chronos |
635
|
|
|
{ |
636
|
|
|
return $this->subscriptionBegin; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* @param null|Chronos $subscriptionBegin |
641
|
|
|
*/ |
642
|
|
|
public function setSubscriptionBegin(?Chronos $subscriptionBegin): void |
643
|
|
|
{ |
644
|
|
|
$this->subscriptionBegin = $subscriptionBegin; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Set subscription type |
649
|
|
|
* |
650
|
|
|
* @API\Input(type="?ProductType") |
651
|
|
|
* |
652
|
|
|
* @param null|string $subscriptionType |
653
|
|
|
*/ |
654
|
|
|
public function setSubscriptionType(?string $subscriptionType): void |
655
|
|
|
{ |
656
|
|
|
$this->subscriptionType = $subscriptionType; |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Get subscription type |
661
|
|
|
* |
662
|
|
|
* @API\Field(type="ProductType") |
663
|
|
|
* |
664
|
|
|
* @return null|string |
665
|
|
|
*/ |
666
|
|
|
public function getSubscriptionType(): ?string |
667
|
|
|
{ |
668
|
|
|
return $this->subscriptionType; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* Get last subscription related product |
673
|
|
|
* |
674
|
|
|
* @return null|Product |
675
|
|
|
*/ |
676
|
|
|
public function getSubscriptionLastNumber(): ?Product |
677
|
|
|
{ |
678
|
|
|
return $this->subscriptionLastNumber; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Set related product as last subscribed one |
683
|
|
|
* |
684
|
|
|
* @param null|Product $subscriptionLastNumber |
685
|
|
|
*/ |
686
|
|
|
public function setSubscriptionLastNumber(?Product $subscriptionLastNumber): void |
687
|
|
|
{ |
688
|
|
|
$this->subscriptionLastNumber = $subscriptionLastNumber; |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
|