]> 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\GroupBundle\Entity\Group;
19 use Wallabag\GroupBundle\Entity\UserGroup;
20
21 /**
22 * User.
23 *
24 * @XmlRoot("user")
25 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
26 * @ORM\Table(name="`user`")
27 * @ORM\HasLifecycleCallbacks()
28 *
29 * @UniqueEntity("email")
30 * @UniqueEntity("username")
31 */
32 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
33 {
34 /** @Serializer\XmlAttribute */
35 /**
36 * @var int
37 *
38 * @ORM\Column(name="id", type="integer")
39 * @ORM\Id
40 * @ORM\GeneratedValue(strategy="AUTO")
41 *
42 * @Groups({"user_api", "user_api_with_client"})
43 */
44 protected $id;
45
46 /**
47 * @var string
48 *
49 * @ORM\Column(name="name", type="text", nullable=true)
50 *
51 * @Groups({"user_api", "user_api_with_client"})
52 */
53 protected $name;
54
55 /**
56 * @var string
57 *
58 * @Groups({"user_api", "user_api_with_client"})
59 */
60 protected $username;
61
62 /**
63 * @var string
64 *
65 * @Groups({"user_api", "user_api_with_client"})
66 */
67 protected $email;
68
69 /**
70 * @var \DateTime
71 *
72 * @ORM\Column(name="created_at", type="datetime")
73 *
74 * @Groups({"user_api", "user_api_with_client"})
75 */
76 protected $createdAt;
77
78 /**
79 * @var \DateTime
80 *
81 * @ORM\Column(name="updated_at", type="datetime")
82 *
83 * @Groups({"user_api", "user_api_with_client"})
84 */
85 protected $updatedAt;
86
87 /**
88 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
89 */
90 protected $entries;
91
92 /**
93 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
94 */
95 protected $config;
96
97 /**
98 * @ORM\Column(type="integer", nullable=true)
99 */
100 private $authCode;
101
102 /**
103 * @var ArrayCollection
104 *
105 * @ORM\OneToMany(targetEntity="Wallabag\GroupBundle\Entity\UserGroup", mappedBy="user", cascade={"persist", "remove"})
106 */
107 protected $userGroups;
108
109 /**
110 * @var bool Enabled yes/no
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
120 /**
121 * @var ArrayCollection
122 *
123 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
124 */
125 protected $clients;
126
127 /**
128 * @see getFirstClient() below
129 *
130 * @Groups({"user_api_with_client"})
131 * @Accessor(getter="getFirstClient")
132 */
133 protected $default_client;
134
135 public function __construct()
136 {
137 parent::__construct();
138 $this->entries = new ArrayCollection();
139 $this->userGroups = new ArrayCollection();
140 $this->roles = ['ROLE_USER'];
141 }
142
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
156 /**
157 * Set name.
158 *
159 * @param string $name
160 *
161 * @return User
162 */
163 public function setName($name)
164 {
165 $this->name = $name;
166
167 return $this;
168 }
169
170 /**
171 * Get name.
172 *
173 * @return string
174 */
175 public function getName()
176 {
177 return $this->name;
178 }
179
180 /**
181 * @return \DateTime
182 */
183 public function getCreatedAt()
184 {
185 return $this->createdAt;
186 }
187
188 /**
189 * @return \DateTime
190 */
191 public function getUpdatedAt()
192 {
193 return $this->updatedAt;
194 }
195
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
216 public function isEqualTo(UserInterface $user)
217 {
218 return $this->username === $user->getUsername();
219 }
220
221 /**
222 * Set config.
223 *
224 * @param Config $config
225 *
226 * @return User
227 */
228 public function setConfig(Config $config = null)
229 {
230 $this->config = $config;
231
232 return $this;
233 }
234
235 /**
236 * Get config.
237 *
238 * @return Config
239 */
240 public function getConfig()
241 {
242 return $this->config;
243 }
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 }
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 }
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 {
320 if (!empty($this->clients)) {
321 return $this->clients->first();
322 }
323
324 }
325
326 /**
327 * @param Group $group
328 * @return UserGroup
329 */
330 public function getUserGroupFromGroup(Group $group)
331 {
332 foreach ($this->userGroups as $userGroup) {
333 if ($userGroup->getGroup() == $group) {
334 return $userGroup;
335 }
336 }
337 return null;
338 }
339
340
341 /**
342 * @param Group $group
343 * @param $role
344 */
345 public function setGroupRole(Group $group, $role)
346 {
347 if ($userGroup = $this->getUserGroupFromGroup($group)) {
348 $userGroup->setRole($role);
349 }
350 }
351
352 /**
353 * @param Group $group
354 * @return int
355 */
356 public function getGroupRoleForUser(Group $group)
357 {
358 if ($userGroup = $this->getUserGroupFromGroup($group)) {
359 return $userGroup->getRole();
360 }
361 return 0;
362 }
363
364 /**
365 * @param Group $group
366 * @return bool
367 */
368 public function inGroup(Group $group)
369 {
370 if ($group::ACCESS_REQUEST === $group->getAcceptSystem()) {
371 $userGroup = $this->getUserGroupFromGroup($group);
372 return $userGroup->isAccepted();
373 }
374 return null !== $this->getUserGroupFromGroup($group);
375 }
376
377 /**
378 * @return ArrayCollection<Group>
379 */
380 public function getGroups()
381 {
382 $groups = new ArrayCollection();
383 foreach ($this->userGroups as $userGroup) {
384 $groups->add($userGroup->getGroup());
385 }
386 return $groups;
387 }
388 }