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