]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
WIP
[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 Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
9 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
10 use FOS\UserBundle\Model\User as BaseUser;
11 use JMS\Serializer\Annotation\ExclusionPolicy;
12 use JMS\Serializer\Annotation\Expose;
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 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
23 * @ORM\Table(name="`user`")
24 * @ORM\HasLifecycleCallbacks()
25 * @ExclusionPolicy("all")
26 *
27 * @UniqueEntity("email")
28 * @UniqueEntity("username")
29 */
30 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
31 {
32 /**
33 * @var int
34 *
35 * @Expose
36 * @ORM\Column(name="id", type="integer")
37 * @ORM\Id
38 * @ORM\GeneratedValue(strategy="AUTO")
39 * @Groups({"user_api"})
40 */
41 protected $id;
42
43 /**
44 * @var string
45 *
46 * @ORM\Column(name="name", type="text", nullable=true)
47 * @Groups({"user_api"})
48 */
49 protected $name;
50
51 /**
52 * @var date
53 *
54 * @ORM\Column(name="created_at", type="datetime")
55 * @Groups({"user_api"})
56 */
57 protected $createdAt;
58
59 /**
60 * @var date
61 *
62 * @ORM\Column(name="updated_at", type="datetime")
63 * @Groups({"user_api"})
64 */
65 protected $updatedAt;
66
67 /**
68 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
69 */
70 protected $entries;
71
72 /**
73 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
74 */
75 protected $config;
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 /**
94 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
95 */
96 protected $clients;
97
98 public function __construct()
99 {
100 parent::__construct();
101 $this->entries = new ArrayCollection();
102 $this->roles = ['ROLE_USER'];
103 }
104
105 /**
106 * @ORM\PrePersist
107 * @ORM\PreUpdate
108 */
109 public function timestamps()
110 {
111 if (is_null($this->createdAt)) {
112 $this->createdAt = new \DateTime();
113 }
114
115 $this->updatedAt = new \DateTime();
116 }
117
118 /**
119 * Set name.
120 *
121 * @param string $name
122 *
123 * @return User
124 */
125 public function setName($name)
126 {
127 $this->name = $name;
128
129 return $this;
130 }
131
132 /**
133 * Get name.
134 *
135 * @return string
136 */
137 public function getName()
138 {
139 return $this->name;
140 }
141
142 /**
143 * @return string
144 */
145 public function getCreatedAt()
146 {
147 return $this->createdAt;
148 }
149
150 /**
151 * @return string
152 */
153 public function getUpdatedAt()
154 {
155 return $this->updatedAt;
156 }
157
158 /**
159 * @param Entry $entry
160 *
161 * @return User
162 */
163 public function addEntry(Entry $entry)
164 {
165 $this->entries[] = $entry;
166
167 return $this;
168 }
169
170 /**
171 * @return ArrayCollection<Entry>
172 */
173 public function getEntries()
174 {
175 return $this->entries;
176 }
177
178 public function isEqualTo(UserInterface $user)
179 {
180 return $this->username === $user->getUsername();
181 }
182
183 /**
184 * Set config.
185 *
186 * @param Config $config
187 *
188 * @return User
189 */
190 public function setConfig(Config $config = null)
191 {
192 $this->config = $config;
193
194 return $this;
195 }
196
197 /**
198 * Get config.
199 *
200 * @return Config
201 */
202 public function getConfig()
203 {
204 return $this->config;
205 }
206
207 /**
208 * @return bool
209 */
210 public function isTwoFactorAuthentication()
211 {
212 return $this->twoFactorAuthentication;
213 }
214
215 /**
216 * @param bool $twoFactorAuthentication
217 */
218 public function setTwoFactorAuthentication($twoFactorAuthentication)
219 {
220 $this->twoFactorAuthentication = $twoFactorAuthentication;
221 }
222
223 public function isEmailAuthEnabled()
224 {
225 return $this->twoFactorAuthentication;
226 }
227
228 public function getEmailAuthCode()
229 {
230 return $this->authCode;
231 }
232
233 public function setEmailAuthCode($authCode)
234 {
235 $this->authCode = $authCode;
236 }
237
238 public function addTrustedComputer($token, \DateTime $validUntil)
239 {
240 $this->trusted[$token] = $validUntil->format('r');
241 }
242
243 public function isTrustedComputer($token)
244 {
245 if (isset($this->trusted[$token])) {
246 $now = new \DateTime();
247 $validUntil = new \DateTime($this->trusted[$token]);
248
249 return $now < $validUntil;
250 }
251
252 return false;
253 }
254
255 /**
256 * @param Client $client
257 *
258 * @return User
259 */
260 public function addClient(Client $client)
261 {
262 $this->clients[] = $client;
263
264 return $this;
265 }
266
267 /**
268 * @return ArrayCollection<Entry>
269 */
270 public function getClients()
271 {
272 return $this->clients;
273 }
274 }