1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2015 Michael Giesler, Stephan Kreutzer |
3
|
|
|
* |
4
|
|
|
* This file is part of Dembelo. |
5
|
|
|
* |
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License 3 for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
namespace DembeloMain\Document; |
20
|
|
|
|
21
|
|
|
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique; |
22
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; |
23
|
|
|
use Exception; |
24
|
|
|
use Symfony\Component\Security\Core\User\AdvancedUserInterface; |
25
|
|
|
use Symfony\Component\Security\Core\User\EquatableInterface; |
26
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
27
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class User |
31
|
|
|
* |
32
|
|
|
* @MongoDB\Document |
33
|
|
|
* |
34
|
|
|
* @MongoDBUnique(fields="email") |
35
|
|
|
* |
36
|
|
|
* @MongoDB\Document(repositoryClass="\DembeloMain\Model\Repository\Doctrine\ODM\UserRepository") |
37
|
|
|
*/ |
38
|
|
|
class User implements UserInterface, \Serializable, AdvancedUserInterface, EquatableInterface |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @MongoDB\Id |
42
|
|
|
*/ |
43
|
|
|
protected $id; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @MongoDB\Field(type="string") |
47
|
|
|
* |
48
|
|
|
* @Assert\NotBlank() |
49
|
|
|
* @Assert\Email() |
50
|
|
|
*/ |
51
|
|
|
protected $email; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @MongoDB\Field(type="string") |
55
|
|
|
* |
56
|
|
|
* @Assert\NotBlank() |
57
|
|
|
*/ |
58
|
|
|
protected $password; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @MongoDB\Field(type="collection") |
62
|
|
|
* |
63
|
|
|
* @Assert\NotBlank() |
64
|
|
|
*/ |
65
|
|
|
protected $roles; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @MongoDB\Field(type="object_id") |
69
|
|
|
*/ |
70
|
|
|
protected $licenseeId; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @MongoDB\Field(type="object_id") |
74
|
|
|
*/ |
75
|
|
|
protected $currentTextnode; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @MongoDB\Field(type="string") |
79
|
|
|
*/ |
80
|
|
|
protected $gender; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @MongoDB\Field(type="string") |
84
|
|
|
*/ |
85
|
|
|
protected $source; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @MongoDB\Field(type="string") |
89
|
|
|
*/ |
90
|
|
|
protected $reason; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @MongoDB\Field(type="int") |
94
|
|
|
* |
95
|
|
|
* @Assert\NotBlank() |
96
|
|
|
*/ |
97
|
|
|
protected $status; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @MongoDB\Field(type="string") |
101
|
|
|
*/ |
102
|
|
|
protected $activationHash; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @MongoDB\Field(type="hash") |
106
|
|
|
*/ |
107
|
|
|
protected $metadata; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @MongoDB\Field(type="object_id") |
111
|
|
|
*/ |
112
|
|
|
protected $lastTopicId; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @MongoDB\Field(type="hash") |
116
|
|
|
*/ |
117
|
|
|
protected $favorites = []; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* gets the mongodb id |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
6 |
|
public function getId() |
125
|
|
|
{ |
126
|
6 |
|
return $this->id; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* sets the mongoDB id |
131
|
|
|
* |
132
|
|
|
* @param string $id |
133
|
|
|
*/ |
134
|
4 |
|
public function setId($id) |
135
|
|
|
{ |
136
|
4 |
|
$this->id = $id; |
137
|
4 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* gets the email |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
5 |
|
public function getEmail() |
145
|
|
|
{ |
146
|
5 |
|
return $this->email; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* sets the usermail, used for security |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
1 |
|
public function getUsername() |
155
|
|
|
{ |
156
|
1 |
|
return $this->getEmail(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* sets the email used as username |
161
|
|
|
* |
162
|
|
|
* @param string $email |
163
|
|
|
*/ |
164
|
7 |
|
public function setEmail($email) |
165
|
|
|
{ |
166
|
7 |
|
$this->email = $email; |
167
|
7 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* gets the password |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
4 |
|
public function getPassword() |
175
|
|
|
{ |
176
|
4 |
|
return $this->password; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* sets the password |
181
|
|
|
* |
182
|
|
|
* @param string $password |
183
|
|
|
*/ |
184
|
5 |
|
public function setPassword($password) |
185
|
|
|
{ |
186
|
5 |
|
$this->password = $password; |
187
|
5 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* from UserInterface, not needed for our encoder |
191
|
|
|
* |
192
|
|
|
* @return null |
193
|
|
|
*/ |
194
|
1 |
|
public function getSalt() |
195
|
|
|
{ |
196
|
1 |
|
return null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* gets the user's roles |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
3 |
|
public function getRoles() |
205
|
|
|
{ |
206
|
3 |
|
return $this->roles; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* sets the roles |
211
|
|
|
* |
212
|
|
|
* @param array|string $roles |
213
|
|
|
*/ |
214
|
6 |
|
public function setRoles($roles) |
215
|
|
|
{ |
216
|
6 |
|
if (!is_array($roles)) { |
217
|
2 |
|
$roles = array($roles); |
218
|
|
|
} |
219
|
6 |
|
$this->roles = $roles; |
220
|
6 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Gets the last textnode ID of topic \p $topicId the user was reading. |
224
|
|
|
* |
225
|
|
|
* @return string|null Textnode ID or null, if there wasn't a current |
226
|
|
|
* textnode ID set so far. |
227
|
|
|
*/ |
228
|
2 |
|
public function getCurrentTextnode() |
229
|
|
|
{ |
230
|
2 |
|
return $this->currentTextnode; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Saves the ID of the textnode the user is currently |
235
|
|
|
* reading. |
236
|
|
|
* |
237
|
|
|
* @param string $textnodeId ID of the textnode the user is |
238
|
|
|
* currently reading. |
239
|
|
|
*/ |
240
|
2 |
|
public function setCurrentTextnode($textnodeId) |
241
|
|
|
{ |
242
|
2 |
|
$this->currentTextnode = $textnodeId; |
243
|
2 |
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* from UserInterface |
247
|
|
|
*/ |
248
|
1 |
|
public function eraseCredentials() |
249
|
|
|
{ |
250
|
1 |
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* serializes the object |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
1 |
|
public function serialize() |
258
|
|
|
{ |
259
|
1 |
|
return serialize(array( |
260
|
1 |
|
$this->id, |
261
|
1 |
|
$this->email, |
262
|
1 |
|
$this->password, |
263
|
|
|
// see section on salt below |
264
|
|
|
// $this->salt, |
265
|
1 |
|
$this->currentTextnode, |
266
|
|
|
)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* unserializes the object |
271
|
|
|
* |
272
|
|
|
* @param string $serialized |
273
|
|
|
*/ |
274
|
1 |
|
public function unserialize($serialized) |
275
|
|
|
{ |
276
|
|
|
[ |
277
|
1 |
|
$this->id, |
278
|
1 |
|
$this->email, |
279
|
1 |
|
$this->password, |
280
|
|
|
// see section on salt below |
281
|
|
|
// $this->salt, |
282
|
1 |
|
$this->currentTextnode, |
283
|
1 |
|
] = unserialize($serialized); |
284
|
1 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* sets the licensee id |
288
|
|
|
* |
289
|
|
|
* @param string $id licensee ID |
290
|
|
|
*/ |
291
|
2 |
|
public function setLicenseeId($id) |
292
|
|
|
{ |
293
|
2 |
|
$this->licenseeId = $id; |
294
|
2 |
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* gets the licensee id |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
2 |
|
public function getLicenseeId() |
302
|
|
|
{ |
303
|
2 |
|
return $this->licenseeId; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* sets the gender |
308
|
|
|
* |
309
|
|
|
* @param string $gender Gender |
310
|
|
|
*/ |
311
|
2 |
|
public function setGender($gender) |
312
|
|
|
{ |
313
|
2 |
|
$this->gender = $gender; |
314
|
2 |
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* gets the gender |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
2 |
|
public function getGender() |
322
|
|
|
{ |
323
|
2 |
|
return $this->gender; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* gets the source from where the user came to this site |
328
|
|
|
* |
329
|
|
|
* @return String |
330
|
|
|
*/ |
331
|
2 |
|
public function getSource() |
332
|
|
|
{ |
333
|
2 |
|
return $this->source; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* sets the source from where the user came to this site |
338
|
|
|
* |
339
|
|
|
* @param String $source |
340
|
|
|
*/ |
341
|
2 |
|
public function setSource($source) |
342
|
|
|
{ |
343
|
2 |
|
$this->source = $source; |
344
|
2 |
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* gets the reason for registration |
348
|
|
|
* |
349
|
|
|
* @return String |
350
|
|
|
*/ |
351
|
2 |
|
public function getReason() |
352
|
|
|
{ |
353
|
2 |
|
return $this->reason; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* sets the reason for registration |
358
|
|
|
* |
359
|
|
|
* @param String $reason |
360
|
|
|
*/ |
361
|
2 |
|
public function setReason($reason) |
362
|
|
|
{ |
363
|
2 |
|
$this->reason = $reason; |
364
|
2 |
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* gets status |
368
|
|
|
* |
369
|
|
|
* @return integer |
370
|
|
|
*/ |
371
|
2 |
|
public function getStatus() |
372
|
|
|
{ |
373
|
2 |
|
return $this->status; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* sets status |
378
|
|
|
* |
379
|
|
|
* @param integer $status status |
380
|
|
|
*/ |
381
|
6 |
|
public function setStatus($status) |
382
|
|
|
{ |
383
|
6 |
|
$this->status = $status; |
384
|
6 |
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* gets activation hash |
388
|
|
|
* |
389
|
|
|
* @return mixed |
390
|
|
|
*/ |
391
|
1 |
|
public function getActivationHash() |
392
|
|
|
{ |
393
|
1 |
|
return $this->activationHash; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* sets activation hash |
398
|
|
|
* |
399
|
|
|
* @param String $hash activation hash |
400
|
|
|
*/ |
401
|
1 |
|
public function setActivationHash($hash) |
402
|
|
|
{ |
403
|
1 |
|
$this->activationHash = $hash; |
404
|
1 |
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* checks if account is not expired |
408
|
|
|
* |
409
|
|
|
* @return bool |
410
|
|
|
*/ |
411
|
1 |
|
public function isAccountNonExpired() |
412
|
|
|
{ |
413
|
1 |
|
return true; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* checks if account is not locked |
418
|
|
|
* |
419
|
|
|
* @return bool |
420
|
|
|
*/ |
421
|
1 |
|
public function isAccountNonLocked() |
422
|
|
|
{ |
423
|
1 |
|
return true; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* checks if credentials are not expired |
428
|
|
|
* @return bool |
429
|
|
|
*/ |
430
|
1 |
|
public function isCredentialsNonExpired() |
431
|
|
|
{ |
432
|
1 |
|
return true; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* checks if enabled |
437
|
|
|
* |
438
|
|
|
* @return bool |
439
|
|
|
*/ |
440
|
3 |
|
public function isEnabled() |
441
|
|
|
{ |
442
|
3 |
|
return $this->status === 1; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* gets the metadata |
447
|
|
|
* @return array |
448
|
|
|
*/ |
449
|
1 |
|
public function getMetadata() |
450
|
|
|
{ |
451
|
1 |
|
return $this->metadata; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* sets the metadata |
456
|
|
|
* @param array|string $metadata |
457
|
|
|
* @param string $value |
458
|
|
|
* |
459
|
|
|
* @throws Exception |
460
|
|
|
*/ |
461
|
3 |
|
public function setMetadata($metadata, $value = null) |
462
|
|
|
{ |
463
|
3 |
|
if (is_array($metadata) && null === $value) { |
464
|
1 |
|
$this->metadata = $metadata; |
465
|
2 |
|
} elseif (is_string($metadata) && null !== $value) { |
466
|
2 |
|
$this->metadata[$metadata] = $value; |
467
|
|
|
} else { |
468
|
|
|
throw new Exception('invalid data'); |
469
|
|
|
} |
470
|
3 |
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* sets the last topic id this user selected |
474
|
|
|
* |
475
|
|
|
* @param string $lastTopicId |
476
|
|
|
*/ |
477
|
1 |
|
public function setLastTopicId($lastTopicId) |
478
|
|
|
{ |
479
|
1 |
|
$this->lastTopicId = $lastTopicId; |
480
|
1 |
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* gets the last topic id this user selected |
484
|
|
|
* @return string |
485
|
|
|
*/ |
486
|
1 |
|
public function getLastTopicId() |
487
|
|
|
{ |
488
|
1 |
|
return $this->lastTopicId; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* sets favorite textnode for topic |
493
|
|
|
* @param String $topicId |
494
|
|
|
* @param String $textnodeId |
495
|
|
|
*/ |
496
|
2 |
|
public function setFavorite($topicId, $textnodeId) |
497
|
|
|
{ |
498
|
2 |
|
$this->favorites[$topicId] = $textnodeId; |
499
|
2 |
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* gets favorite textnode for topic |
503
|
|
|
* @param String $topicId |
504
|
|
|
* |
505
|
|
|
* @return null|String |
506
|
|
|
*/ |
507
|
2 |
|
public function getFavorite($topicId) |
508
|
|
|
{ |
509
|
2 |
|
if (!isset($this->favorites[$topicId])) { |
510
|
|
|
return null; |
511
|
|
|
} |
512
|
|
|
|
513
|
2 |
|
return $this->favorites[$topicId]; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* The equality comparison should neither be done by referential equality |
518
|
|
|
* nor by comparing identities (i.e. getId() === getId()). |
519
|
|
|
* |
520
|
|
|
* However, you do not need to compare every attribute, but only those that |
521
|
|
|
* are relevant for assessing whether re-authentication is required. |
522
|
|
|
* |
523
|
|
|
* Also implementation should consider that $user instance may implement |
524
|
|
|
* the extended user interface `AdvancedUserInterface`. |
525
|
|
|
* |
526
|
|
|
* @param UserInterface $user |
527
|
|
|
* |
528
|
|
|
* @return bool |
529
|
|
|
*/ |
530
|
|
|
public function isEqualTo(UserInterface $user): bool |
531
|
|
|
{ |
532
|
|
|
if ($this->getPassword() !== $user->getPassword()) { |
533
|
|
|
return false; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
if ($this->getUsername() !== $user->getUsername()) { |
537
|
|
|
return false; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
return true; |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
|