]>
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\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"}) | |
92 | */ | |
93 | protected $config; | |
94 | ||
95 | /** | |
96 | * @var ArrayCollection | |
97 | * | |
98 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"}) | |
99 | */ | |
100 | protected $siteCredentials; | |
101 | ||
102 | /** | |
103 | * @var ArrayCollection | |
104 | * | |
105 | * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"}) | |
106 | */ | |
107 | protected $clients; | |
108 | ||
109 | /** | |
110 | * @see getFirstClient() below | |
111 | * | |
112 | * @Groups({"user_api_with_client"}) | |
113 | * @Accessor(getter="getFirstClient") | |
114 | */ | |
115 | protected $default_client; | |
116 | ||
117 | /** | |
118 | * @ORM\Column(type="integer", nullable=true) | |
119 | */ | |
120 | private $authCode; | |
121 | ||
122 | /** | |
123 | * @var bool | |
124 | * | |
125 | * @ORM\Column(type="boolean") | |
126 | */ | |
127 | private $twoFactorAuthentication = false; | |
128 | ||
129 | /** | |
130 | * @ORM\Column(type="json_array", nullable=true) | |
131 | */ | |
132 | private $trusted; | |
133 | ||
134 | public function __construct() | |
135 | { | |
136 | parent::__construct(); | |
137 | $this->entries = new ArrayCollection(); | |
138 | $this->roles = ['ROLE_USER']; | |
139 | } | |
140 | ||
141 | /** | |
142 | * @ORM\PrePersist | |
143 | * @ORM\PreUpdate | |
144 | */ | |
145 | public function timestamps() | |
146 | { | |
147 | if (null === $this->createdAt) { | |
148 | $this->createdAt = new \DateTime(); | |
149 | } | |
150 | ||
151 | $this->updatedAt = new \DateTime(); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Set name. | |
156 | * | |
157 | * @param string $name | |
158 | * | |
159 | * @return User | |
160 | */ | |
161 | public function setName($name) | |
162 | { | |
163 | $this->name = $name; | |
164 | ||
165 | return $this; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Get name. | |
170 | * | |
171 | * @return string | |
172 | */ | |
173 | public function getName() | |
174 | { | |
175 | return $this->name; | |
176 | } | |
177 | ||
178 | /** | |
179 | * @return \DateTime | |
180 | */ | |
181 | public function getCreatedAt() | |
182 | { | |
183 | return $this->createdAt; | |
184 | } | |
185 | ||
186 | /** | |
187 | * @return \DateTime | |
188 | */ | |
189 | public function getUpdatedAt() | |
190 | { | |
191 | return $this->updatedAt; | |
192 | } | |
193 | ||
194 | /** | |
195 | * @param Entry $entry | |
196 | * | |
197 | * @return User | |
198 | */ | |
199 | public function addEntry(Entry $entry) | |
200 | { | |
201 | $this->entries[] = $entry; | |
202 | ||
203 | return $this; | |
204 | } | |
205 | ||
206 | /** | |
207 | * @return ArrayCollection<Entry> | |
208 | */ | |
209 | public function getEntries() | |
210 | { | |
211 | return $this->entries; | |
212 | } | |
213 | ||
214 | public function isEqualTo(UserInterface $user) | |
215 | { | |
216 | return $this->username === $user->getUsername(); | |
217 | } | |
218 | ||
219 | /** | |
220 | * Set config. | |
221 | * | |
222 | * @param Config $config | |
223 | * | |
224 | * @return User | |
225 | */ | |
226 | public function setConfig(Config $config = null) | |
227 | { | |
228 | $this->config = $config; | |
229 | ||
230 | return $this; | |
231 | } | |
232 | ||
233 | /** | |
234 | * Get config. | |
235 | * | |
236 | * @return Config | |
237 | */ | |
238 | public function getConfig() | |
239 | { | |
240 | return $this->config; | |
241 | } | |
242 | ||
243 | /** | |
244 | * @return bool | |
245 | */ | |
246 | public function isTwoFactorAuthentication() | |
247 | { | |
248 | return $this->twoFactorAuthentication; | |
249 | } | |
250 | ||
251 | /** | |
252 | * @param bool $twoFactorAuthentication | |
253 | */ | |
254 | public function setTwoFactorAuthentication($twoFactorAuthentication) | |
255 | { | |
256 | $this->twoFactorAuthentication = $twoFactorAuthentication; | |
257 | } | |
258 | ||
259 | public function isEmailAuthEnabled() | |
260 | { | |
261 | return $this->twoFactorAuthentication; | |
262 | } | |
263 | ||
264 | public function getEmailAuthCode() | |
265 | { | |
266 | return $this->authCode; | |
267 | } | |
268 | ||
269 | public function setEmailAuthCode($authCode) | |
270 | { | |
271 | $this->authCode = $authCode; | |
272 | } | |
273 | ||
274 | public function addTrustedComputer($token, \DateTime $validUntil) | |
275 | { | |
276 | $this->trusted[$token] = $validUntil->format('r'); | |
277 | } | |
278 | ||
279 | public function isTrustedComputer($token) | |
280 | { | |
281 | if (isset($this->trusted[$token])) { | |
282 | $now = new \DateTime(); | |
283 | $validUntil = new \DateTime($this->trusted[$token]); | |
284 | ||
285 | return $now < $validUntil; | |
286 | } | |
287 | ||
288 | return false; | |
289 | } | |
290 | ||
291 | /** | |
292 | * @param Client $client | |
293 | * | |
294 | * @return User | |
295 | */ | |
296 | public function addClient(Client $client) | |
297 | { | |
298 | $this->clients[] = $client; | |
299 | ||
300 | return $this; | |
301 | } | |
302 | ||
303 | /** | |
304 | * @return ArrayCollection<Entry> | |
305 | */ | |
306 | public function getClients() | |
307 | { | |
308 | return $this->clients; | |
309 | } | |
310 | ||
311 | /** | |
312 | * 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). | |
313 | * | |
314 | * @return Client | |
315 | */ | |
316 | public function getFirstClient() | |
317 | { | |
318 | if (!empty($this->clients)) { | |
319 | return $this->clients->first(); | |
320 | } | |
321 | } | |
322 | } |