]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
1210dae1 | 3 | namespace Wallabag\UserBundle\Entity; |
9d50517c | 4 | |
5f09650e | 5 | use Doctrine\Common\Collections\ArrayCollection; |
9d50517c | 6 | use Doctrine\ORM\Mapping as ORM; |
22510459 | 7 | use JMS\Serializer\Annotation\Groups; |
5709ecb3 | 8 | use JMS\Serializer\Annotation\XmlRoot; |
0c00e525 | 9 | use JMS\Serializer\Annotation\Accessor; |
2db616b5 NL |
10 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; |
11 | use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; | |
619cc453 | 12 | use FOS\UserBundle\Model\User as BaseUser; |
619cc453 JB |
13 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
14 | use Symfony\Component\Security\Core\User\UserInterface; | |
23406ca3 | 15 | use Wallabag\ApiBundle\Entity\Client; |
1210dae1 NL |
16 | use Wallabag\CoreBundle\Entity\Config; |
17 | use Wallabag\CoreBundle\Entity\Entry; | |
2041810a TC |
18 | use Wallabag\GroupBundle\Entity\Group; |
19 | use 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 | 32 | class 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 TC |
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 | |
56c81a2f NL |
366 | * @return bool |
367 | */ | |
2041810a | 368 | public function inGroup(Group $group) |
56c81a2f | 369 | { |
2041810a TC |
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; | |
0c00e525 | 387 | } |
9d50517c | 388 | } |