]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
WIP
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
1 <?php
2
3 namespace Wallabag\UserBundle\Entity;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\ORM\Mapping as ORM;
7 use JMS\Serializer\Annotation\Groups;
8 use JMS\Serializer\Annotation\XmlRoot;
9 use JMS\Serializer\Annotation\Accessor;
10 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
11 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
12 use FOS\UserBundle\Model\User as BaseUser;
13 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
14 use Symfony\Component\Security\Core\User\UserInterface;
15 use Wallabag\ApiBundle\Entity\Client;
16 use Wallabag\CoreBundle\Entity\Config;
17 use Wallabag\CoreBundle\Entity\Entry;
18 use Wallabag\FederationBundle\Entity\Account;
19
20 /**
21 * User.
22 *
23 * @XmlRoot("user")
24 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
25 * @ORM\Table(name="`user`")
26 * @ORM\HasLifecycleCallbacks()
27 *
28 * @UniqueEntity("email")
29 * @UniqueEntity("username")
30 */
31 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
32 {
33 /** @Serializer\XmlAttribute */
34 /**
35 * @var int
36 *
37 * @ORM\Column(name="id", type="integer")
38 * @ORM\Id
39 * @ORM\GeneratedValue(strategy="AUTO")
40 *
41 * @Groups({"user_api", "user_api_with_client"})
42 */
43 protected $id;
44
45 /**
46 * @var string
47 *
48 * @ORM\Column(name="name", type="text", nullable=true)
49 *
50 * @Groups({"user_api", "user_api_with_client"})
51 */
52 protected $name;
53
54 /**
55 * @var string
56 *
57 * @Groups({"user_api", "user_api_with_client"})
58 */
59 protected $username;
60
61 /**
62 * @var string
63 *
64 * @Groups({"user_api", "user_api_with_client"})
65 */
66 protected $email;
67
68 /**
69 * @var \DateTime
70 *
71 * @ORM\Column(name="created_at", type="datetime")
72 *
73 * @Groups({"user_api", "user_api_with_client"})
74 */
75 protected $createdAt;
76
77 /**
78 * @var \DateTime
79 *
80 * @ORM\Column(name="updated_at", type="datetime")
81 *
82 * @Groups({"user_api", "user_api_with_client"})
83 */
84 protected $updatedAt;
85
86 /**
87 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
88 */
89 protected $entries;
90
91 /**
92 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Notification", mappedBy="user", cascade={"remove"})
93 */
94 protected $notifications;
95
96 /**
97 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
98 */
99 protected $config;
100
101 /**
102 * @ORM\Column(type="integer", nullable=true)
103 */
104 private $authCode;
105
106 /**
107 * @var bool
108 *
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
118 /**
119 * @var ArrayCollection
120 *
121 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
122 */
123 protected $clients;
124
125 /**
126 * @see getFirstClient() below
127 *
128 * @Groups({"user_api_with_client"})
129 * @Accessor(getter="getFirstClient")
130 */
131 protected $default_client;
132
133 /**
134 * @ORM\OneToOne(targetEntity="Wallabag\FederationBundle\Entity\Account", mappedBy="user", cascade={"remove"})
135 */
136 protected $account;
137
138 /**
139 * User constructor.
140 */
141 public function __construct()
142 {
143 parent::__construct();
144 $this->entries = new ArrayCollection();
145 $this->roles = ['ROLE_USER'];
146 }
147
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
161 /**
162 * Set name.
163 *
164 * @param string $name
165 *
166 * @return User
167 */
168 public function setName($name)
169 {
170 $this->name = $name;
171
172 return $this;
173 }
174
175 /**
176 * Get name.
177 *
178 * @return string
179 */
180 public function getName()
181 {
182 return $this->name;
183 }
184
185 /**
186 * @return \DateTime
187 */
188 public function getCreatedAt()
189 {
190 return $this->createdAt;
191 }
192
193 /**
194 * @return \DateTime
195 */
196 public function getUpdatedAt()
197 {
198 return $this->updatedAt;
199 }
200
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
221 public function isEqualTo(UserInterface $user)
222 {
223 return $this->username === $user->getUsername();
224 }
225
226 /**
227 * Set config.
228 *
229 * @param Config $config
230 *
231 * @return User
232 */
233 public function setConfig(Config $config = null)
234 {
235 $this->config = $config;
236
237 return $this;
238 }
239
240 /**
241 * Get config.
242 *
243 * @return Config
244 */
245 public function getConfig()
246 {
247 return $this->config;
248 }
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 }
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 }
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 {
325 if (!empty($this->clients)) {
326 return $this->clients->first();
327 }
328 }
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 }
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 }
363 }