]>
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; |
22510459 | 7 | use JMS\Serializer\Annotation\Groups; |
2db616b5 NL |
8 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; |
9 | use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; | |
619cc453 | 10 | use FOS\UserBundle\Model\User as BaseUser; |
0f006880 NL |
11 | use JMS\Serializer\Annotation\ExclusionPolicy; |
12 | use JMS\Serializer\Annotation\Expose; | |
619cc453 JB |
13 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
14 | use Symfony\Component\Security\Core\User\UserInterface; | |
23406ca3 | 15 | use Wallabag\ApiBundle\Entity\Client; |
1210dae1 NL |
16 | use Wallabag\CoreBundle\Entity\Config; |
17 | use Wallabag\CoreBundle\Entity\Entry; | |
9d50517c NL |
18 | |
19 | /** | |
4346a860 | 20 | * User. |
9d50517c | 21 | * |
1210dae1 | 22 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") |
bd0f3d32 | 23 | * @ORM\Table(name="`user`") |
2f69eb4a | 24 | * @ORM\HasLifecycleCallbacks() |
0f006880 | 25 | * @ExclusionPolicy("all") |
c844dc0c J |
26 | * |
27 | * @UniqueEntity("email") | |
28 | * @UniqueEntity("username") | |
9d50517c | 29 | */ |
2db616b5 | 30 | class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface |
9d50517c NL |
31 | { |
32 | /** | |
4346a860 | 33 | * @var int |
9d50517c | 34 | * |
0f006880 | 35 | * @Expose |
2f69eb4a | 36 | * @ORM\Column(name="id", type="integer") |
9d50517c | 37 | * @ORM\Id |
2f69eb4a | 38 | * @ORM\GeneratedValue(strategy="AUTO") |
22510459 | 39 | * @Groups({"user_api"}) |
9d50517c | 40 | */ |
a1691859 | 41 | protected $id; |
9d50517c NL |
42 | |
43 | /** | |
44 | * @var string | |
45 | * | |
46 | * @ORM\Column(name="name", type="text", nullable=true) | |
22510459 | 47 | * @Groups({"user_api"}) |
9d50517c | 48 | */ |
a1691859 | 49 | protected $name; |
6894d48e | 50 | |
2f69eb4a NL |
51 | /** |
52 | * @var date | |
53 | * | |
54 | * @ORM\Column(name="created_at", type="datetime") | |
22510459 | 55 | * @Groups({"user_api"}) |
2f69eb4a | 56 | */ |
a1691859 | 57 | protected $createdAt; |
2f69eb4a NL |
58 | |
59 | /** | |
60 | * @var date | |
61 | * | |
62 | * @ORM\Column(name="updated_at", type="datetime") | |
22510459 | 63 | * @Groups({"user_api"}) |
2f69eb4a | 64 | */ |
a1691859 | 65 | protected $updatedAt; |
2f69eb4a | 66 | |
5f09650e | 67 | /** |
1210dae1 | 68 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"}) |
5f09650e | 69 | */ |
a1691859 | 70 | protected $entries; |
5f09650e | 71 | |
32da2a70 | 72 | /** |
152fcccd | 73 | * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"}) |
32da2a70 | 74 | */ |
a1691859 | 75 | protected $config; |
32da2a70 | 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 | ||
23406ca3 NL |
93 | /** |
94 | * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"}) | |
95 | */ | |
96 | protected $clients; | |
97 | ||
c3235553 NL |
98 | public function __construct() |
99 | { | |
a1691859 | 100 | parent::__construct(); |
98f0929f | 101 | $this->entries = new ArrayCollection(); |
4094ea47 | 102 | $this->roles = ['ROLE_USER']; |
c3235553 | 103 | } |
9d50517c | 104 | |
2f69eb4a NL |
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 | ||
9d50517c | 118 | /** |
4346a860 JB |
119 | * Set name. |
120 | * | |
121 | * @param string $name | |
9d50517c | 122 | * |
2f69eb4a | 123 | * @return User |
9d50517c NL |
124 | */ |
125 | public function setName($name) | |
126 | { | |
127 | $this->name = $name; | |
128 | ||
129 | return $this; | |
130 | } | |
131 | ||
132 | /** | |
4346a860 | 133 | * Get name. |
9d50517c | 134 | * |
7df80cb3 | 135 | * @return string |
9d50517c NL |
136 | */ |
137 | public function getName() | |
138 | { | |
139 | return $this->name; | |
140 | } | |
141 | ||
2f69eb4a NL |
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 | ||
5f09650e NL |
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 | ||
c3235553 NL |
178 | public function isEqualTo(UserInterface $user) |
179 | { | |
180 | return $this->username === $user->getUsername(); | |
181 | } | |
182 | ||
32da2a70 | 183 | /** |
4346a860 JB |
184 | * Set config. |
185 | * | |
1210dae1 | 186 | * @param Config $config |
32da2a70 | 187 | * |
32da2a70 J |
188 | * @return User |
189 | */ | |
1210dae1 | 190 | public function setConfig(Config $config = null) |
32da2a70 J |
191 | { |
192 | $this->config = $config; | |
193 | ||
194 | return $this; | |
195 | } | |
196 | ||
197 | /** | |
4346a860 | 198 | * Get config. |
32da2a70 | 199 | * |
1210dae1 | 200 | * @return Config |
32da2a70 J |
201 | */ |
202 | public function getConfig() | |
203 | { | |
204 | return $this->config; | |
205 | } | |
2db616b5 NL |
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 | } | |
23406ca3 NL |
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 | } | |
9d50517c | 274 | } |