]>
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; | |
9d50517c NL |
16 | |
17 | /** | |
4346a860 | 18 | * User. |
9d50517c | 19 | * |
1210dae1 | 20 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") |
bd0f3d32 | 21 | * @ORM\Table(name="`user`") |
2f69eb4a | 22 | * @ORM\HasLifecycleCallbacks() |
0f006880 | 23 | * @ExclusionPolicy("all") |
c844dc0c J |
24 | * |
25 | * @UniqueEntity("email") | |
26 | * @UniqueEntity("username") | |
9d50517c | 27 | */ |
2db616b5 | 28 | class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface |
9d50517c NL |
29 | { |
30 | /** | |
4346a860 | 31 | * @var int |
9d50517c | 32 | * |
0f006880 | 33 | * @Expose |
2f69eb4a | 34 | * @ORM\Column(name="id", type="integer") |
9d50517c | 35 | * @ORM\Id |
2f69eb4a | 36 | * @ORM\GeneratedValue(strategy="AUTO") |
9d50517c | 37 | */ |
a1691859 | 38 | protected $id; |
9d50517c NL |
39 | |
40 | /** | |
41 | * @var string | |
42 | * | |
43 | * @ORM\Column(name="name", type="text", nullable=true) | |
44 | */ | |
a1691859 | 45 | protected $name; |
6894d48e | 46 | |
2f69eb4a NL |
47 | /** |
48 | * @var date | |
49 | * | |
50 | * @ORM\Column(name="created_at", type="datetime") | |
51 | */ | |
a1691859 | 52 | protected $createdAt; |
2f69eb4a NL |
53 | |
54 | /** | |
55 | * @var date | |
56 | * | |
57 | * @ORM\Column(name="updated_at", type="datetime") | |
58 | */ | |
a1691859 | 59 | protected $updatedAt; |
2f69eb4a | 60 | |
5f09650e | 61 | /** |
1210dae1 | 62 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"}) |
5f09650e | 63 | */ |
a1691859 | 64 | protected $entries; |
5f09650e | 65 | |
32da2a70 | 66 | /** |
1210dae1 | 67 | * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user") |
32da2a70 | 68 | */ |
a1691859 | 69 | protected $config; |
32da2a70 | 70 | |
2db616b5 NL |
71 | /** |
72 | * @ORM\Column(type="integer", nullable=true) | |
73 | */ | |
74 | private $authCode; | |
75 | ||
76 | /** | |
77 | * @var bool Enabled yes/no | |
78 | * @ORM\Column(type="boolean") | |
79 | */ | |
80 | private $twoFactorAuthentication = false; | |
81 | ||
82 | /** | |
83 | * @ORM\Column(type="json_array", nullable=true) | |
84 | */ | |
85 | private $trusted; | |
86 | ||
c3235553 NL |
87 | public function __construct() |
88 | { | |
a1691859 | 89 | parent::__construct(); |
98f0929f | 90 | $this->entries = new ArrayCollection(); |
fcb1fba5 | 91 | $this->roles = array('ROLE_USER'); |
c3235553 | 92 | } |
9d50517c | 93 | |
2f69eb4a NL |
94 | /** |
95 | * @ORM\PrePersist | |
96 | * @ORM\PreUpdate | |
97 | */ | |
98 | public function timestamps() | |
99 | { | |
100 | if (is_null($this->createdAt)) { | |
101 | $this->createdAt = new \DateTime(); | |
102 | } | |
103 | ||
104 | $this->updatedAt = new \DateTime(); | |
105 | } | |
106 | ||
9d50517c | 107 | /** |
4346a860 JB |
108 | * Set name. |
109 | * | |
110 | * @param string $name | |
9d50517c | 111 | * |
2f69eb4a | 112 | * @return User |
9d50517c NL |
113 | */ |
114 | public function setName($name) | |
115 | { | |
116 | $this->name = $name; | |
117 | ||
118 | return $this; | |
119 | } | |
120 | ||
121 | /** | |
4346a860 | 122 | * Get name. |
9d50517c | 123 | * |
7df80cb3 | 124 | * @return string |
9d50517c NL |
125 | */ |
126 | public function getName() | |
127 | { | |
128 | return $this->name; | |
129 | } | |
130 | ||
2f69eb4a NL |
131 | /** |
132 | * @return string | |
133 | */ | |
134 | public function getCreatedAt() | |
135 | { | |
136 | return $this->createdAt; | |
137 | } | |
138 | ||
139 | /** | |
140 | * @return string | |
141 | */ | |
142 | public function getUpdatedAt() | |
143 | { | |
144 | return $this->updatedAt; | |
145 | } | |
146 | ||
5f09650e NL |
147 | /** |
148 | * @param Entry $entry | |
149 | * | |
150 | * @return User | |
151 | */ | |
152 | public function addEntry(Entry $entry) | |
153 | { | |
154 | $this->entries[] = $entry; | |
155 | ||
156 | return $this; | |
157 | } | |
158 | ||
159 | /** | |
160 | * @return ArrayCollection<Entry> | |
161 | */ | |
162 | public function getEntries() | |
163 | { | |
164 | return $this->entries; | |
165 | } | |
166 | ||
c3235553 NL |
167 | public function isEqualTo(UserInterface $user) |
168 | { | |
169 | return $this->username === $user->getUsername(); | |
170 | } | |
171 | ||
32da2a70 | 172 | /** |
4346a860 JB |
173 | * Set config. |
174 | * | |
1210dae1 | 175 | * @param Config $config |
32da2a70 | 176 | * |
32da2a70 J |
177 | * @return User |
178 | */ | |
1210dae1 | 179 | public function setConfig(Config $config = null) |
32da2a70 J |
180 | { |
181 | $this->config = $config; | |
182 | ||
183 | return $this; | |
184 | } | |
185 | ||
186 | /** | |
4346a860 | 187 | * Get config. |
32da2a70 | 188 | * |
1210dae1 | 189 | * @return Config |
32da2a70 J |
190 | */ |
191 | public function getConfig() | |
192 | { | |
193 | return $this->config; | |
194 | } | |
2db616b5 NL |
195 | |
196 | /** | |
197 | * @return bool | |
198 | */ | |
199 | public function isTwoFactorAuthentication() | |
200 | { | |
201 | return $this->twoFactorAuthentication; | |
202 | } | |
203 | ||
204 | /** | |
205 | * @param bool $twoFactorAuthentication | |
206 | */ | |
207 | public function setTwoFactorAuthentication($twoFactorAuthentication) | |
208 | { | |
209 | $this->twoFactorAuthentication = $twoFactorAuthentication; | |
210 | } | |
211 | ||
212 | public function isEmailAuthEnabled() | |
213 | { | |
214 | return $this->twoFactorAuthentication; | |
215 | } | |
216 | ||
217 | public function getEmailAuthCode() | |
218 | { | |
219 | return $this->authCode; | |
220 | } | |
221 | ||
222 | public function setEmailAuthCode($authCode) | |
223 | { | |
224 | $this->authCode = $authCode; | |
225 | } | |
226 | ||
227 | public function addTrustedComputer($token, \DateTime $validUntil) | |
228 | { | |
229 | $this->trusted[$token] = $validUntil->format('r'); | |
230 | } | |
231 | ||
232 | public function isTrustedComputer($token) | |
233 | { | |
234 | if (isset($this->trusted[$token])) { | |
235 | $now = new \DateTime(); | |
236 | $validUntil = new \DateTime($this->trusted[$token]); | |
237 | ||
238 | return $now < $validUntil; | |
239 | } | |
240 | ||
241 | return false; | |
242 | } | |
9d50517c | 243 | } |