1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
6
|
|
|
* @license MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Auth\Entity; |
10
|
|
|
|
11
|
|
|
use Core\Entity\AbstractIdentifiableEntity; |
12
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
13
|
|
|
use Laminas\Session\Container as Session; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* An Anonymous user. |
17
|
|
|
* |
18
|
|
|
* Anonymous user may not be persisted in the database and is used solely |
19
|
|
|
* for identifying an user through subsequential requests by a session variable. |
20
|
|
|
* |
21
|
|
|
* @author Mathias Gelhausen <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @ODM\Document(collection="users", repositoryClass="Auth\Repository\User") |
24
|
|
|
* @ODM\HasLifecycleCallbacks |
25
|
|
|
*/ |
26
|
|
|
class AnonymousUser extends User |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Unique identification key (session-based) |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $token; |
34
|
|
|
|
35
|
|
|
public function __construct(?string $token = null) |
36
|
|
|
{ |
37
|
|
|
$this->token = $token; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Prevents this entity from persistence. |
42
|
|
|
* |
43
|
|
|
* This is a Doctrine Hook which throws an exception when called. |
44
|
1 |
|
* |
45
|
|
|
* @throws \RuntimeException |
46
|
1 |
|
* @ODM\PrePersist |
47
|
|
|
* @ODM\PreUpdate |
48
|
|
|
*/ |
49
|
|
|
public function preventPersistence() |
50
|
|
|
{ |
51
|
|
|
throw new \RuntimeException('Anonymous users may not be persisted.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
/** |
55
|
|
|
* Gets the anonymous identification key. |
56
|
1 |
|
* |
57
|
1 |
|
* @return string |
58
|
1 |
|
*/ |
59
|
1 |
|
public function getToken() |
60
|
|
|
{ |
61
|
1 |
|
if (!$this->token) { |
62
|
|
|
$session = new Session('Auth'); |
63
|
1 |
|
if (!$session->token) { |
64
|
|
|
$session->token = uniqid(); |
65
|
|
|
} |
66
|
|
|
$this->token = $session->token; |
67
|
|
|
} |
68
|
|
|
return $this->token; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
1 |
|
* |
74
|
|
|
* Normalizes the token for use in Permission Entity |
75
|
1 |
|
* |
76
|
1 |
|
* @see \Core\Entity\AbstractIdentifiableEntity::getId() |
77
|
|
|
*/ |
78
|
1 |
|
public function getId() |
79
|
|
|
{ |
80
|
|
|
if (!$this->id) { |
81
|
|
|
$this->setId('token:' . $this->getToken()); |
82
|
|
|
} |
83
|
|
|
return $this->id; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
3 |
|
* |
89
|
|
|
* Always returns "guest" |
90
|
3 |
|
* |
91
|
|
|
* @see \Auth\Entity\User::getRole() |
92
|
|
|
*/ |
93
|
|
|
public function getRole() |
94
|
|
|
{ |
95
|
|
|
return "guest"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
* |
101
|
|
|
* Disabled. Simply returns this instance. |
102
|
|
|
* |
103
|
|
|
* @see \Auth\Entity\User::setPassword() |
104
|
|
|
*/ |
105
|
|
|
public function setPassword($password) |
106
|
|
|
{ |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
1 |
|
* |
113
|
|
|
* Disabled. Simply returns this instance. |
114
|
1 |
|
* |
115
|
|
|
* @see \Auth\Entity\User::setCredential() |
116
|
|
|
*/ |
117
|
|
|
public function setCredential($credential) |
118
|
|
|
{ |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
1 |
|
* |
125
|
|
|
* Disabled. Simply returns this instance. |
126
|
1 |
|
* |
127
|
|
|
* @see \Auth\Entity\User::setSecret() |
128
|
|
|
*/ |
129
|
|
|
public function setSecret($secret) |
130
|
|
|
{ |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|