]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Entity/User.php
Create a client when creating a user using the api
[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;
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 30class 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
32da2a70 90 /**
152fcccd 91 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
32da2a70 92 */
a1691859 93 protected $config;
32da2a70 94
2db616b5
NL
95 /**
96 * @ORM\Column(type="integer", nullable=true)
97 */
98 private $authCode;
99
100 /**
0c00e525
JB
101 * @var bool
102 *
2db616b5
NL
103 * @ORM\Column(type="boolean")
104 */
105 private $twoFactorAuthentication = false;
106
107 /**
108 * @ORM\Column(type="json_array", nullable=true)
109 */
110 private $trusted;
111
23406ca3
NL
112 /**
113 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
114 */
115 protected $clients;
116
0c00e525
JB
117 /**
118 * @see getFirstClient() below
119 *
120 * @Groups({"user_api_with_client"})
121 * @Accessor(getter="getFirstClient")
122 */
123 protected $default_client;
124
c3235553
NL
125 public function __construct()
126 {
a1691859 127 parent::__construct();
98f0929f 128 $this->entries = new ArrayCollection();
4094ea47 129 $this->roles = ['ROLE_USER'];
c3235553 130 }
9d50517c 131
2f69eb4a
NL
132 /**
133 * @ORM\PrePersist
134 * @ORM\PreUpdate
135 */
136 public function timestamps()
137 {
138 if (is_null($this->createdAt)) {
139 $this->createdAt = new \DateTime();
140 }
141
142 $this->updatedAt = new \DateTime();
143 }
144
9d50517c 145 /**
4346a860
JB
146 * Set name.
147 *
148 * @param string $name
9d50517c 149 *
2f69eb4a 150 * @return User
9d50517c
NL
151 */
152 public function setName($name)
153 {
154 $this->name = $name;
155
156 return $this;
157 }
158
159 /**
4346a860 160 * Get name.
9d50517c 161 *
7df80cb3 162 * @return string
9d50517c
NL
163 */
164 public function getName()
165 {
166 return $this->name;
167 }
168
2f69eb4a 169 /**
3a6af6c5 170 * @return \DateTime
2f69eb4a
NL
171 */
172 public function getCreatedAt()
173 {
174 return $this->createdAt;
175 }
176
177 /**
3a6af6c5 178 * @return \DateTime
2f69eb4a
NL
179 */
180 public function getUpdatedAt()
181 {
182 return $this->updatedAt;
183 }
184
5f09650e
NL
185 /**
186 * @param Entry $entry
187 *
188 * @return User
189 */
190 public function addEntry(Entry $entry)
191 {
192 $this->entries[] = $entry;
193
194 return $this;
195 }
196
197 /**
198 * @return ArrayCollection<Entry>
199 */
200 public function getEntries()
201 {
202 return $this->entries;
203 }
204
c3235553
NL
205 public function isEqualTo(UserInterface $user)
206 {
207 return $this->username === $user->getUsername();
208 }
209
32da2a70 210 /**
4346a860
JB
211 * Set config.
212 *
1210dae1 213 * @param Config $config
32da2a70 214 *
32da2a70
J
215 * @return User
216 */
1210dae1 217 public function setConfig(Config $config = null)
32da2a70
J
218 {
219 $this->config = $config;
220
221 return $this;
222 }
223
224 /**
4346a860 225 * Get config.
32da2a70 226 *
1210dae1 227 * @return Config
32da2a70
J
228 */
229 public function getConfig()
230 {
231 return $this->config;
232 }
2db616b5
NL
233
234 /**
235 * @return bool
236 */
237 public function isTwoFactorAuthentication()
238 {
239 return $this->twoFactorAuthentication;
240 }
241
242 /**
243 * @param bool $twoFactorAuthentication
244 */
245 public function setTwoFactorAuthentication($twoFactorAuthentication)
246 {
247 $this->twoFactorAuthentication = $twoFactorAuthentication;
248 }
249
250 public function isEmailAuthEnabled()
251 {
252 return $this->twoFactorAuthentication;
253 }
254
255 public function getEmailAuthCode()
256 {
257 return $this->authCode;
258 }
259
260 public function setEmailAuthCode($authCode)
261 {
262 $this->authCode = $authCode;
263 }
264
265 public function addTrustedComputer($token, \DateTime $validUntil)
266 {
267 $this->trusted[$token] = $validUntil->format('r');
268 }
269
270 public function isTrustedComputer($token)
271 {
272 if (isset($this->trusted[$token])) {
273 $now = new \DateTime();
274 $validUntil = new \DateTime($this->trusted[$token]);
275
276 return $now < $validUntil;
277 }
278
279 return false;
280 }
23406ca3
NL
281
282 /**
283 * @param Client $client
284 *
285 * @return User
286 */
287 public function addClient(Client $client)
288 {
289 $this->clients[] = $client;
290
291 return $this;
292 }
293
294 /**
295 * @return ArrayCollection<Entry>
296 */
297 public function getClients()
298 {
299 return $this->clients;
300 }
0c00e525
JB
301
302 /**
303 * 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).
304 *
305 * @return Client
306 */
307 public function getFirstClient()
308 {
309 if (empty($this->clients)) {
310 return $this->clients;
311 }
312
313 return $this->clients->first();
314 }
9d50517c 315}