]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
Add a real configuration for CS-Fixer
[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;
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
19 /**
20 * User.
21 *
22 * @XmlRoot("user")
23 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
24 * @ORM\Table(name="`user`")
25 * @ORM\HasLifecycleCallbacks()
26 *
27 * @UniqueEntity("email")
28 * @UniqueEntity("username")
29 */
30 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
31 {
32 /** @Serializer\XmlAttribute */
33 /**
34 * @var int
35 *
36 * @ORM\Column(name="id", type="integer")
37 * @ORM\Id
38 * @ORM\GeneratedValue(strategy="AUTO")
39 *
40 * @Groups({"user_api", "user_api_with_client"})
41 */
42 protected $id;
43
44 /**
45 * @var string
46 *
47 * @ORM\Column(name="name", type="text", nullable=true)
48 *
49 * @Groups({"user_api", "user_api_with_client"})
50 */
51 protected $name;
52
53 /**
54 * @var string
55 *
56 * @Groups({"user_api", "user_api_with_client"})
57 */
58 protected $username;
59
60 /**
61 * @var string
62 *
63 * @Groups({"user_api", "user_api_with_client"})
64 */
65 protected $email;
66
67 /**
68 * @var \DateTime
69 *
70 * @ORM\Column(name="created_at", type="datetime")
71 *
72 * @Groups({"user_api", "user_api_with_client"})
73 */
74 protected $createdAt;
75
76 /**
77 * @var \DateTime
78 *
79 * @ORM\Column(name="updated_at", type="datetime")
80 *
81 * @Groups({"user_api", "user_api_with_client"})
82 */
83 protected $updatedAt;
84
85 /**
86 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
87 */
88 protected $entries;
89
90 /**
91 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
92 */
93 protected $config;
94
95 /**
96 * @var ArrayCollection
97 *
98 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
99 */
100 protected $clients;
101
102 /**
103 * @see getFirstClient() below
104 *
105 * @Groups({"user_api_with_client"})
106 * @Accessor(getter="getFirstClient")
107 */
108 protected $default_client;
109
110 /**
111 * @ORM\Column(type="integer", nullable=true)
112 */
113 private $authCode;
114
115 /**
116 * @var bool
117 *
118 * @ORM\Column(type="boolean")
119 */
120 private $twoFactorAuthentication = false;
121
122 /**
123 * @ORM\Column(type="json_array", nullable=true)
124 */
125 private $trusted;
126
127 public function __construct()
128 {
129 parent::__construct();
130 $this->entries = new ArrayCollection();
131 $this->roles = ['ROLE_USER'];
132 }
133
134 /**
135 * @ORM\PrePersist
136 * @ORM\PreUpdate
137 */
138 public function timestamps()
139 {
140 if (null === $this->createdAt) {
141 $this->createdAt = new \DateTime();
142 }
143
144 $this->updatedAt = new \DateTime();
145 }
146
147 /**
148 * Set name.
149 *
150 * @param string $name
151 *
152 * @return User
153 */
154 public function setName($name)
155 {
156 $this->name = $name;
157
158 return $this;
159 }
160
161 /**
162 * Get name.
163 *
164 * @return string
165 */
166 public function getName()
167 {
168 return $this->name;
169 }
170
171 /**
172 * @return \DateTime
173 */
174 public function getCreatedAt()
175 {
176 return $this->createdAt;
177 }
178
179 /**
180 * @return \DateTime
181 */
182 public function getUpdatedAt()
183 {
184 return $this->updatedAt;
185 }
186
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
207 public function isEqualTo(UserInterface $user)
208 {
209 return $this->username === $user->getUsername();
210 }
211
212 /**
213 * Set config.
214 *
215 * @param Config $config
216 *
217 * @return User
218 */
219 public function setConfig(Config $config = null)
220 {
221 $this->config = $config;
222
223 return $this;
224 }
225
226 /**
227 * Get config.
228 *
229 * @return Config
230 */
231 public function getConfig()
232 {
233 return $this->config;
234 }
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 }
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 }
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 {
311 if (!empty($this->clients)) {
312 return $this->clients->first();
313 }
314 }
315 }