]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Entity/User.php
Added migration for user group
[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 JMS\Serializer\Annotation\XmlRoot;
9 use JMS\Serializer\Annotation\Accessor;
10 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
11 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
12 use FOS\UserBundle\Model\User as BaseUser;
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 * @XmlRoot("user")
23 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
24 * @ORM\Table(name="`user`")
25 * @ORM\HasLifecycleCallbacks()
26 *
27 * @UniqueEntity("email")
28 * @UniqueEntity("username")
29 */
30 class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
31 {
32 /** @Serializer\XmlAttribute */
33 /**
34 * @var int
35 *
36 * @ORM\Column(name="id", type="integer")
37 * @ORM\Id
38 * @ORM\GeneratedValue(strategy="AUTO")
39 *
40 * @Groups({"user_api", "user_api_with_client"})
41 */
42 protected $id;
43
44 /**
45 * @var string
46 *
47 * @ORM\Column(name="name", type="text", nullable=true)
48 *
49 * @Groups({"user_api", "user_api_with_client"})
50 */
51 protected $name;
52
53 /**
54 * @var string
55 *
56 * @Groups({"user_api", "user_api_with_client"})
57 */
58 protected $username;
59
60 /**
61 * @var string
62 *
63 * @Groups({"user_api", "user_api_with_client"})
64 */
65 protected $email;
66
67 /**
68 * @var \DateTime
69 *
70 * @ORM\Column(name="created_at", type="datetime")
71 *
72 * @Groups({"user_api", "user_api_with_client"})
73 */
74 protected $createdAt;
75
76 /**
77 * @var \DateTime
78 *
79 * @ORM\Column(name="updated_at", type="datetime")
80 *
81 * @Groups({"user_api", "user_api_with_client"})
82 */
83 protected $updatedAt;
84
85 /**
86 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
87 */
88 protected $entries;
89
90 /**
91 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
92 */
93 protected $config;
94
95 /**
96 * @ORM\Column(type="integer", nullable=true)
97 */
98 private $authCode;
99
100 /**
101 * @ORM\ManyToMany(targetEntity="Wallabag\UserBundle\Entity\Group")
102 * @ORM\JoinTable(name="user_group",
103 * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
104 * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
105 * )
106 */
107 protected $groups;
108
109 /**
110 * @var bool Enabled yes/no
111 * @ORM\Column(type="boolean")
112 */
113 private $twoFactorAuthentication = false;
114
115 /**
116 * @ORM\Column(type="json_array", nullable=true)
117 */
118 private $trusted;
119
120 /**
121 * @var ArrayCollection
122 *
123 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
124 */
125 protected $clients;
126
127 /**
128 * @see getFirstClient() below
129 *
130 * @Groups({"user_api_with_client"})
131 * @Accessor(getter="getFirstClient")
132 */
133 protected $default_client;
134
135 public function __construct()
136 {
137 parent::__construct();
138 $this->entries = new ArrayCollection();
139 $this->roles = ['ROLE_USER'];
140 }
141
142 /**
143 * @ORM\PrePersist
144 * @ORM\PreUpdate
145 */
146 public function timestamps()
147 {
148 if (is_null($this->createdAt)) {
149 $this->createdAt = new \DateTime();
150 }
151
152 $this->updatedAt = new \DateTime();
153 }
154
155 /**
156 * Set name.
157 *
158 * @param string $name
159 *
160 * @return User
161 */
162 public function setName($name)
163 {
164 $this->name = $name;
165
166 return $this;
167 }
168
169 /**
170 * Get name.
171 *
172 * @return string
173 */
174 public function getName()
175 {
176 return $this->name;
177 }
178
179 /**
180 * @return \DateTime
181 */
182 public function getCreatedAt()
183 {
184 return $this->createdAt;
185 }
186
187 /**
188 * @return \DateTime
189 */
190 public function getUpdatedAt()
191 {
192 return $this->updatedAt;
193 }
194
195 /**
196 * @param Entry $entry
197 *
198 * @return User
199 */
200 public function addEntry(Entry $entry)
201 {
202 $this->entries[] = $entry;
203
204 return $this;
205 }
206
207 /**
208 * @return ArrayCollection<Entry>
209 */
210 public function getEntries()
211 {
212 return $this->entries;
213 }
214
215 public function isEqualTo(UserInterface $user)
216 {
217 return $this->username === $user->getUsername();
218 }
219
220 /**
221 * Set config.
222 *
223 * @param Config $config
224 *
225 * @return User
226 */
227 public function setConfig(Config $config = null)
228 {
229 $this->config = $config;
230
231 return $this;
232 }
233
234 /**
235 * Get config.
236 *
237 * @return Config
238 */
239 public function getConfig()
240 {
241 return $this->config;
242 }
243
244 /**
245 * @return bool
246 */
247 public function isTwoFactorAuthentication()
248 {
249 return $this->twoFactorAuthentication;
250 }
251
252 /**
253 * @param bool $twoFactorAuthentication
254 */
255 public function setTwoFactorAuthentication($twoFactorAuthentication)
256 {
257 $this->twoFactorAuthentication = $twoFactorAuthentication;
258 }
259
260 public function isEmailAuthEnabled()
261 {
262 return $this->twoFactorAuthentication;
263 }
264
265 public function getEmailAuthCode()
266 {
267 return $this->authCode;
268 }
269
270 public function setEmailAuthCode($authCode)
271 {
272 $this->authCode = $authCode;
273 }
274
275 public function addTrustedComputer($token, \DateTime $validUntil)
276 {
277 $this->trusted[$token] = $validUntil->format('r');
278 }
279
280 public function isTrustedComputer($token)
281 {
282 if (isset($this->trusted[$token])) {
283 $now = new \DateTime();
284 $validUntil = new \DateTime($this->trusted[$token]);
285
286 return $now < $validUntil;
287 }
288
289 return false;
290 }
291
292 /**
293 * @param Client $client
294 *
295 * @return User
296 */
297 public function addClient(Client $client)
298 {
299 $this->clients[] = $client;
300
301 return $this;
302 }
303
304 /**
305 * @return ArrayCollection<Entry>
306 */
307 public function getClients()
308 {
309 return $this->clients;
310 }
311
312 /**
313 * Only used by the API when creating a new user it'll also return the first client (which was also created at the same time).
314 *
315 * @return Client
316 */
317 public function getFirstClient()
318 {
319 if (!empty($this->clients)) {
320 return $this->clients->first();
321 }
322 }
323 }