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