]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
Create a client when creating a user using the api
[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 JMS\Serializer\Annotation\Accessor;
10 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
11 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
12 use FOS\UserBundle\Model\User as BaseUser;
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 * @ORM\Column(type="integer", nullable=true)
97 */
98 private $authCode;
99
100 /**
101 * @var bool
102 *
103 * @ORM\Column(type="boolean")
104 */
105 private $twoFactorAuthentication = false;
106
107 /**
108 * @ORM\Column(type="json_array", nullable=true)
109 */
110 private $trusted;
111
112 /**
113 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
114 */
115 protected $clients;
116
117 /**
118 * @see getFirstClient() below
119 *
120 * @Groups({"user_api_with_client"})
121 * @Accessor(getter="getFirstClient")
122 */
123 protected $default_client;
124
125 public function __construct()
126 {
127 parent::__construct();
128 $this->entries = new ArrayCollection();
129 $this->roles = ['ROLE_USER'];
130 }
131
132 /**
133 * @ORM\PrePersist
134 * @ORM\PreUpdate
135 */
136 public function timestamps()
137 {
138 if (is_null($this->createdAt)) {
139 $this->createdAt = new \DateTime();
140 }
141
142 $this->updatedAt = new \DateTime();
143 }
144
145 /**
146 * Set name.
147 *
148 * @param string $name
149 *
150 * @return User
151 */
152 public function setName($name)
153 {
154 $this->name = $name;
155
156 return $this;
157 }
158
159 /**
160 * Get name.
161 *
162 * @return string
163 */
164 public function getName()
165 {
166 return $this->name;
167 }
168
169 /**
170 * @return \DateTime
171 */
172 public function getCreatedAt()
173 {
174 return $this->createdAt;
175 }
176
177 /**
178 * @return \DateTime
179 */
180 public function getUpdatedAt()
181 {
182 return $this->updatedAt;
183 }
184
185 /**
186 * @param Entry $entry
187 *
188 * @return User
189 */
190 public function addEntry(Entry $entry)
191 {
192 $this->entries[] = $entry;
193
194 return $this;
195 }
196
197 /**
198 * @return ArrayCollection<Entry>
199 */
200 public function getEntries()
201 {
202 return $this->entries;
203 }
204
205 public function isEqualTo(UserInterface $user)
206 {
207 return $this->username === $user->getUsername();
208 }
209
210 /**
211 * Set config.
212 *
213 * @param Config $config
214 *
215 * @return User
216 */
217 public function setConfig(Config $config = null)
218 {
219 $this->config = $config;
220
221 return $this;
222 }
223
224 /**
225 * Get config.
226 *
227 * @return Config
228 */
229 public function getConfig()
230 {
231 return $this->config;
232 }
233
234 /**
235 * @return bool
236 */
237 public function isTwoFactorAuthentication()
238 {
239 return $this->twoFactorAuthentication;
240 }
241
242 /**
243 * @param bool $twoFactorAuthentication
244 */
245 public function setTwoFactorAuthentication($twoFactorAuthentication)
246 {
247 $this->twoFactorAuthentication = $twoFactorAuthentication;
248 }
249
250 public function isEmailAuthEnabled()
251 {
252 return $this->twoFactorAuthentication;
253 }
254
255 public function getEmailAuthCode()
256 {
257 return $this->authCode;
258 }
259
260 public function setEmailAuthCode($authCode)
261 {
262 $this->authCode = $authCode;
263 }
264
265 public function addTrustedComputer($token, \DateTime $validUntil)
266 {
267 $this->trusted[$token] = $validUntil->format('r');
268 }
269
270 public function isTrustedComputer($token)
271 {
272 if (isset($this->trusted[$token])) {
273 $now = new \DateTime();
274 $validUntil = new \DateTime($this->trusted[$token]);
275
276 return $now < $validUntil;
277 }
278
279 return false;
280 }
281
282 /**
283 * @param Client $client
284 *
285 * @return User
286 */
287 public function addClient(Client $client)
288 {
289 $this->clients[] = $client;
290
291 return $this;
292 }
293
294 /**
295 * @return ArrayCollection<Entry>
296 */
297 public function getClients()
298 {
299 return $this->clients;
300 }
301
302 /**
303 * 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).
304 *
305 * @return Client
306 */
307 public function getFirstClient()
308 {
309 if (empty($this->clients)) {
310 return $this->clients;
311 }
312
313 return $this->clients->first();
314 }
315 }