]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
Merge remote-tracking branch 'origin/master' into 2.2
[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 FOS\UserBundle\Model\User as BaseUser;
10 use JMS\Serializer\Annotation\ExclusionPolicy;
11 use JMS\Serializer\Annotation\Expose;
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 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
22 * @ORM\Table(name="`user`")
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", cascade={"remove"})
69 */
70 protected $config;
71
72 /**
73 * @ORM\Column(type="integer", nullable=true)
74 */
75 private $authCode;
76
77 /**
78 * @var bool Enabled yes/no
79 * @ORM\Column(type="boolean")
80 */
81 private $twoFactorAuthentication = false;
82
83 /**
84 * @ORM\Column(type="json_array", nullable=true)
85 */
86 private $trusted;
87
88 /**
89 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
90 */
91 protected $clients;
92
93 public function __construct()
94 {
95 parent::__construct();
96 $this->entries = new ArrayCollection();
97 $this->roles = ['ROLE_USER'];
98 }
99
100 /**
101 * @ORM\PrePersist
102 * @ORM\PreUpdate
103 */
104 public function timestamps()
105 {
106 if (is_null($this->createdAt)) {
107 $this->createdAt = new \DateTime();
108 }
109
110 $this->updatedAt = new \DateTime();
111 }
112
113 /**
114 * Set name.
115 *
116 * @param string $name
117 *
118 * @return User
119 */
120 public function setName($name)
121 {
122 $this->name = $name;
123
124 return $this;
125 }
126
127 /**
128 * Get name.
129 *
130 * @return string
131 */
132 public function getName()
133 {
134 return $this->name;
135 }
136
137 /**
138 * @return string
139 */
140 public function getCreatedAt()
141 {
142 return $this->createdAt;
143 }
144
145 /**
146 * @return string
147 */
148 public function getUpdatedAt()
149 {
150 return $this->updatedAt;
151 }
152
153 /**
154 * @param Entry $entry
155 *
156 * @return User
157 */
158 public function addEntry(Entry $entry)
159 {
160 $this->entries[] = $entry;
161
162 return $this;
163 }
164
165 /**
166 * @return ArrayCollection<Entry>
167 */
168 public function getEntries()
169 {
170 return $this->entries;
171 }
172
173 public function isEqualTo(UserInterface $user)
174 {
175 return $this->username === $user->getUsername();
176 }
177
178 /**
179 * Set config.
180 *
181 * @param Config $config
182 *
183 * @return User
184 */
185 public function setConfig(Config $config = null)
186 {
187 $this->config = $config;
188
189 return $this;
190 }
191
192 /**
193 * Get config.
194 *
195 * @return Config
196 */
197 public function getConfig()
198 {
199 return $this->config;
200 }
201
202 /**
203 * @return bool
204 */
205 public function isTwoFactorAuthentication()
206 {
207 return $this->twoFactorAuthentication;
208 }
209
210 /**
211 * @param bool $twoFactorAuthentication
212 */
213 public function setTwoFactorAuthentication($twoFactorAuthentication)
214 {
215 $this->twoFactorAuthentication = $twoFactorAuthentication;
216 }
217
218 public function isEmailAuthEnabled()
219 {
220 return $this->twoFactorAuthentication;
221 }
222
223 public function getEmailAuthCode()
224 {
225 return $this->authCode;
226 }
227
228 public function setEmailAuthCode($authCode)
229 {
230 $this->authCode = $authCode;
231 }
232
233 public function addTrustedComputer($token, \DateTime $validUntil)
234 {
235 $this->trusted[$token] = $validUntil->format('r');
236 }
237
238 public function isTrustedComputer($token)
239 {
240 if (isset($this->trusted[$token])) {
241 $now = new \DateTime();
242 $validUntil = new \DateTime($this->trusted[$token]);
243
244 return $now < $validUntil;
245 }
246
247 return false;
248 }
249
250 /**
251 * @param Client $client
252 *
253 * @return User
254 */
255 public function addClient(Client $client)
256 {
257 $this->clients[] = $client;
258
259 return $this;
260 }
261
262 /**
263 * @return ArrayCollection<Entry>
264 */
265 public function getClients()
266 {
267 return $this->clients;
268 }
269 }