1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhUser\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use ZfcRbac\Identity\IdentityInterface; |
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
use ZfcUser\Entity\UserInterface; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
use Doctrine\Common\Collections\Collection; |
11
|
|
|
use Rbac\Role\RoleInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class User |
15
|
|
|
* @package JhUser\Entity |
16
|
|
|
* @author Aydin Hassan <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @ORM\Entity |
19
|
|
|
* @ORM\Table(name="user") |
20
|
|
|
* @ORM\HasLifecycleCallbacks |
21
|
|
|
*/ |
22
|
|
|
class User implements UserInterface, IdentityInterface, JsonSerializable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
* |
27
|
|
|
* @ORM\Id |
28
|
|
|
* @ORM\Column(type="integer") |
29
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
30
|
|
|
*/ |
31
|
|
|
protected $id; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
* |
36
|
|
|
* @ORM\Column(type="string", unique=true, length=255, nullable=false) |
37
|
|
|
*/ |
38
|
|
|
protected $email; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
* @ORM\Column(type="string", unique=true, length=255, nullable=true) |
43
|
|
|
*/ |
44
|
|
|
protected $username; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
* |
49
|
|
|
* @ORM\Column(type="string", name="display_name", length=50, nullable=true) |
50
|
|
|
*/ |
51
|
|
|
protected $displayName; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
* |
56
|
|
|
* @ORM\Column(type="string", length=128) |
57
|
|
|
*/ |
58
|
|
|
protected $password; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int |
62
|
|
|
* |
63
|
|
|
* @ORM\Column(type="integer", name="state", nullable=true) |
64
|
|
|
*/ |
65
|
|
|
protected $state; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \DateTime |
69
|
|
|
* |
70
|
|
|
* @ORM\Column(type="datetime", name="created_at", nullable=false) |
71
|
|
|
*/ |
72
|
|
|
protected $createdAt; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var \Doctrine\Common\Collections\Collection |
76
|
|
|
* |
77
|
|
|
* @ORM\ManyToMany(targetEntity="JhUser\Entity\HierarchicalRole") |
78
|
|
|
* @ORM\JoinTable(name="user_role", |
79
|
|
|
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, |
80
|
|
|
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")} |
81
|
|
|
* ) |
82
|
|
|
*/ |
83
|
|
|
protected $roles; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initialise the roles variable. |
87
|
|
|
*/ |
88
|
|
|
public function __construct() |
89
|
|
|
{ |
90
|
|
|
$this->roles = new ArrayCollection(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function getId() |
97
|
|
|
{ |
98
|
|
|
return $this->id; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int $id |
103
|
|
|
* @return \JhUser\Entity\User |
104
|
|
|
*/ |
105
|
|
|
public function setId($id) |
106
|
|
|
{ |
107
|
|
|
$this->id = (int) $id; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getEmail() |
115
|
|
|
{ |
116
|
|
|
return $this->email; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $email |
121
|
|
|
* @return \JhUser\Entity\User |
122
|
|
|
*/ |
123
|
|
|
public function setEmail($email) |
124
|
|
|
{ |
125
|
|
|
$this->email = (string) $email; |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getUsername() |
133
|
|
|
{ |
134
|
|
|
return $this->username; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $username |
139
|
|
|
* @return \JhUser\Entity\User |
140
|
|
|
*/ |
141
|
|
|
public function setUsername($username) |
142
|
|
|
{ |
143
|
|
|
$this->username = (string) $username; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getDisplayName() |
151
|
|
|
{ |
152
|
|
|
if (!$this->displayName) { |
153
|
|
|
return null; |
154
|
|
|
} else { |
155
|
|
|
return ucwords($this->displayName); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $displayName |
161
|
|
|
* @return \JhUser\Entity\User |
162
|
|
|
*/ |
163
|
|
|
public function setDisplayName($displayName) |
164
|
|
|
{ |
165
|
|
|
$this->displayName = (string) $displayName; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getPassword() |
173
|
|
|
{ |
174
|
|
|
return $this->password; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $password |
179
|
|
|
* @return \JhUser\Entity\User |
180
|
|
|
*/ |
181
|
|
|
public function setPassword($password) |
182
|
|
|
{ |
183
|
|
|
$this->password = (string) $password; |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
|
|
public function getState() |
191
|
|
|
{ |
192
|
|
|
return $this->state; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param int|null $state |
197
|
|
|
* @return \JhUser\Entity\User |
198
|
|
|
*/ |
199
|
|
|
public function setState($state) |
200
|
|
|
{ |
201
|
|
|
$this->state = $state; |
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return \DateTime |
207
|
|
|
*/ |
208
|
|
|
public function getCreatedAt() |
209
|
|
|
{ |
210
|
|
|
return $this->createdAt; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param \DateTime $createdAt |
215
|
|
|
* @return \JhUser\Entity\User |
216
|
|
|
*/ |
217
|
|
|
public function setCreatedAt(\DateTime $createdAt) |
218
|
|
|
{ |
219
|
|
|
$this->createdAt = $createdAt; |
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function getRoles() |
227
|
|
|
{ |
228
|
|
|
return $this->roles->toArray(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set the list of roles |
233
|
|
|
* @param Collection $roles |
234
|
|
|
*/ |
235
|
|
|
public function setRoles(Collection $roles) |
236
|
|
|
{ |
237
|
|
|
$this->roles->clear(); |
238
|
|
|
foreach ($roles as $role) { |
239
|
|
|
$this->roles[] = $role; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Add one role to roles list |
245
|
|
|
* @param \Rbac\Role\RoleInterface $role |
246
|
|
|
*/ |
247
|
|
|
public function addRole(RoleInterface $role) |
248
|
|
|
{ |
249
|
|
|
$this->roles[] = $role; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Remove a particular role from a user |
254
|
|
|
* |
255
|
|
|
* @param RoleInterface $removeRole |
256
|
|
|
*/ |
257
|
|
|
public function removeRole(RoleInterface $removeRole) |
258
|
|
|
{ |
259
|
|
|
foreach ($this->roles as $role) { |
260
|
|
|
if ($role->getName() === $removeRole->getName()) { |
261
|
|
|
$this->roles->removeElement($removeRole); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function jsonSerialize() |
270
|
|
|
{ |
271
|
|
|
return [ |
272
|
|
|
'id' => $this->id, |
273
|
|
|
'name' => $this->displayName, |
274
|
|
|
'email' => $this->email, |
275
|
|
|
'state' => $this->state, |
276
|
|
|
]; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @ORM\PrePersist |
281
|
|
|
*/ |
282
|
|
|
public function setCreatedAtDate() |
283
|
|
|
{ |
284
|
|
|
$this->setCreatedAt(new \DateTime); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|