]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Entity; |
9d50517c | 4 | |
5f09650e | 5 | use Doctrine\Common\Collections\ArrayCollection; |
9d50517c | 6 | use Doctrine\ORM\Mapping as ORM; |
c3235553 | 7 | use Symfony\Component\Security\Core\User\UserInterface; |
c3235553 | 8 | use Symfony\Component\Security\Core\User\AdvancedUserInterface; |
9d50517c NL |
9 | |
10 | /** | |
2f69eb4a | 11 | * User |
9d50517c | 12 | * |
2f69eb4a | 13 | * @ORM\Table(name="user") |
9d50517c | 14 | * @ORM\Entity |
2f69eb4a | 15 | * @ORM\HasLifecycleCallbacks() |
9d50517c | 16 | */ |
2f69eb4a | 17 | class User implements AdvancedUserInterface, \Serializable |
9d50517c NL |
18 | { |
19 | /** | |
20 | * @var integer | |
21 | * | |
2f69eb4a | 22 | * @ORM\Column(name="id", type="integer") |
9d50517c | 23 | * @ORM\Id |
2f69eb4a | 24 | * @ORM\GeneratedValue(strategy="AUTO") |
9d50517c NL |
25 | */ |
26 | private $id; | |
27 | ||
28 | /** | |
29 | * @var string | |
30 | * | |
2f69eb4a | 31 | * @ORM\Column(name="username", type="text") |
9d50517c NL |
32 | */ |
33 | private $username; | |
34 | ||
c3235553 | 35 | /** |
2f69eb4a NL |
36 | * @var string |
37 | * | |
c3235553 NL |
38 | * @ORM\Column(type="string", length=32) |
39 | */ | |
40 | private $salt; | |
41 | ||
9d50517c NL |
42 | /** |
43 | * @var string | |
44 | * | |
2f69eb4a | 45 | * @ORM\Column(name="password", type="text") |
9d50517c NL |
46 | */ |
47 | private $password; | |
48 | ||
49 | /** | |
50 | * @var string | |
51 | * | |
52 | * @ORM\Column(name="name", type="text", nullable=true) | |
53 | */ | |
54 | private $name; | |
55 | ||
56 | /** | |
57 | * @var string | |
58 | * | |
59 | * @ORM\Column(name="email", type="text", nullable=true) | |
60 | */ | |
61 | private $email; | |
62 | ||
c3235553 NL |
63 | /** |
64 | * @ORM\Column(name="is_active", type="boolean") | |
65 | */ | |
66 | private $isActive; | |
9d50517c | 67 | |
2f69eb4a NL |
68 | /** |
69 | * @var date | |
70 | * | |
71 | * @ORM\Column(name="created_at", type="datetime") | |
72 | */ | |
73 | private $createdAt; | |
74 | ||
75 | /** | |
76 | * @var date | |
77 | * | |
78 | * @ORM\Column(name="updated_at", type="datetime") | |
79 | */ | |
80 | private $updatedAt; | |
81 | ||
5f09650e NL |
82 | /** |
83 | * @ORM\OneToMany(targetEntity="Entry", mappedBy="user", cascade={"remove"}) | |
84 | */ | |
85 | private $entries; | |
86 | ||
c3235553 NL |
87 | public function __construct() |
88 | { | |
89 | $this->isActive = true; | |
5f09650e NL |
90 | $this->salt = md5(uniqid(null, true)); |
91 | $this->entries = new ArrayCollection(); | |
c3235553 | 92 | } |
9d50517c | 93 | |
2f69eb4a NL |
94 | /** |
95 | * @ORM\PrePersist | |
96 | * @ORM\PreUpdate | |
97 | */ | |
98 | public function timestamps() | |
99 | { | |
100 | if (is_null($this->createdAt)) { | |
101 | $this->createdAt = new \DateTime(); | |
102 | } | |
103 | ||
104 | $this->updatedAt = new \DateTime(); | |
105 | } | |
106 | ||
9d50517c NL |
107 | /** |
108 | * Get id | |
109 | * | |
7df80cb3 | 110 | * @return integer |
9d50517c NL |
111 | */ |
112 | public function getId() | |
113 | { | |
114 | return $this->id; | |
115 | } | |
116 | ||
117 | /** | |
118 | * Set username | |
119 | * | |
7df80cb3 | 120 | * @param string $username |
2f69eb4a | 121 | * @return User |
9d50517c NL |
122 | */ |
123 | public function setUsername($username) | |
124 | { | |
125 | $this->username = $username; | |
126 | ||
127 | return $this; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Get username | |
132 | * | |
7df80cb3 | 133 | * @return string |
9d50517c NL |
134 | */ |
135 | public function getUsername() | |
136 | { | |
137 | return $this->username; | |
138 | } | |
139 | ||
c3235553 NL |
140 | /** |
141 | * @inheritDoc | |
142 | */ | |
143 | public function getSalt() | |
144 | { | |
145 | return $this->salt; | |
146 | } | |
147 | ||
148 | /** | |
149 | * @inheritDoc | |
150 | */ | |
151 | public function getRoles() | |
152 | { | |
153 | return array('ROLE_USER'); | |
154 | } | |
155 | ||
9d50517c NL |
156 | /** |
157 | * Set password | |
158 | * | |
7df80cb3 | 159 | * @param string $password |
2f69eb4a | 160 | * @return User |
9d50517c NL |
161 | */ |
162 | public function setPassword($password) | |
163 | { | |
d9169157 J |
164 | if (!$password && 0 === strlen($password)) { |
165 | return; | |
166 | } | |
167 | ||
168 | $this->password = sha1($password.$this->getUsername().$this->getSalt()); | |
9d50517c NL |
169 | |
170 | return $this; | |
171 | } | |
172 | ||
173 | /** | |
174 | * Get password | |
175 | * | |
7df80cb3 | 176 | * @return string |
9d50517c NL |
177 | */ |
178 | public function getPassword() | |
179 | { | |
180 | return $this->password; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Set name | |
185 | * | |
7df80cb3 | 186 | * @param string $name |
2f69eb4a | 187 | * @return User |
9d50517c NL |
188 | */ |
189 | public function setName($name) | |
190 | { | |
191 | $this->name = $name; | |
192 | ||
193 | return $this; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Get name | |
198 | * | |
7df80cb3 | 199 | * @return string |
9d50517c NL |
200 | */ |
201 | public function getName() | |
202 | { | |
203 | return $this->name; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Set email | |
208 | * | |
7df80cb3 | 209 | * @param string $email |
2f69eb4a | 210 | * @return User |
9d50517c NL |
211 | */ |
212 | public function setEmail($email) | |
213 | { | |
214 | $this->email = $email; | |
215 | ||
216 | return $this; | |
217 | } | |
218 | ||
219 | /** | |
220 | * Get email | |
221 | * | |
7df80cb3 | 222 | * @return string |
9d50517c NL |
223 | */ |
224 | public function getEmail() | |
225 | { | |
226 | return $this->email; | |
227 | } | |
c3235553 | 228 | |
2f69eb4a NL |
229 | /** |
230 | * @return string | |
231 | */ | |
232 | public function getCreatedAt() | |
233 | { | |
234 | return $this->createdAt; | |
235 | } | |
236 | ||
237 | /** | |
238 | * @return string | |
239 | */ | |
240 | public function getUpdatedAt() | |
241 | { | |
242 | return $this->updatedAt; | |
243 | } | |
244 | ||
5f09650e NL |
245 | /** |
246 | * @param Entry $entry | |
247 | * | |
248 | * @return User | |
249 | */ | |
250 | public function addEntry(Entry $entry) | |
251 | { | |
252 | $this->entries[] = $entry; | |
253 | ||
254 | return $this; | |
255 | } | |
256 | ||
257 | /** | |
258 | * @return ArrayCollection<Entry> | |
259 | */ | |
260 | public function getEntries() | |
261 | { | |
262 | return $this->entries; | |
263 | } | |
264 | ||
c3235553 NL |
265 | /** |
266 | * @inheritDoc | |
267 | */ | |
268 | public function eraseCredentials() | |
269 | { | |
270 | } | |
271 | ||
272 | /** | |
273 | * @see \Serializable::serialize() | |
274 | */ | |
275 | public function serialize() | |
276 | { | |
277 | return serialize(array( | |
278 | $this->id, | |
279 | )); | |
280 | } | |
281 | ||
282 | /** | |
283 | * @see \Serializable::unserialize() | |
284 | */ | |
285 | public function unserialize($serialized) | |
286 | { | |
7df80cb3 | 287 | list( |
c3235553 NL |
288 | $this->id, |
289 | ) = unserialize($serialized); | |
290 | } | |
291 | ||
292 | public function isEqualTo(UserInterface $user) | |
293 | { | |
294 | return $this->username === $user->getUsername(); | |
295 | } | |
296 | ||
297 | public function isAccountNonExpired() | |
298 | { | |
299 | return true; | |
300 | } | |
301 | ||
302 | public function isAccountNonLocked() | |
303 | { | |
304 | return true; | |
305 | } | |
306 | ||
307 | public function isCredentialsNonExpired() | |
308 | { | |
309 | return true; | |
310 | } | |
311 | ||
312 | public function isEnabled() | |
313 | { | |
314 | return $this->isActive; | |
315 | } | |
9d50517c | 316 | } |