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