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