]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
6e305719fb0cd2daf4e506bb886ad7cc1f16fcb9
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
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 as EmailTwoFactorInterface;
12 use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface as GoogleTwoFactorInterface;
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 EmailTwoFactorInterface, GoogleTwoFactorInterface
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 * @ORM\Column(name="googleAuthenticatorSecret", type="string", nullable=true)
127 */
128 private $googleAuthenticatorSecret;
129
130 /**
131 * @var bool
132 *
133 * @ORM\Column(type="boolean")
134 */
135 private $emailTwoFactor = false;
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 isEmailTwoFactor()
237 {
238 return $this->emailTwoFactor;
239 }
240
241 /**
242 * @param bool $emailTwoFactor
243 */
244 public function setEmailTwoFactor($emailTwoFactor)
245 {
246 $this->emailTwoFactor = $emailTwoFactor;
247 }
248
249 /**
250 * Used in the user config form to be "like" the email option.
251 */
252 public function isGoogleTwoFactor()
253 {
254 return $this->isGoogleAuthenticatorEnabled();
255 }
256
257 /**
258 * {@inheritdoc}
259 */
260 public function isEmailAuthEnabled(): bool
261 {
262 return $this->emailTwoFactor;
263 }
264
265 /**
266 * {@inheritdoc}
267 */
268 public function getEmailAuthCode(): string
269 {
270 return $this->authCode;
271 }
272
273 /**
274 * {@inheritdoc}
275 */
276 public function setEmailAuthCode(string $authCode): void
277 {
278 $this->authCode = $authCode;
279 }
280
281 /**
282 * {@inheritdoc}
283 */
284 public function getEmailAuthRecipient(): string
285 {
286 return $this->email;
287 }
288
289 /**
290 * {@inheritdoc}
291 */
292 public function isGoogleAuthenticatorEnabled(): bool
293 {
294 return $this->googleAuthenticatorSecret ? true : false;
295 }
296
297 /**
298 * {@inheritdoc}
299 */
300 public function getGoogleAuthenticatorUsername(): string
301 {
302 return $this->username;
303 }
304
305 /**
306 * {@inheritdoc}
307 */
308 public function getGoogleAuthenticatorSecret(): string
309 {
310 return $this->googleAuthenticatorSecret;
311 }
312
313 /**
314 * {@inheritdoc}
315 */
316 public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
317 {
318 $this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
319 }
320
321 /**
322 * @param Client $client
323 *
324 * @return User
325 */
326 public function addClient(Client $client)
327 {
328 $this->clients[] = $client;
329
330 return $this;
331 }
332
333 /**
334 * @return ArrayCollection<Entry>
335 */
336 public function getClients()
337 {
338 return $this->clients;
339 }
340
341 /**
342 * 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).
343 *
344 * @return Client
345 */
346 public function getFirstClient()
347 {
348 if (!empty($this->clients)) {
349 return $this->clients->first();
350 }
351 }
352 }