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