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