]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Entity/User.php
assign tags to entries and add lastPocketImport attribute to user
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
CommitLineData
9d50517c
NL
1<?php
2
1210dae1 3namespace Wallabag\UserBundle\Entity;
9d50517c 4
5f09650e 5use Doctrine\Common\Collections\ArrayCollection;
9d50517c 6use Doctrine\ORM\Mapping as ORM;
2db616b5
NL
7use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
8use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
c844dc0c 9use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
c3235553 10use Symfony\Component\Security\Core\User\UserInterface;
0f006880
NL
11use JMS\Serializer\Annotation\ExclusionPolicy;
12use JMS\Serializer\Annotation\Expose;
a1691859 13use FOS\UserBundle\Model\User as BaseUser;
1210dae1
NL
14use Wallabag\CoreBundle\Entity\Config;
15use Wallabag\CoreBundle\Entity\Entry;
9d50517c
NL
16
17/**
4346a860 18 * User.
9d50517c 19 *
1210dae1 20 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
bd0f3d32 21 * @ORM\Table(name="`user`")
2f69eb4a 22 * @ORM\HasLifecycleCallbacks()
0f006880 23 * @ExclusionPolicy("all")
c844dc0c
J
24 *
25 * @UniqueEntity("email")
26 * @UniqueEntity("username")
9d50517c 27 */
2db616b5 28class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
9d50517c
NL
29{
30 /**
4346a860 31 * @var int
9d50517c 32 *
0f006880 33 * @Expose
2f69eb4a 34 * @ORM\Column(name="id", type="integer")
9d50517c 35 * @ORM\Id
2f69eb4a 36 * @ORM\GeneratedValue(strategy="AUTO")
9d50517c 37 */
a1691859 38 protected $id;
9d50517c
NL
39
40 /**
41 * @var string
42 *
43 * @ORM\Column(name="name", type="text", nullable=true)
44 */
a1691859 45 protected $name;
6894d48e 46
2f69eb4a
NL
47 /**
48 * @var date
49 *
50 * @ORM\Column(name="created_at", type="datetime")
51 */
a1691859 52 protected $createdAt;
2f69eb4a
NL
53
54 /**
55 * @var date
56 *
57 * @ORM\Column(name="updated_at", type="datetime")
58 */
a1691859 59 protected $updatedAt;
2f69eb4a 60
5f09650e 61 /**
1210dae1 62 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
5f09650e 63 */
a1691859 64 protected $entries;
5f09650e 65
32da2a70 66 /**
1210dae1 67 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user")
32da2a70 68 */
a1691859 69 protected $config;
32da2a70 70
2db616b5
NL
71 /**
72 * @ORM\Column(type="integer", nullable=true)
73 */
74 private $authCode;
75
76 /**
77 * @var bool Enabled yes/no
78 * @ORM\Column(type="boolean")
79 */
80 private $twoFactorAuthentication = false;
81
82 /**
83 * @ORM\Column(type="json_array", nullable=true)
84 */
85 private $trusted;
86
87f23b00
NL
87 /**
88 * @var date
89 *
90 * @ORM\Column(name="last_pocket_import", type="datetime", nullable=true)
91 */
92 private $lastPocketImport;
93
c3235553
NL
94 public function __construct()
95 {
a1691859 96 parent::__construct();
98f0929f 97 $this->entries = new ArrayCollection();
fcb1fba5 98 $this->roles = array('ROLE_USER');
c3235553 99 }
9d50517c 100
2f69eb4a
NL
101 /**
102 * @ORM\PrePersist
103 * @ORM\PreUpdate
104 */
105 public function timestamps()
106 {
107 if (is_null($this->createdAt)) {
108 $this->createdAt = new \DateTime();
109 }
110
111 $this->updatedAt = new \DateTime();
112 }
113
9d50517c 114 /**
4346a860
JB
115 * Set name.
116 *
117 * @param string $name
9d50517c 118 *
2f69eb4a 119 * @return User
9d50517c
NL
120 */
121 public function setName($name)
122 {
123 $this->name = $name;
124
125 return $this;
126 }
127
128 /**
4346a860 129 * Get name.
9d50517c 130 *
7df80cb3 131 * @return string
9d50517c
NL
132 */
133 public function getName()
134 {
135 return $this->name;
136 }
137
2f69eb4a
NL
138 /**
139 * @return string
140 */
141 public function getCreatedAt()
142 {
143 return $this->createdAt;
144 }
145
146 /**
147 * @return string
148 */
149 public function getUpdatedAt()
150 {
151 return $this->updatedAt;
152 }
153
5f09650e
NL
154 /**
155 * @param Entry $entry
156 *
157 * @return User
158 */
159 public function addEntry(Entry $entry)
160 {
161 $this->entries[] = $entry;
162
163 return $this;
164 }
165
166 /**
167 * @return ArrayCollection<Entry>
168 */
169 public function getEntries()
170 {
171 return $this->entries;
172 }
173
c3235553
NL
174 public function isEqualTo(UserInterface $user)
175 {
176 return $this->username === $user->getUsername();
177 }
178
32da2a70 179 /**
4346a860
JB
180 * Set config.
181 *
1210dae1 182 * @param Config $config
32da2a70 183 *
32da2a70
J
184 * @return User
185 */
1210dae1 186 public function setConfig(Config $config = null)
32da2a70
J
187 {
188 $this->config = $config;
189
190 return $this;
191 }
192
193 /**
4346a860 194 * Get config.
32da2a70 195 *
1210dae1 196 * @return Config
32da2a70
J
197 */
198 public function getConfig()
199 {
200 return $this->config;
201 }
2db616b5
NL
202
203 /**
204 * @return bool
205 */
206 public function isTwoFactorAuthentication()
207 {
208 return $this->twoFactorAuthentication;
209 }
210
211 /**
212 * @param bool $twoFactorAuthentication
213 */
214 public function setTwoFactorAuthentication($twoFactorAuthentication)
215 {
216 $this->twoFactorAuthentication = $twoFactorAuthentication;
217 }
218
219 public function isEmailAuthEnabled()
220 {
221 return $this->twoFactorAuthentication;
222 }
223
224 public function getEmailAuthCode()
225 {
226 return $this->authCode;
227 }
228
229 public function setEmailAuthCode($authCode)
230 {
231 $this->authCode = $authCode;
232 }
233
234 public function addTrustedComputer($token, \DateTime $validUntil)
235 {
236 $this->trusted[$token] = $validUntil->format('r');
237 }
238
239 public function isTrustedComputer($token)
240 {
241 if (isset($this->trusted[$token])) {
242 $now = new \DateTime();
243 $validUntil = new \DateTime($this->trusted[$token]);
244
245 return $now < $validUntil;
246 }
247
248 return false;
249 }
87f23b00
NL
250
251 /**
252 * @return date
253 */
254 public function getLastPocketImport()
255 {
256 return $this->lastPocketImport;
257 }
258
259 /**
260 * @param date $lastPocketImport
261 */
262 public function setLastPocketImport($lastPocketImport)
263 {
264 $this->lastPocketImport = $lastPocketImport;
265 }
9d50517c 266}