]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
1210dae1 | 3 | namespace Wallabag\UserBundle\Entity; |
9d50517c | 4 | |
5f09650e | 5 | use Doctrine\Common\Collections\ArrayCollection; |
9d50517c | 6 | use Doctrine\ORM\Mapping as ORM; |
f808b016 JB |
7 | use FOS\UserBundle\Model\User as BaseUser; |
8 | use JMS\Serializer\Annotation\Accessor; | |
22510459 | 9 | use JMS\Serializer\Annotation\Groups; |
5709ecb3 | 10 | use JMS\Serializer\Annotation\XmlRoot; |
2db616b5 NL |
11 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; |
12 | use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; | |
619cc453 JB |
13 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
14 | use Symfony\Component\Security\Core\User\UserInterface; | |
23406ca3 | 15 | use Wallabag\ApiBundle\Entity\Client; |
1210dae1 NL |
16 | use Wallabag\CoreBundle\Entity\Config; |
17 | use Wallabag\CoreBundle\Entity\Entry; | |
9d50517c NL |
18 | |
19 | /** | |
4346a860 | 20 | * User. |
9d50517c | 21 | * |
5709ecb3 | 22 | * @XmlRoot("user") |
1210dae1 | 23 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") |
bd0f3d32 | 24 | * @ORM\Table(name="`user`") |
2f69eb4a | 25 | * @ORM\HasLifecycleCallbacks() |
c844dc0c J |
26 | * |
27 | * @UniqueEntity("email") | |
28 | * @UniqueEntity("username") | |
9d50517c | 29 | */ |
2db616b5 | 30 | class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface |
9d50517c | 31 | { |
5709ecb3 | 32 | /** @Serializer\XmlAttribute */ |
9d50517c | 33 | /** |
4346a860 | 34 | * @var int |
9d50517c | 35 | * |
2f69eb4a | 36 | * @ORM\Column(name="id", type="integer") |
9d50517c | 37 | * @ORM\Id |
2f69eb4a | 38 | * @ORM\GeneratedValue(strategy="AUTO") |
5709ecb3 | 39 | * |
0c00e525 | 40 | * @Groups({"user_api", "user_api_with_client"}) |
9d50517c | 41 | */ |
a1691859 | 42 | protected $id; |
9d50517c NL |
43 | |
44 | /** | |
45 | * @var string | |
46 | * | |
47 | * @ORM\Column(name="name", type="text", nullable=true) | |
5709ecb3 | 48 | * |
0c00e525 | 49 | * @Groups({"user_api", "user_api_with_client"}) |
9d50517c | 50 | */ |
a1691859 | 51 | protected $name; |
6894d48e | 52 | |
5709ecb3 JB |
53 | /** |
54 | * @var string | |
55 | * | |
0c00e525 | 56 | * @Groups({"user_api", "user_api_with_client"}) |
5709ecb3 JB |
57 | */ |
58 | protected $username; | |
59 | ||
60 | /** | |
61 | * @var string | |
62 | * | |
0c00e525 | 63 | * @Groups({"user_api", "user_api_with_client"}) |
5709ecb3 JB |
64 | */ |
65 | protected $email; | |
66 | ||
2f69eb4a | 67 | /** |
c3f7a2ca | 68 | * @var \DateTime |
2f69eb4a NL |
69 | * |
70 | * @ORM\Column(name="created_at", type="datetime") | |
5709ecb3 | 71 | * |
0c00e525 | 72 | * @Groups({"user_api", "user_api_with_client"}) |
2f69eb4a | 73 | */ |
a1691859 | 74 | protected $createdAt; |
2f69eb4a NL |
75 | |
76 | /** | |
c3f7a2ca | 77 | * @var \DateTime |
2f69eb4a NL |
78 | * |
79 | * @ORM\Column(name="updated_at", type="datetime") | |
5709ecb3 | 80 | * |
0c00e525 | 81 | * @Groups({"user_api", "user_api_with_client"}) |
2f69eb4a | 82 | */ |
a1691859 | 83 | protected $updatedAt; |
2f69eb4a | 84 | |
5f09650e | 85 | /** |
1210dae1 | 86 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"}) |
5f09650e | 87 | */ |
a1691859 | 88 | protected $entries; |
5f09650e | 89 | |
378aaefb TC |
90 | /** |
91 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Notification", mappedBy="user", cascade={"remove"}) | |
92 | */ | |
93 | protected $notifications; | |
94 | ||
32da2a70 | 95 | /** |
152fcccd | 96 | * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"}) |
32da2a70 | 97 | */ |
a1691859 | 98 | protected $config; |
32da2a70 | 99 | |
9114615a JB |
100 | /** |
101 | * @var ArrayCollection | |
102 | * | |
103 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"}) | |
104 | */ | |
e50d7d31 | 105 | protected $siteCredentials; |
9114615a | 106 | |
2db616b5 | 107 | /** |
f808b016 JB |
108 | * @var ArrayCollection |
109 | * | |
110 | * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"}) | |
2db616b5 | 111 | */ |
f808b016 | 112 | protected $clients; |
2db616b5 NL |
113 | |
114 | /** | |
f808b016 | 115 | * @see getFirstClient() below |
0c00e525 | 116 | * |
f808b016 JB |
117 | * @Groups({"user_api_with_client"}) |
118 | * @Accessor(getter="getFirstClient") | |
2db616b5 | 119 | */ |
f808b016 | 120 | protected $default_client; |
2db616b5 NL |
121 | |
122 | /** | |
f808b016 | 123 | * @ORM\Column(type="integer", nullable=true) |
2db616b5 | 124 | */ |
f808b016 | 125 | private $authCode; |
2db616b5 | 126 | |
23406ca3 | 127 | /** |
f808b016 | 128 | * @var bool |
eb570e49 | 129 | * |
f808b016 | 130 | * @ORM\Column(type="boolean") |
23406ca3 | 131 | */ |
f808b016 | 132 | private $twoFactorAuthentication = false; |
23406ca3 | 133 | |
0c00e525 | 134 | /** |
f808b016 | 135 | * @ORM\Column(type="json_array", nullable=true) |
0c00e525 | 136 | */ |
f808b016 | 137 | private $trusted; |
0c00e525 | 138 | |
c3235553 NL |
139 | public function __construct() |
140 | { | |
a1691859 | 141 | parent::__construct(); |
98f0929f | 142 | $this->entries = new ArrayCollection(); |
4094ea47 | 143 | $this->roles = ['ROLE_USER']; |
c3235553 | 144 | } |
9d50517c | 145 | |
2f69eb4a NL |
146 | /** |
147 | * @ORM\PrePersist | |
148 | * @ORM\PreUpdate | |
149 | */ | |
150 | public function timestamps() | |
151 | { | |
f808b016 | 152 | if (null === $this->createdAt) { |
2f69eb4a NL |
153 | $this->createdAt = new \DateTime(); |
154 | } | |
155 | ||
156 | $this->updatedAt = new \DateTime(); | |
157 | } | |
158 | ||
9d50517c | 159 | /** |
4346a860 JB |
160 | * Set name. |
161 | * | |
162 | * @param string $name | |
9d50517c | 163 | * |
2f69eb4a | 164 | * @return User |
9d50517c NL |
165 | */ |
166 | public function setName($name) | |
167 | { | |
168 | $this->name = $name; | |
169 | ||
170 | return $this; | |
171 | } | |
172 | ||
173 | /** | |
4346a860 | 174 | * Get name. |
9d50517c | 175 | * |
7df80cb3 | 176 | * @return string |
9d50517c NL |
177 | */ |
178 | public function getName() | |
179 | { | |
180 | return $this->name; | |
181 | } | |
182 | ||
2f69eb4a | 183 | /** |
3a6af6c5 | 184 | * @return \DateTime |
2f69eb4a NL |
185 | */ |
186 | public function getCreatedAt() | |
187 | { | |
188 | return $this->createdAt; | |
189 | } | |
190 | ||
191 | /** | |
3a6af6c5 | 192 | * @return \DateTime |
2f69eb4a NL |
193 | */ |
194 | public function getUpdatedAt() | |
195 | { | |
196 | return $this->updatedAt; | |
197 | } | |
198 | ||
5f09650e NL |
199 | /** |
200 | * @param Entry $entry | |
201 | * | |
202 | * @return User | |
203 | */ | |
204 | public function addEntry(Entry $entry) | |
205 | { | |
206 | $this->entries[] = $entry; | |
207 | ||
208 | return $this; | |
209 | } | |
210 | ||
211 | /** | |
212 | * @return ArrayCollection<Entry> | |
213 | */ | |
214 | public function getEntries() | |
215 | { | |
216 | return $this->entries; | |
217 | } | |
218 | ||
c3235553 NL |
219 | public function isEqualTo(UserInterface $user) |
220 | { | |
221 | return $this->username === $user->getUsername(); | |
222 | } | |
223 | ||
32da2a70 | 224 | /** |
4346a860 JB |
225 | * Set config. |
226 | * | |
1210dae1 | 227 | * @param Config $config |
32da2a70 | 228 | * |
32da2a70 J |
229 | * @return User |
230 | */ | |
1210dae1 | 231 | public function setConfig(Config $config = null) |
32da2a70 J |
232 | { |
233 | $this->config = $config; | |
234 | ||
235 | return $this; | |
236 | } | |
237 | ||
238 | /** | |
4346a860 | 239 | * Get config. |
32da2a70 | 240 | * |
1210dae1 | 241 | * @return Config |
32da2a70 J |
242 | */ |
243 | public function getConfig() | |
244 | { | |
245 | return $this->config; | |
246 | } | |
2db616b5 NL |
247 | |
248 | /** | |
249 | * @return bool | |
250 | */ | |
251 | public function isTwoFactorAuthentication() | |
252 | { | |
253 | return $this->twoFactorAuthentication; | |
254 | } | |
255 | ||
256 | /** | |
257 | * @param bool $twoFactorAuthentication | |
258 | */ | |
259 | public function setTwoFactorAuthentication($twoFactorAuthentication) | |
260 | { | |
261 | $this->twoFactorAuthentication = $twoFactorAuthentication; | |
262 | } | |
263 | ||
264 | public function isEmailAuthEnabled() | |
265 | { | |
266 | return $this->twoFactorAuthentication; | |
267 | } | |
268 | ||
269 | public function getEmailAuthCode() | |
270 | { | |
271 | return $this->authCode; | |
272 | } | |
273 | ||
274 | public function setEmailAuthCode($authCode) | |
275 | { | |
276 | $this->authCode = $authCode; | |
277 | } | |
278 | ||
279 | public function addTrustedComputer($token, \DateTime $validUntil) | |
280 | { | |
281 | $this->trusted[$token] = $validUntil->format('r'); | |
282 | } | |
283 | ||
284 | public function isTrustedComputer($token) | |
285 | { | |
286 | if (isset($this->trusted[$token])) { | |
287 | $now = new \DateTime(); | |
288 | $validUntil = new \DateTime($this->trusted[$token]); | |
289 | ||
290 | return $now < $validUntil; | |
291 | } | |
292 | ||
293 | return false; | |
294 | } | |
23406ca3 NL |
295 | |
296 | /** | |
297 | * @param Client $client | |
298 | * | |
299 | * @return User | |
300 | */ | |
301 | public function addClient(Client $client) | |
302 | { | |
303 | $this->clients[] = $client; | |
304 | ||
305 | return $this; | |
306 | } | |
307 | ||
308 | /** | |
309 | * @return ArrayCollection<Entry> | |
310 | */ | |
311 | public function getClients() | |
312 | { | |
313 | return $this->clients; | |
314 | } | |
0c00e525 JB |
315 | |
316 | /** | |
317 | * Only used by the API when creating a new user it'll also return the first client (which was also created at the same time). | |
318 | * | |
319 | * @return Client | |
320 | */ | |
321 | public function getFirstClient() | |
322 | { | |
eb570e49 JB |
323 | if (!empty($this->clients)) { |
324 | return $this->clients->first(); | |
0c00e525 | 325 | } |
0c00e525 | 326 | } |
378aaefb TC |
327 | |
328 | /** | |
329 | * @return ArrayCollection<NotificationInterface> | |
330 | */ | |
331 | public function getNotifications() | |
332 | { | |
333 | return $this->notifications; | |
334 | } | |
335 | ||
336 | /** | |
337 | * @param ArrayCollection<NotificationInterface> $notifications | |
338 | */ | |
339 | public function setNotifications($notifications) | |
340 | { | |
341 | $this->notifications = $notifications; | |
342 | } | |
9d50517c | 343 | } |