]>
Commit | Line | Data |
---|---|---|
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\CoreBundle\Entity\Config; | |
15 | use Wallabag\CoreBundle\Entity\Entry; | |
16 | ||
17 | /** | |
18 | * User. | |
19 | * | |
20 | * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") | |
21 | * @ORM\Table(name="`user`") | |
22 | * @ORM\HasLifecycleCallbacks() | |
23 | * @ExclusionPolicy("all") | |
24 | * | |
25 | * @UniqueEntity("email") | |
26 | * @UniqueEntity("username") | |
27 | */ | |
28 | class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface | |
29 | { | |
30 | /** | |
31 | * @var int | |
32 | * | |
33 | * @Expose | |
34 | * @ORM\Column(name="id", type="integer") | |
35 | * @ORM\Id | |
36 | * @ORM\GeneratedValue(strategy="AUTO") | |
37 | */ | |
38 | protected $id; | |
39 | ||
40 | /** | |
41 | * @var string | |
42 | * | |
43 | * @ORM\Column(name="name", type="text", nullable=true) | |
44 | */ | |
45 | protected $name; | |
46 | ||
47 | /** | |
48 | * @var date | |
49 | * | |
50 | * @ORM\Column(name="created_at", type="datetime") | |
51 | */ | |
52 | protected $createdAt; | |
53 | ||
54 | /** | |
55 | * @var date | |
56 | * | |
57 | * @ORM\Column(name="updated_at", type="datetime") | |
58 | */ | |
59 | protected $updatedAt; | |
60 | ||
61 | /** | |
62 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"}) | |
63 | */ | |
64 | protected $entries; | |
65 | ||
66 | /** | |
67 | * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user") | |
68 | */ | |
69 | protected $config; | |
70 | ||
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 | ||
87 | public function __construct() | |
88 | { | |
89 | parent::__construct(); | |
90 | $this->entries = new ArrayCollection(); | |
91 | $this->roles = array('ROLE_USER'); | |
92 | } | |
93 | ||
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 | ||
107 | /** | |
108 | * Set name. | |
109 | * | |
110 | * @param string $name | |
111 | * | |
112 | * @return User | |
113 | */ | |
114 | public function setName($name) | |
115 | { | |
116 | $this->name = $name; | |
117 | ||
118 | return $this; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Get name. | |
123 | * | |
124 | * @return string | |
125 | */ | |
126 | public function getName() | |
127 | { | |
128 | return $this->name; | |
129 | } | |
130 | ||
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 | ||
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 | ||
167 | public function isEqualTo(UserInterface $user) | |
168 | { | |
169 | return $this->username === $user->getUsername(); | |
170 | } | |
171 | ||
172 | /** | |
173 | * Set config. | |
174 | * | |
175 | * @param Config $config | |
176 | * | |
177 | * @return User | |
178 | */ | |
179 | public function setConfig(Config $config = null) | |
180 | { | |
181 | $this->config = $config; | |
182 | ||
183 | return $this; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Get config. | |
188 | * | |
189 | * @return Config | |
190 | */ | |
191 | public function getConfig() | |
192 | { | |
193 | return $this->config; | |
194 | } | |
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 | } | |
243 | } |