|
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\ORM\Query\Filter\AclFilter; |
|
10
|
|
|
use Application\Traits\HasName; |
|
11
|
|
|
use Application\Traits\HasResponsible; |
|
12
|
|
|
use Application\Utility; |
|
13
|
|
|
use Cake\Chronos\Chronos; |
|
14
|
|
|
use Cake\Chronos\Date; |
|
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
16
|
|
|
use Doctrine\Common\Collections\Collection; |
|
17
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
18
|
|
|
use GraphQL\Doctrine\Annotation as API; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* User |
|
22
|
|
|
* |
|
23
|
|
|
* @ORM\Entity(repositoryClass="Application\Repository\UserRepository") |
|
24
|
|
|
*/ |
|
25
|
|
|
class User extends AbstractModel |
|
26
|
|
|
{ |
|
27
|
|
|
const ROLE_ANONYMOUS = 'anonymous'; |
|
28
|
|
|
const ROLE_MEMBER = 'member'; |
|
29
|
|
|
const ROLE_ADMINISTRATOR = 'administrator'; |
|
30
|
|
|
|
|
31
|
|
|
use HasName; |
|
32
|
|
|
use HasResponsible; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var User |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $currentUser; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set currently logged in user |
|
41
|
|
|
* WARNING: this method should only be called from \Application\Authentication\AuthenticationListener |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Application\Model\User $user |
|
44
|
|
|
*/ |
|
45
|
24 |
|
public static function setCurrent(?self $user): void |
|
46
|
|
|
{ |
|
47
|
24 |
|
self::$currentUser = $user; |
|
48
|
|
|
|
|
49
|
|
|
// Initalize ACL filter with current user if a logged in one exists |
|
50
|
24 |
|
_em()->getFilters()->getFilter(AclFilter::class)->setUser($user); |
|
51
|
24 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns currently logged user or null |
|
55
|
|
|
* |
|
56
|
|
|
* @return null|self |
|
57
|
|
|
*/ |
|
58
|
19 |
|
public static function getCurrent(): ?self |
|
59
|
|
|
{ |
|
60
|
19 |
|
return self::$currentUser; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
* |
|
66
|
|
|
* @ORM\Column(type="string", length=50, unique=true) |
|
67
|
|
|
*/ |
|
68
|
|
|
private $login = ''; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var string |
|
72
|
|
|
* |
|
73
|
|
|
* @ORM\Column(type="string", length=255) |
|
74
|
|
|
*/ |
|
75
|
|
|
private $password = ''; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string |
|
79
|
|
|
* @ORM\Column(type="string", length=191) |
|
80
|
|
|
*/ |
|
81
|
|
|
private $email; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var string |
|
85
|
|
|
* @ORM\Column(type="UserRole", options={"default" = User::ROLE_MEMBER}) |
|
86
|
|
|
*/ |
|
87
|
|
|
private $role = self::ROLE_MEMBER; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var Chronos |
|
91
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
|
92
|
|
|
*/ |
|
93
|
|
|
private $activeUntil; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var string |
|
97
|
|
|
* @ORM\Column(type="string", length=25, options={"default" = ""}) |
|
98
|
|
|
*/ |
|
99
|
|
|
private $phone = ''; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var null|Date |
|
103
|
|
|
* @ORM\Column(type="date", nullable=true) |
|
104
|
|
|
*/ |
|
105
|
|
|
private $birthday; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var Collection |
|
109
|
|
|
* @ORM\OneToMany(targetEntity="Booking", mappedBy="responsible") |
|
110
|
|
|
*/ |
|
111
|
|
|
private $bookings; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Constructor |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $role role for new user |
|
117
|
|
|
*/ |
|
118
|
17 |
|
public function __construct(string $role = self::ROLE_MEMBER) |
|
119
|
|
|
{ |
|
120
|
17 |
|
$this->role = $role; |
|
121
|
17 |
|
$this->bookings = new ArrayCollection(); |
|
122
|
17 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Set login (eg: johndoe) |
|
126
|
|
|
* |
|
127
|
|
|
* @API\Input(type="Application\Api\Scalar\LoginType") |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $login |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setLogin(string $login): void |
|
132
|
|
|
{ |
|
133
|
|
|
$this->login = $login; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get login (eg: johndoe) |
|
138
|
|
|
* |
|
139
|
|
|
* @API\Field(type="Application\Api\Scalar\LoginType") |
|
140
|
|
|
* |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function getLogin(): string |
|
144
|
|
|
{ |
|
145
|
1 |
|
return $this->login; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Encrypt and change the user password |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $password |
|
152
|
|
|
*/ |
|
153
|
3 |
|
public function setPassword(string $password): void |
|
154
|
|
|
{ |
|
155
|
|
|
// Ignore empty password that could be sent "by mistake" by the client |
|
156
|
|
|
// when agreeing to terms |
|
157
|
3 |
|
if ($password === '') { |
|
158
|
1 |
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
$this->password = password_hash($password, PASSWORD_DEFAULT); |
|
162
|
3 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the hashed password |
|
166
|
|
|
* |
|
167
|
|
|
* @API\Exclude |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
3 |
|
public function getPassword(): string |
|
172
|
|
|
{ |
|
173
|
3 |
|
return $this->password; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set email |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $email |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function setEmail(string $email): void |
|
182
|
|
|
{ |
|
183
|
1 |
|
$this->email = $email; |
|
184
|
1 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get email |
|
188
|
|
|
* |
|
189
|
|
|
* @API\Field(type="Email") |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function getEmail(): string |
|
194
|
|
|
{ |
|
195
|
1 |
|
return $this->email; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Returns whether the user is administrator and thus have can do anything. |
|
200
|
|
|
* |
|
201
|
|
|
* @API\Field(type="Application\Api\Enum\UserRoleType") |
|
202
|
|
|
*/ |
|
203
|
14 |
|
public function getRole(): string |
|
204
|
|
|
{ |
|
205
|
14 |
|
return $this->role; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Sets the user role |
|
210
|
|
|
* |
|
211
|
|
|
* The current user is allowed to promote another user up to the same role as himself. So |
|
212
|
|
|
* a Senior can promote a Student to Senior. Or an Admin can promote a Junior to Admin. |
|
213
|
|
|
* |
|
214
|
|
|
* But the current user is **not** allowed to demote a user who has a higher role than himself. |
|
215
|
|
|
* That means that a Senior cannot demote an Admin to Student. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $role |
|
218
|
|
|
*/ |
|
219
|
7 |
|
public function setRole(string $role): void |
|
220
|
|
|
{ |
|
221
|
7 |
|
if ($role === $this->role) { |
|
222
|
2 |
|
return; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
5 |
|
$currentRole = self::getCurrent() ? self::getCurrent()->getRole() : self::ROLE_ANONYMOUS; |
|
226
|
|
|
$orderedRoles = [ |
|
227
|
5 |
|
self::ROLE_ANONYMOUS, |
|
228
|
5 |
|
self::ROLE_MEMBER, |
|
229
|
5 |
|
self::ROLE_ADMINISTRATOR, |
|
230
|
|
|
]; |
|
231
|
|
|
|
|
232
|
5 |
|
$newFound = false; |
|
233
|
5 |
|
$oldFound = false; |
|
234
|
5 |
|
foreach ($orderedRoles as $r) { |
|
235
|
5 |
|
if ($r === $this->role) { |
|
236
|
3 |
|
$oldFound = true; |
|
237
|
|
|
} |
|
238
|
5 |
|
if ($r === $role) { |
|
239
|
2 |
|
$newFound = true; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
5 |
|
if ($r === $currentRole) { |
|
243
|
5 |
|
break; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
5 |
|
if (!$newFound || !$oldFound) { |
|
248
|
3 |
|
throw new Exception($currentRole . ' is not allowed to change role to ' . $role); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
2 |
|
$this->role = $role; |
|
252
|
2 |
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* The date until the user is active. Or `null` if there is not limit in time |
|
256
|
|
|
* |
|
257
|
|
|
* @return null|Chronos |
|
258
|
|
|
*/ |
|
259
|
5 |
|
public function getActiveUntil(): ?Chronos |
|
260
|
|
|
{ |
|
261
|
5 |
|
return $this->activeUntil; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* The date until the user is active. Or `null` if there is not limit in time |
|
266
|
|
|
* |
|
267
|
|
|
* @param null|Chronos $activeUntil |
|
268
|
|
|
*/ |
|
269
|
2 |
|
public function setActiveUntil(?Chronos $activeUntil): void |
|
270
|
|
|
{ |
|
271
|
2 |
|
$this->activeUntil = $activeUntil; |
|
272
|
2 |
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Get a list of global permissions for this user |
|
276
|
|
|
* |
|
277
|
|
|
* @API\Field(type="GlobalPermissionsList") |
|
278
|
|
|
* |
|
279
|
|
|
* @return array |
|
280
|
|
|
*/ |
|
281
|
3 |
|
public function getGlobalPermissions(): array |
|
282
|
|
|
{ |
|
283
|
3 |
|
$acl = new Acl(); |
|
284
|
|
|
$types = [ |
|
285
|
3 |
|
Country::class, |
|
286
|
|
|
Tag::class, |
|
287
|
|
|
self::class, |
|
288
|
|
|
]; |
|
289
|
|
|
|
|
290
|
3 |
|
$permissions = ['create']; |
|
291
|
3 |
|
$result = []; |
|
292
|
|
|
|
|
293
|
3 |
|
$previousUser = self::getCurrent(); |
|
294
|
3 |
|
self::setCurrent($this); |
|
295
|
3 |
|
foreach ($types as $type) { |
|
296
|
3 |
|
$instance = new $type(); |
|
297
|
3 |
|
$sh = lcfirst(Utility::getShortClassName($instance)); |
|
298
|
3 |
|
$result[$sh] = []; |
|
299
|
|
|
|
|
300
|
3 |
|
foreach ($permissions as $p) { |
|
301
|
3 |
|
$result[$sh][$p] = $acl->isCurrentUserAllowed($instance, $p); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
3 |
|
self::setCurrent($previousUser); |
|
306
|
|
|
|
|
307
|
3 |
|
return $result; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @return string |
|
312
|
|
|
*/ |
|
313
|
|
|
public function getPhone(): string |
|
314
|
|
|
{ |
|
315
|
|
|
return $this->phone; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @param string $phone |
|
320
|
|
|
*/ |
|
321
|
|
|
public function setPhone(string $phone): void |
|
322
|
|
|
{ |
|
323
|
|
|
$this->phone = $phone; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* @return null|Date |
|
328
|
|
|
*/ |
|
329
|
|
|
public function getBirthday(): ?Date |
|
330
|
|
|
{ |
|
331
|
|
|
return $this->birthday; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @param null|Date $birthday |
|
336
|
|
|
*/ |
|
337
|
|
|
public function setBirthday(?Date $birthday): void |
|
338
|
|
|
{ |
|
339
|
|
|
$this->birthday = $birthday; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Get bookings |
|
344
|
|
|
* |
|
345
|
|
|
* @return Collection |
|
346
|
|
|
*/ |
|
347
|
|
|
public function getBookings(): Collection |
|
348
|
|
|
{ |
|
349
|
|
|
return $this->bookings; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Notify the user that it has a new booking. |
|
354
|
|
|
* This should only be called by Booking::setResponsible() |
|
355
|
|
|
* |
|
356
|
|
|
* @param Booking $booking |
|
357
|
|
|
*/ |
|
358
|
|
|
public function bookingAdded(Booking $booking): void |
|
359
|
|
|
{ |
|
360
|
|
|
$this->bookings->add($booking); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|