1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the User class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\Security; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\User\User as APIUser; |
12
|
|
|
use eZ\Publish\Core\Repository\Values\User\UserReference; |
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface; |
14
|
|
|
use Symfony\Component\Security\Core\User\EquatableInterface; |
15
|
|
|
|
16
|
|
|
class User implements ReferenceUserInterface, EquatableInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \eZ\Publish\API\Repository\Values\User\User |
20
|
|
|
*/ |
21
|
|
|
private $user; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \eZ\Publish\API\Repository\Values\User\UserReference |
25
|
|
|
*/ |
26
|
|
|
private $reference; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $roles; |
32
|
|
|
|
33
|
|
|
public function __construct(APIUser $user = null, array $roles = array()) |
34
|
|
|
{ |
35
|
|
|
$this->user = $user; |
36
|
|
|
$this->reference = new UserReference($user ? $user->getUserId() : null); |
37
|
|
|
$this->roles = $roles; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the roles granted to the user. |
42
|
|
|
* |
43
|
|
|
* <code> |
44
|
|
|
* public function getRoles() |
45
|
|
|
* { |
46
|
|
|
* return array( 'ROLE_USER' ); |
47
|
|
|
* } |
48
|
|
|
* </code> |
49
|
|
|
* |
50
|
|
|
* Alternatively, the roles might be stored on a ``roles`` property, |
51
|
|
|
* and populated in any number of different ways when the user object |
52
|
|
|
* is created. |
53
|
|
|
* |
54
|
|
|
* @return Role[] The user roles |
55
|
|
|
*/ |
56
|
|
|
public function getRoles() |
57
|
|
|
{ |
58
|
|
|
return $this->roles; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the password used to authenticate the user. |
63
|
|
|
* |
64
|
|
|
* This should be the encoded password. On authentication, a plain-text |
65
|
|
|
* password will be salted, encoded, and then compared to this value. |
66
|
|
|
* |
67
|
|
|
* @return string The password |
68
|
|
|
*/ |
69
|
|
|
public function getPassword() |
70
|
|
|
{ |
71
|
|
|
return $this->getAPIUser()->passwordHash; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the salt that was originally used to encode the password. |
76
|
|
|
* |
77
|
|
|
* This can return null if the password was not encoded using a salt. |
78
|
|
|
* |
79
|
|
|
* @return string The salt |
80
|
|
|
*/ |
81
|
|
|
public function getSalt() |
82
|
|
|
{ |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the username used to authenticate the user. |
88
|
|
|
* |
89
|
|
|
* @return string The username |
90
|
|
|
*/ |
91
|
|
|
public function getUsername() |
92
|
|
|
{ |
93
|
|
|
return $this->getAPIUser()->login; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Removes sensitive data from the user. |
98
|
|
|
* |
99
|
|
|
* This is important if, at any given point, sensitive information like |
100
|
|
|
* the plain-text password is stored on this object. |
101
|
|
|
*/ |
102
|
|
|
public function eraseCredentials() |
103
|
|
|
{ |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\UserReference |
108
|
|
|
*/ |
109
|
|
|
public function getAPIUserReference() |
110
|
|
|
{ |
111
|
|
|
return $this->reference; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\User |
116
|
|
|
*/ |
117
|
|
|
public function getAPIUser() |
118
|
|
|
{ |
119
|
|
|
if (!$this->user instanceof APIUser) { |
120
|
|
|
throw new \LogicException( |
121
|
|
|
'Attempts to get APIUser before it has been set by UserProvider, APIUser is not serialized to session' |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->user; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\User $user |
130
|
|
|
*/ |
131
|
|
|
public function setAPIUser(APIUser $user) |
132
|
|
|
{ |
133
|
|
|
$this->user = $user; |
134
|
|
|
$this->reference = new UserReference($user->getUserId()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function isEqualTo(BaseUserInterface $user) |
138
|
|
|
{ |
139
|
|
|
// Check for the lighter ReferenceUserInterface first |
140
|
|
|
if ($user instanceof ReferenceUserInterface) { |
141
|
|
|
return $user->getAPIUserReference()->getUserId() === $this->reference->getUserId(); |
142
|
|
|
} elseif ($user instanceof UserInterface) { |
143
|
|
|
return $user->getAPIUser()->getUserId() === $this->reference->getUserId(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function __toString() |
150
|
|
|
{ |
151
|
|
|
return $this->getAPIUser()->contentInfo->name; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Checks whether the user's account has expired. |
156
|
|
|
* |
157
|
|
|
* Internally, if this method returns false, the authentication system |
158
|
|
|
* will throw an AccountExpiredException and prevent login. |
159
|
|
|
* |
160
|
|
|
* @return bool true if the user's account is non expired, false otherwise |
161
|
|
|
* |
162
|
|
|
* @see AccountExpiredException |
163
|
|
|
*/ |
164
|
|
|
public function isAccountNonExpired() |
165
|
|
|
{ |
166
|
|
|
return $this->getAPIUser()->enabled; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Checks whether the user is locked. |
171
|
|
|
* |
172
|
|
|
* Internally, if this method returns false, the authentication system |
173
|
|
|
* will throw a LockedException and prevent login. |
174
|
|
|
* |
175
|
|
|
* @return bool true if the user is not locked, false otherwise |
176
|
|
|
* |
177
|
|
|
* @see LockedException |
178
|
|
|
*/ |
179
|
|
|
public function isAccountNonLocked() |
180
|
|
|
{ |
181
|
|
|
return $this->getAPIUser()->enabled; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Checks whether the user's credentials (password) has expired. |
186
|
|
|
* |
187
|
|
|
* Internally, if this method returns false, the authentication system |
188
|
|
|
* will throw a CredentialsExpiredException and prevent login. |
189
|
|
|
* |
190
|
|
|
* @return bool true if the user's credentials are non expired, false otherwise |
191
|
|
|
* |
192
|
|
|
* @see CredentialsExpiredException |
193
|
|
|
*/ |
194
|
|
|
public function isCredentialsNonExpired() |
195
|
|
|
{ |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Checks whether the user is enabled. |
201
|
|
|
* |
202
|
|
|
* Internally, if this method returns false, the authentication system |
203
|
|
|
* will throw a DisabledException and prevent login. |
204
|
|
|
* |
205
|
|
|
* @return bool true if the user is enabled, false otherwise |
206
|
|
|
* |
207
|
|
|
* @see DisabledException |
208
|
|
|
*/ |
209
|
|
|
public function isEnabled() |
210
|
|
|
{ |
211
|
|
|
return $this->getAPIUser()->enabled; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Make sure we don't serialize the whole API user object given it's a full fledged api content object. We set |
216
|
|
|
* (& either way refresh) the user object in \eZ\Publish\Core\MVC\Symfony\Security\User\Provider->refreshUser() |
217
|
|
|
* when object wakes back up from session. |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function __sleep() |
222
|
|
|
{ |
223
|
|
|
return ['reference', 'roles']; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|