]> git.immae.eu Git - github/wallabag/wallabag.git/blob - 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
1 <?php
2
3 namespace Wallabag\UserBundle\Entity;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\ORM\Mapping as ORM;
7 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
8 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
9 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
10 use Symfony\Component\Security\Core\User\UserInterface;
11 use JMS\Serializer\Annotation\ExclusionPolicy;
12 use JMS\Serializer\Annotation\Expose;
13 use FOS\UserBundle\Model\User as BaseUser;
14 use Wallabag\CoreBundle\Entity\Config;
15 use Wallabag\CoreBundle\Entity\Entry;
16
17 /**
18 * User.
19 *
20 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
21 * @ORM\Table(name="`user`")
22 * @ORM\HasLifecycleCallbacks()
23 * @ExclusionPolicy("all")
24 *
25 * @UniqueEntity("email")
26 * @UniqueEntity("username")
27 */
28 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
29 {
30 /**
31 * @var int
32 *
33 * @Expose
34 * @ORM\Column(name="id", type="integer")
35 * @ORM\Id
36 * @ORM\GeneratedValue(strategy="AUTO")
37 */
38 protected $id;
39
40 /**
41 * @var string
42 *
43 * @ORM\Column(name="name", type="text", nullable=true)
44 */
45 protected $name;
46
47 /**
48 * @var date
49 *
50 * @ORM\Column(name="created_at", type="datetime")
51 */
52 protected $createdAt;
53
54 /**
55 * @var date
56 *
57 * @ORM\Column(name="updated_at", type="datetime")
58 */
59 protected $updatedAt;
60
61 /**
62 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
63 */
64 protected $entries;
65
66 /**
67 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user")
68 */
69 protected $config;
70
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
87 /**
88 * @var date
89 *
90 * @ORM\Column(name="last_pocket_import", type="datetime", nullable=true)
91 */
92 private $lastPocketImport;
93
94 public function __construct()
95 {
96 parent::__construct();
97 $this->entries = new ArrayCollection();
98 $this->roles = array('ROLE_USER');
99 }
100
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
114 /**
115 * Set name.
116 *
117 * @param string $name
118 *
119 * @return User
120 */
121 public function setName($name)
122 {
123 $this->name = $name;
124
125 return $this;
126 }
127
128 /**
129 * Get name.
130 *
131 * @return string
132 */
133 public function getName()
134 {
135 return $this->name;
136 }
137
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
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
174 public function isEqualTo(UserInterface $user)
175 {
176 return $this->username === $user->getUsername();
177 }
178
179 /**
180 * Set config.
181 *
182 * @param Config $config
183 *
184 * @return User
185 */
186 public function setConfig(Config $config = null)
187 {
188 $this->config = $config;
189
190 return $this;
191 }
192
193 /**
194 * Get config.
195 *
196 * @return Config
197 */
198 public function getConfig()
199 {
200 return $this->config;
201 }
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 }
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 }
266 }