]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
2factor authentication via email
[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 use Wallabag\CoreBundle\Entity\Tag;
17
18 /**
19 * User.
20 *
21 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
22 * @ORM\Table
23 * @ORM\HasLifecycleCallbacks()
24 * @ExclusionPolicy("all")
25 *
26 * @UniqueEntity("email")
27 * @UniqueEntity("username")
28 */
29 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
30 {
31 /**
32 * @var int
33 *
34 * @Expose
35 * @ORM\Column(name="id", type="integer")
36 * @ORM\Id
37 * @ORM\GeneratedValue(strategy="AUTO")
38 */
39 protected $id;
40
41 /**
42 * @var string
43 *
44 * @ORM\Column(name="name", type="text", nullable=true)
45 */
46 protected $name;
47
48 /**
49 * @var date
50 *
51 * @ORM\Column(name="created_at", type="datetime")
52 */
53 protected $createdAt;
54
55 /**
56 * @var date
57 *
58 * @ORM\Column(name="updated_at", type="datetime")
59 */
60 protected $updatedAt;
61
62 /**
63 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
64 */
65 protected $entries;
66
67 /**
68 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user")
69 */
70 protected $config;
71
72 /**
73 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Tag", mappedBy="user", cascade={"remove"})
74 */
75 protected $tags;
76
77 /**
78 * @ORM\Column(type="integer", nullable=true)
79 */
80 private $authCode;
81
82 /**
83 * @var bool Enabled yes/no
84 * @ORM\Column(type="boolean")
85 */
86 private $twoFactorAuthentication = false;
87
88 /**
89 * @ORM\Column(type="json_array", nullable=true)
90 */
91 private $trusted;
92
93 public function __construct()
94 {
95 parent::__construct();
96 $this->entries = new ArrayCollection();
97 $this->tags = 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 /**
175 * @param Entry $entry
176 *
177 * @return User
178 */
179 public function addTag(Tag $tag)
180 {
181 $this->tags[] = $tag;
182
183 return $this;
184 }
185
186 /**
187 * @return ArrayCollection<Tag>
188 */
189 public function getTags()
190 {
191 return $this->tags;
192 }
193
194 public function isEqualTo(UserInterface $user)
195 {
196 return $this->username === $user->getUsername();
197 }
198
199 /**
200 * Set config.
201 *
202 * @param Config $config
203 *
204 * @return User
205 */
206 public function setConfig(Config $config = null)
207 {
208 $this->config = $config;
209
210 return $this;
211 }
212
213 /**
214 * Get config.
215 *
216 * @return Config
217 */
218 public function getConfig()
219 {
220 return $this->config;
221 }
222
223 /**
224 * @return bool
225 */
226 public function isTwoFactorAuthentication()
227 {
228 return $this->twoFactorAuthentication;
229 }
230
231 /**
232 * @param bool $twoFactorAuthentication
233 */
234 public function setTwoFactorAuthentication($twoFactorAuthentication)
235 {
236 $this->twoFactorAuthentication = $twoFactorAuthentication;
237 }
238
239 public function isEmailAuthEnabled()
240 {
241 return $this->twoFactorAuthentication;
242 }
243
244 public function getEmailAuthCode()
245 {
246 return $this->authCode;
247 }
248
249 public function setEmailAuthCode($authCode)
250 {
251 $this->authCode = $authCode;
252 }
253
254 public function addTrustedComputer($token, \DateTime $validUntil)
255 {
256 $this->trusted[$token] = $validUntil->format('r');
257 }
258
259 public function isTrustedComputer($token)
260 {
261 if (isset($this->trusted[$token])) {
262 $now = new \DateTime();
263 $validUntil = new \DateTime($this->trusted[$token]);
264
265 return $now < $validUntil;
266 }
267
268 return false;
269 }
270 }