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