]>
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 | /** | |
6a50b4cc | 101 | * @ORM\ManyToMany(targetEntity="Wallabag\GroupBundle\Entity\Group", inversedBy="users", cascade={"persist"}) |
f1900b68 NL |
102 | * @ORM\JoinTable(name="user_group", |
103 | * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, | |
104 | * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")} | |
105 | * ) | |
106 | */ | |
107 | protected $groups; | |
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(); |
4094ea47 | 139 | $this->roles = ['ROLE_USER']; |
c3235553 | 140 | } |
9d50517c | 141 | |
2f69eb4a NL |
142 | /** |
143 | * @ORM\PrePersist | |
144 | * @ORM\PreUpdate | |
145 | */ | |
146 | public function timestamps() | |
147 | { | |
148 | if (is_null($this->createdAt)) { | |
149 | $this->createdAt = new \DateTime(); | |
150 | } | |
151 | ||
152 | $this->updatedAt = new \DateTime(); | |
153 | } | |
154 | ||
9d50517c | 155 | /** |
4346a860 JB |
156 | * Set name. |
157 | * | |
158 | * @param string $name | |
9d50517c | 159 | * |
2f69eb4a | 160 | * @return User |
9d50517c NL |
161 | */ |
162 | public function setName($name) | |
163 | { | |
164 | $this->name = $name; | |
165 | ||
166 | return $this; | |
167 | } | |
168 | ||
169 | /** | |
4346a860 | 170 | * Get name. |
9d50517c | 171 | * |
7df80cb3 | 172 | * @return string |
9d50517c NL |
173 | */ |
174 | public function getName() | |
175 | { | |
176 | return $this->name; | |
177 | } | |
178 | ||
2f69eb4a | 179 | /** |
3a6af6c5 | 180 | * @return \DateTime |
2f69eb4a NL |
181 | */ |
182 | public function getCreatedAt() | |
183 | { | |
184 | return $this->createdAt; | |
185 | } | |
186 | ||
187 | /** | |
3a6af6c5 | 188 | * @return \DateTime |
2f69eb4a NL |
189 | */ |
190 | public function getUpdatedAt() | |
191 | { | |
192 | return $this->updatedAt; | |
193 | } | |
194 | ||
5f09650e NL |
195 | /** |
196 | * @param Entry $entry | |
197 | * | |
198 | * @return User | |
199 | */ | |
200 | public function addEntry(Entry $entry) | |
201 | { | |
202 | $this->entries[] = $entry; | |
203 | ||
204 | return $this; | |
205 | } | |
206 | ||
207 | /** | |
208 | * @return ArrayCollection<Entry> | |
209 | */ | |
210 | public function getEntries() | |
211 | { | |
212 | return $this->entries; | |
213 | } | |
214 | ||
c3235553 NL |
215 | public function isEqualTo(UserInterface $user) |
216 | { | |
217 | return $this->username === $user->getUsername(); | |
218 | } | |
219 | ||
32da2a70 | 220 | /** |
4346a860 JB |
221 | * Set config. |
222 | * | |
1210dae1 | 223 | * @param Config $config |
32da2a70 | 224 | * |
32da2a70 J |
225 | * @return User |
226 | */ | |
1210dae1 | 227 | public function setConfig(Config $config = null) |
32da2a70 J |
228 | { |
229 | $this->config = $config; | |
230 | ||
231 | return $this; | |
232 | } | |
233 | ||
234 | /** | |
4346a860 | 235 | * Get config. |
32da2a70 | 236 | * |
1210dae1 | 237 | * @return Config |
32da2a70 J |
238 | */ |
239 | public function getConfig() | |
240 | { | |
241 | return $this->config; | |
242 | } | |
2db616b5 NL |
243 | |
244 | /** | |
245 | * @return bool | |
246 | */ | |
247 | public function isTwoFactorAuthentication() | |
248 | { | |
249 | return $this->twoFactorAuthentication; | |
250 | } | |
251 | ||
252 | /** | |
253 | * @param bool $twoFactorAuthentication | |
254 | */ | |
255 | public function setTwoFactorAuthentication($twoFactorAuthentication) | |
256 | { | |
257 | $this->twoFactorAuthentication = $twoFactorAuthentication; | |
258 | } | |
259 | ||
260 | public function isEmailAuthEnabled() | |
261 | { | |
262 | return $this->twoFactorAuthentication; | |
263 | } | |
264 | ||
265 | public function getEmailAuthCode() | |
266 | { | |
267 | return $this->authCode; | |
268 | } | |
269 | ||
270 | public function setEmailAuthCode($authCode) | |
271 | { | |
272 | $this->authCode = $authCode; | |
273 | } | |
274 | ||
275 | public function addTrustedComputer($token, \DateTime $validUntil) | |
276 | { | |
277 | $this->trusted[$token] = $validUntil->format('r'); | |
278 | } | |
279 | ||
280 | public function isTrustedComputer($token) | |
281 | { | |
282 | if (isset($this->trusted[$token])) { | |
283 | $now = new \DateTime(); | |
284 | $validUntil = new \DateTime($this->trusted[$token]); | |
285 | ||
286 | return $now < $validUntil; | |
287 | } | |
288 | ||
289 | return false; | |
290 | } | |
23406ca3 NL |
291 | |
292 | /** | |
293 | * @param Client $client | |
294 | * | |
295 | * @return User | |
296 | */ | |
297 | public function addClient(Client $client) | |
298 | { | |
299 | $this->clients[] = $client; | |
300 | ||
301 | return $this; | |
302 | } | |
303 | ||
304 | /** | |
305 | * @return ArrayCollection<Entry> | |
306 | */ | |
307 | public function getClients() | |
308 | { | |
309 | return $this->clients; | |
310 | } | |
0c00e525 JB |
311 | |
312 | /** | |
313 | * 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). | |
314 | * | |
315 | * @return Client | |
316 | */ | |
317 | public function getFirstClient() | |
318 | { | |
eb570e49 JB |
319 | if (!empty($this->clients)) { |
320 | return $this->clients->first(); | |
0c00e525 | 321 | } |
56c81a2f NL |
322 | |
323 | } | |
324 | ||
325 | /** | |
326 | * @param string $name | |
327 | * @return bool | |
328 | */ | |
329 | public function hasGroup($name = '') | |
330 | { | |
331 | return in_array($name, $this->getGroupNames()); | |
0c00e525 | 332 | } |
9d50517c | 333 | } |