]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Entity/User.php
Add EntityTimestampsTrait to handle dates
[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;
f808b016
JB
7use FOS\UserBundle\Model\User as BaseUser;
8use JMS\Serializer\Annotation\Accessor;
22510459 9use JMS\Serializer\Annotation\Groups;
5709ecb3 10use JMS\Serializer\Annotation\XmlRoot;
2db616b5
NL
11use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
12use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
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;
927c9e79 18use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
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{
927c9e79
JB
33 use EntityTimestampsTrait;
34
5709ecb3 35 /** @Serializer\XmlAttribute */
9d50517c 36 /**
4346a860 37 * @var int
9d50517c 38 *
2f69eb4a 39 * @ORM\Column(name="id", type="integer")
9d50517c 40 * @ORM\Id
2f69eb4a 41 * @ORM\GeneratedValue(strategy="AUTO")
5709ecb3 42 *
0c00e525 43 * @Groups({"user_api", "user_api_with_client"})
9d50517c 44 */
a1691859 45 protected $id;
9d50517c
NL
46
47 /**
48 * @var string
49 *
50 * @ORM\Column(name="name", type="text", nullable=true)
5709ecb3 51 *
0c00e525 52 * @Groups({"user_api", "user_api_with_client"})
9d50517c 53 */
a1691859 54 protected $name;
6894d48e 55
5709ecb3
JB
56 /**
57 * @var string
58 *
0c00e525 59 * @Groups({"user_api", "user_api_with_client"})
5709ecb3
JB
60 */
61 protected $username;
62
63 /**
64 * @var string
65 *
0c00e525 66 * @Groups({"user_api", "user_api_with_client"})
5709ecb3
JB
67 */
68 protected $email;
69
2f69eb4a 70 /**
c3f7a2ca 71 * @var \DateTime
2f69eb4a
NL
72 *
73 * @ORM\Column(name="created_at", type="datetime")
5709ecb3 74 *
0c00e525 75 * @Groups({"user_api", "user_api_with_client"})
2f69eb4a 76 */
a1691859 77 protected $createdAt;
2f69eb4a
NL
78
79 /**
c3f7a2ca 80 * @var \DateTime
2f69eb4a
NL
81 *
82 * @ORM\Column(name="updated_at", type="datetime")
5709ecb3 83 *
0c00e525 84 * @Groups({"user_api", "user_api_with_client"})
2f69eb4a 85 */
a1691859 86 protected $updatedAt;
2f69eb4a 87
5f09650e 88 /**
1210dae1 89 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
5f09650e 90 */
a1691859 91 protected $entries;
5f09650e 92
32da2a70 93 /**
152fcccd 94 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
32da2a70 95 */
a1691859 96 protected $config;
32da2a70 97
9114615a
JB
98 /**
99 * @var ArrayCollection
100 *
101 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
102 */
e50d7d31 103 protected $siteCredentials;
9114615a 104
2db616b5 105 /**
f808b016
JB
106 * @var ArrayCollection
107 *
108 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
2db616b5 109 */
f808b016 110 protected $clients;
2db616b5
NL
111
112 /**
f808b016 113 * @see getFirstClient() below
0c00e525 114 *
f808b016
JB
115 * @Groups({"user_api_with_client"})
116 * @Accessor(getter="getFirstClient")
2db616b5 117 */
f808b016 118 protected $default_client;
2db616b5
NL
119
120 /**
f808b016 121 * @ORM\Column(type="integer", nullable=true)
2db616b5 122 */
f808b016 123 private $authCode;
2db616b5 124
23406ca3 125 /**
f808b016 126 * @var bool
eb570e49 127 *
f808b016 128 * @ORM\Column(type="boolean")
23406ca3 129 */
f808b016 130 private $twoFactorAuthentication = false;
23406ca3 131
0c00e525 132 /**
f808b016 133 * @ORM\Column(type="json_array", nullable=true)
0c00e525 134 */
f808b016 135 private $trusted;
0c00e525 136
c3235553
NL
137 public function __construct()
138 {
a1691859 139 parent::__construct();
98f0929f 140 $this->entries = new ArrayCollection();
4094ea47 141 $this->roles = ['ROLE_USER'];
c3235553 142 }
2f69eb4a 143
9d50517c 144 /**
4346a860
JB
145 * Set name.
146 *
147 * @param string $name
9d50517c 148 *
2f69eb4a 149 * @return User
9d50517c
NL
150 */
151 public function setName($name)
152 {
153 $this->name = $name;
154
155 return $this;
156 }
157
158 /**
4346a860 159 * Get name.
9d50517c 160 *
7df80cb3 161 * @return string
9d50517c
NL
162 */
163 public function getName()
164 {
165 return $this->name;
166 }
167
2f69eb4a 168 /**
3a6af6c5 169 * @return \DateTime
2f69eb4a
NL
170 */
171 public function getCreatedAt()
172 {
173 return $this->createdAt;
174 }
175
176 /**
3a6af6c5 177 * @return \DateTime
2f69eb4a
NL
178 */
179 public function getUpdatedAt()
180 {
181 return $this->updatedAt;
182 }
183
5f09650e
NL
184 /**
185 * @param Entry $entry
186 *
187 * @return User
188 */
189 public function addEntry(Entry $entry)
190 {
191 $this->entries[] = $entry;
192
193 return $this;
194 }
195
196 /**
197 * @return ArrayCollection<Entry>
198 */
199 public function getEntries()
200 {
201 return $this->entries;
202 }
203
c3235553
NL
204 public function isEqualTo(UserInterface $user)
205 {
206 return $this->username === $user->getUsername();
207 }
208
32da2a70 209 /**
4346a860
JB
210 * Set config.
211 *
1210dae1 212 * @param Config $config
32da2a70 213 *
32da2a70
J
214 * @return User
215 */
1210dae1 216 public function setConfig(Config $config = null)
32da2a70
J
217 {
218 $this->config = $config;
219
220 return $this;
221 }
222
223 /**
4346a860 224 * Get config.
32da2a70 225 *
1210dae1 226 * @return Config
32da2a70
J
227 */
228 public function getConfig()
229 {
230 return $this->config;
231 }
2db616b5
NL
232
233 /**
234 * @return bool
235 */
236 public function isTwoFactorAuthentication()
237 {
238 return $this->twoFactorAuthentication;
239 }
240
241 /**
242 * @param bool $twoFactorAuthentication
243 */
244 public function setTwoFactorAuthentication($twoFactorAuthentication)
245 {
246 $this->twoFactorAuthentication = $twoFactorAuthentication;
247 }
248
249 public function isEmailAuthEnabled()
250 {
251 return $this->twoFactorAuthentication;
252 }
253
254 public function getEmailAuthCode()
255 {
256 return $this->authCode;
257 }
258
259 public function setEmailAuthCode($authCode)
260 {
261 $this->authCode = $authCode;
262 }
263
264 public function addTrustedComputer($token, \DateTime $validUntil)
265 {
266 $this->trusted[$token] = $validUntil->format('r');
267 }
268
269 public function isTrustedComputer($token)
270 {
271 if (isset($this->trusted[$token])) {
272 $now = new \DateTime();
273 $validUntil = new \DateTime($this->trusted[$token]);
274
275 return $now < $validUntil;
276 }
277
278 return false;
279 }
23406ca3
NL
280
281 /**
282 * @param Client $client
283 *
284 * @return User
285 */
286 public function addClient(Client $client)
287 {
288 $this->clients[] = $client;
289
290 return $this;
291 }
292
293 /**
294 * @return ArrayCollection<Entry>
295 */
296 public function getClients()
297 {
298 return $this->clients;
299 }
0c00e525
JB
300
301 /**
302 * 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).
303 *
304 * @return Client
305 */
306 public function getFirstClient()
307 {
eb570e49
JB
308 if (!empty($this->clients)) {
309 return $this->clients->first();
0c00e525 310 }
0c00e525 311 }
9d50517c 312}