]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\UserBundle\Entity; | |
4 | ||
5 | use Doctrine\Common\Collections\ArrayCollection; | |
6 | use Doctrine\ORM\Mapping as ORM; | |
7 | use FOS\UserBundle\Model\User as BaseUser; | |
8 | use JMS\Serializer\Annotation\Accessor; | |
9 | use JMS\Serializer\Annotation\Groups; | |
10 | use JMS\Serializer\Annotation\XmlRoot; | |
11 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; | |
12 | use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; | |
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 | ||
19 | /** | |
20 | * User. | |
21 | * | |
22 | * @XmlRoot("user") | |
23 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") | |
24 | * @ORM\Table(name="`user`") | |
25 | * @ORM\HasLifecycleCallbacks() | |
26 | * | |
27 | * @UniqueEntity("email") | |
28 | * @UniqueEntity("username") | |
29 | */ | |
30 | class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface | |
31 | { | |
32 | /** @Serializer\XmlAttribute */ | |
33 | /** | |
34 | * @var int | |
35 | * | |
36 | * @ORM\Column(name="id", type="integer") | |
37 | * @ORM\Id | |
38 | * @ORM\GeneratedValue(strategy="AUTO") | |
39 | * | |
40 | * @Groups({"user_api", "user_api_with_client"}) | |
41 | */ | |
42 | protected $id; | |
43 | ||
44 | /** | |
45 | * @var string | |
46 | * | |
47 | * @ORM\Column(name="name", type="text", nullable=true) | |
48 | * | |
49 | * @Groups({"user_api", "user_api_with_client"}) | |
50 | */ | |
51 | protected $name; | |
52 | ||
53 | /** | |
54 | * @var string | |
55 | * | |
56 | * @Groups({"user_api", "user_api_with_client"}) | |
57 | */ | |
58 | protected $username; | |
59 | ||
60 | /** | |
61 | * @var string | |
62 | * | |
63 | * @Groups({"user_api", "user_api_with_client"}) | |
64 | */ | |
65 | protected $email; | |
66 | ||
67 | /** | |
68 | * @var \DateTime | |
69 | * | |
70 | * @ORM\Column(name="created_at", type="datetime") | |
71 | * | |
72 | * @Groups({"user_api", "user_api_with_client"}) | |
73 | */ | |
74 | protected $createdAt; | |
75 | ||
76 | /** | |
77 | * @var \DateTime | |
78 | * | |
79 | * @ORM\Column(name="updated_at", type="datetime") | |
80 | * | |
81 | * @Groups({"user_api", "user_api_with_client"}) | |
82 | */ | |
83 | protected $updatedAt; | |
84 | ||
85 | /** | |
86 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"}) | |
87 | */ | |
88 | protected $entries; | |
89 | ||
90 | /** | |
91 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Notification", mappedBy="user", cascade={"remove"}) | |
92 | */ | |
93 | protected $notifications; | |
94 | ||
95 | /** | |
96 | * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"}) | |
97 | */ | |
98 | protected $config; | |
99 | ||
100 | /** | |
101 | * @var ArrayCollection | |
102 | * | |
103 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"}) | |
104 | */ | |
105 | protected $siteCredentials; | |
106 | ||
107 | /** | |
108 | * @var ArrayCollection | |
109 | * | |
110 | * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"}) | |
111 | */ | |
112 | protected $clients; | |
113 | ||
114 | /** | |
115 | * @see getFirstClient() below | |
116 | * | |
117 | * @Groups({"user_api_with_client"}) | |
118 | * @Accessor(getter="getFirstClient") | |
119 | */ | |
120 | protected $default_client; | |
121 | ||
122 | /** | |
123 | * @ORM\Column(type="integer", nullable=true) | |
124 | */ | |
125 | private $authCode; | |
126 | ||
127 | /** | |
128 | * @var bool | |
129 | * | |
130 | * @ORM\Column(type="boolean") | |
131 | */ | |
132 | private $twoFactorAuthentication = false; | |
133 | ||
134 | /** | |
135 | * @ORM\Column(type="json_array", nullable=true) | |
136 | */ | |
137 | private $trusted; | |
138 | ||
139 | public function __construct() | |
140 | { | |
141 | parent::__construct(); | |
142 | $this->entries = new ArrayCollection(); | |
143 | $this->roles = ['ROLE_USER']; | |
144 | } | |
145 | ||
146 | /** | |
147 | * @ORM\PrePersist | |
148 | * @ORM\PreUpdate | |
149 | */ | |
150 | public function timestamps() | |
151 | { | |
152 | if (null === $this->createdAt) { | |
153 | $this->createdAt = new \DateTime(); | |
154 | } | |
155 | ||
156 | $this->updatedAt = new \DateTime(); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Set name. | |
161 | * | |
162 | * @param string $name | |
163 | * | |
164 | * @return User | |
165 | */ | |
166 | public function setName($name) | |
167 | { | |
168 | $this->name = $name; | |
169 | ||
170 | return $this; | |
171 | } | |
172 | ||
173 | /** | |
174 | * Get name. | |
175 | * | |
176 | * @return string | |
177 | */ | |
178 | public function getName() | |
179 | { | |
180 | return $this->name; | |
181 | } | |
182 | ||
183 | /** | |
184 | * @return \DateTime | |
185 | */ | |
186 | public function getCreatedAt() | |
187 | { | |
188 | return $this->createdAt; | |
189 | } | |
190 | ||
191 | /** | |
192 | * @return \DateTime | |
193 | */ | |
194 | public function getUpdatedAt() | |
195 | { | |
196 | return $this->updatedAt; | |
197 | } | |
198 | ||
199 | /** | |
200 | * @param Entry $entry | |
201 | * | |
202 | * @return User | |
203 | */ | |
204 | public function addEntry(Entry $entry) | |
205 | { | |
206 | $this->entries[] = $entry; | |
207 | ||
208 | return $this; | |
209 | } | |
210 | ||
211 | /** | |
212 | * @return ArrayCollection<Entry> | |
213 | */ | |
214 | public function getEntries() | |
215 | { | |
216 | return $this->entries; | |
217 | } | |
218 | ||
219 | public function isEqualTo(UserInterface $user) | |
220 | { | |
221 | return $this->username === $user->getUsername(); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Set config. | |
226 | * | |
227 | * @param Config $config | |
228 | * | |
229 | * @return User | |
230 | */ | |
231 | public function setConfig(Config $config = null) | |
232 | { | |
233 | $this->config = $config; | |
234 | ||
235 | return $this; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Get config. | |
240 | * | |
241 | * @return Config | |
242 | */ | |
243 | public function getConfig() | |
244 | { | |
245 | return $this->config; | |
246 | } | |
247 | ||
248 | /** | |
249 | * @return bool | |
250 | */ | |
251 | public function isTwoFactorAuthentication() | |
252 | { | |
253 | return $this->twoFactorAuthentication; | |
254 | } | |
255 | ||
256 | /** | |
257 | * @param bool $twoFactorAuthentication | |
258 | */ | |
259 | public function setTwoFactorAuthentication($twoFactorAuthentication) | |
260 | { | |
261 | $this->twoFactorAuthentication = $twoFactorAuthentication; | |
262 | } | |
263 | ||
264 | public function isEmailAuthEnabled() | |
265 | { | |
266 | return $this->twoFactorAuthentication; | |
267 | } | |
268 | ||
269 | public function getEmailAuthCode() | |
270 | { | |
271 | return $this->authCode; | |
272 | } | |
273 | ||
274 | public function setEmailAuthCode($authCode) | |
275 | { | |
276 | $this->authCode = $authCode; | |
277 | } | |
278 | ||
279 | public function addTrustedComputer($token, \DateTime $validUntil) | |
280 | { | |
281 | $this->trusted[$token] = $validUntil->format('r'); | |
282 | } | |
283 | ||
284 | public function isTrustedComputer($token) | |
285 | { | |
286 | if (isset($this->trusted[$token])) { | |
287 | $now = new \DateTime(); | |
288 | $validUntil = new \DateTime($this->trusted[$token]); | |
289 | ||
290 | return $now < $validUntil; | |
291 | } | |
292 | ||
293 | return false; | |
294 | } | |
295 | ||
296 | /** | |
297 | * @param Client $client | |
298 | * | |
299 | * @return User | |
300 | */ | |
301 | public function addClient(Client $client) | |
302 | { | |
303 | $this->clients[] = $client; | |
304 | ||
305 | return $this; | |
306 | } | |
307 | ||
308 | /** | |
309 | * @return ArrayCollection<Entry> | |
310 | */ | |
311 | public function getClients() | |
312 | { | |
313 | return $this->clients; | |
314 | } | |
315 | ||
316 | /** | |
317 | * 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). | |
318 | * | |
319 | * @return Client | |
320 | */ | |
321 | public function getFirstClient() | |
322 | { | |
323 | if (!empty($this->clients)) { | |
324 | return $this->clients->first(); | |
325 | } | |
326 | } | |
327 | ||
328 | /** | |
329 | * @return ArrayCollection<NotificationInterface> | |
330 | */ | |
331 | public function getNotifications() | |
332 | { | |
333 | return $this->notifications; | |
334 | } | |
335 | ||
336 | /** | |
337 | * @param ArrayCollection<NotificationInterface> $notifications | |
338 | */ | |
339 | public function setNotifications($notifications) | |
340 | { | |
341 | $this->notifications = $notifications; | |
342 | } | |
343 | } |