]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
Merge pull request #3171 from wallabag/drop-fucking-sqlite
[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 JMS\Serializer\Annotation\Groups;
8 use JMS\Serializer\Annotation\XmlRoot;
9 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
10 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
11 use FOS\UserBundle\Model\User as BaseUser;
12 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
13 use Symfony\Component\Security\Core\User\UserInterface;
14 use Wallabag\ApiBundle\Entity\Client;
15 use Wallabag\CoreBundle\Entity\Config;
16 use Wallabag\CoreBundle\Entity\Entry;
17
18 /**
19 * User.
20 *
21 * @XmlRoot("user")
22 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
23 * @ORM\Table(name="`user`")
24 * @ORM\HasLifecycleCallbacks()
25 *
26 * @UniqueEntity("email")
27 * @UniqueEntity("username")
28 */
29 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
30 {
31 /** @Serializer\XmlAttribute */
32 /**
33 * @var int
34 *
35 * @ORM\Column(name="id", type="integer")
36 * @ORM\Id
37 * @ORM\GeneratedValue(strategy="AUTO")
38 *
39 * @Groups({"user_api"})
40 */
41 protected $id;
42
43 /**
44 * @var string
45 *
46 * @ORM\Column(name="name", type="text", nullable=true)
47 *
48 * @Groups({"user_api"})
49 */
50 protected $name;
51
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
66 /**
67 * @var \DateTime
68 *
69 * @ORM\Column(name="created_at", type="datetime")
70 *
71 * @Groups({"user_api"})
72 */
73 protected $createdAt;
74
75 /**
76 * @var \DateTime
77 *
78 * @ORM\Column(name="updated_at", type="datetime")
79 *
80 * @Groups({"user_api"})
81 */
82 protected $updatedAt;
83
84 /**
85 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
86 */
87 protected $entries;
88
89 /**
90 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
91 */
92 protected $config;
93
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
110 /**
111 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
112 */
113 protected $clients;
114
115 public function __construct()
116 {
117 parent::__construct();
118 $this->entries = new ArrayCollection();
119 $this->roles = ['ROLE_USER'];
120 }
121
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
135 /**
136 * Set name.
137 *
138 * @param string $name
139 *
140 * @return User
141 */
142 public function setName($name)
143 {
144 $this->name = $name;
145
146 return $this;
147 }
148
149 /**
150 * Get name.
151 *
152 * @return string
153 */
154 public function getName()
155 {
156 return $this->name;
157 }
158
159 /**
160 * @return \DateTime
161 */
162 public function getCreatedAt()
163 {
164 return $this->createdAt;
165 }
166
167 /**
168 * @return \DateTime
169 */
170 public function getUpdatedAt()
171 {
172 return $this->updatedAt;
173 }
174
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
195 public function isEqualTo(UserInterface $user)
196 {
197 return $this->username === $user->getUsername();
198 }
199
200 /**
201 * Set config.
202 *
203 * @param Config $config
204 *
205 * @return User
206 */
207 public function setConfig(Config $config = null)
208 {
209 $this->config = $config;
210
211 return $this;
212 }
213
214 /**
215 * Get config.
216 *
217 * @return Config
218 */
219 public function getConfig()
220 {
221 return $this->config;
222 }
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 }
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 }
291 }