]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
1210dae1 | 3 | namespace Wallabag\UserBundle\Entity; |
9d50517c | 4 | |
5f09650e | 5 | use Doctrine\Common\Collections\ArrayCollection; |
9d50517c | 6 | use Doctrine\ORM\Mapping as ORM; |
2db616b5 NL |
7 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; |
8 | use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; | |
c844dc0c | 9 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
c3235553 | 10 | use Symfony\Component\Security\Core\User\UserInterface; |
0f006880 NL |
11 | use JMS\Serializer\Annotation\ExclusionPolicy; |
12 | use JMS\Serializer\Annotation\Expose; | |
a1691859 | 13 | use FOS\UserBundle\Model\User as BaseUser; |
1210dae1 NL |
14 | use Wallabag\CoreBundle\Entity\Config; |
15 | use Wallabag\CoreBundle\Entity\Entry; | |
16 | use Wallabag\CoreBundle\Entity\Tag; | |
9d50517c NL |
17 | |
18 | /** | |
4346a860 | 19 | * User. |
9d50517c | 20 | * |
1210dae1 | 21 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") |
bd0f3d32 | 22 | * @ORM\Table(name="`user`") |
2f69eb4a | 23 | * @ORM\HasLifecycleCallbacks() |
0f006880 | 24 | * @ExclusionPolicy("all") |
c844dc0c J |
25 | * |
26 | * @UniqueEntity("email") | |
27 | * @UniqueEntity("username") | |
9d50517c | 28 | */ |
2db616b5 | 29 | class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface |
9d50517c NL |
30 | { |
31 | /** | |
4346a860 | 32 | * @var int |
9d50517c | 33 | * |
0f006880 | 34 | * @Expose |
2f69eb4a | 35 | * @ORM\Column(name="id", type="integer") |
9d50517c | 36 | * @ORM\Id |
2f69eb4a | 37 | * @ORM\GeneratedValue(strategy="AUTO") |
9d50517c | 38 | */ |
a1691859 | 39 | protected $id; |
9d50517c NL |
40 | |
41 | /** | |
42 | * @var string | |
43 | * | |
44 | * @ORM\Column(name="name", type="text", nullable=true) | |
45 | */ | |
a1691859 | 46 | protected $name; |
6894d48e | 47 | |
2f69eb4a NL |
48 | /** |
49 | * @var date | |
50 | * | |
51 | * @ORM\Column(name="created_at", type="datetime") | |
52 | */ | |
a1691859 | 53 | protected $createdAt; |
2f69eb4a NL |
54 | |
55 | /** | |
56 | * @var date | |
57 | * | |
58 | * @ORM\Column(name="updated_at", type="datetime") | |
59 | */ | |
a1691859 | 60 | protected $updatedAt; |
2f69eb4a | 61 | |
5f09650e | 62 | /** |
1210dae1 | 63 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"}) |
5f09650e | 64 | */ |
a1691859 | 65 | protected $entries; |
5f09650e | 66 | |
32da2a70 | 67 | /** |
1210dae1 | 68 | * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user") |
32da2a70 | 69 | */ |
a1691859 | 70 | protected $config; |
32da2a70 | 71 | |
092ca707 | 72 | /** |
1210dae1 | 73 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Tag", mappedBy="user", cascade={"remove"}) |
092ca707 | 74 | */ |
a1691859 | 75 | protected $tags; |
092ca707 | 76 | |
2db616b5 NL |
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 | ||
c3235553 NL |
93 | public function __construct() |
94 | { | |
a1691859 | 95 | parent::__construct(); |
98f0929f JB |
96 | $this->entries = new ArrayCollection(); |
97 | $this->tags = 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 | ||
092ca707 NL |
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 | } | |
c3235553 NL |
193 | |
194 | public function isEqualTo(UserInterface $user) | |
195 | { | |
196 | return $this->username === $user->getUsername(); | |
197 | } | |
198 | ||
32da2a70 | 199 | /** |
4346a860 JB |
200 | * Set config. |
201 | * | |
1210dae1 | 202 | * @param Config $config |
32da2a70 | 203 | * |
32da2a70 J |
204 | * @return User |
205 | */ | |
1210dae1 | 206 | public function setConfig(Config $config = null) |
32da2a70 J |
207 | { |
208 | $this->config = $config; | |
209 | ||
210 | return $this; | |
211 | } | |
212 | ||
213 | /** | |
4346a860 | 214 | * Get config. |
32da2a70 | 215 | * |
1210dae1 | 216 | * @return Config |
32da2a70 J |
217 | */ |
218 | public function getConfig() | |
219 | { | |
220 | return $this->config; | |
221 | } | |
2db616b5 NL |
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 | } | |
9d50517c | 270 | } |