]>
Commit | Line | Data |
---|---|---|
4d85d7e9 J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
34be2d5d JB |
5 | use JMS\Serializer\SerializationContext; |
6 | use JMS\Serializer\SerializerBuilder; | |
dfd0a7bc | 7 | use PragmaRX\Recovery\Recovery as BackupCodes; |
4d85d7e9 | 8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
0c83fd59 | 9 | use Symfony\Component\HttpFoundation\JsonResponse; |
98568055 | 10 | use Symfony\Component\HttpFoundation\RedirectResponse; |
619cc453 | 11 | use Symfony\Component\HttpFoundation\Request; |
34be2d5d | 12 | use Symfony\Component\HttpFoundation\Response; |
bb0c78f4 | 13 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
115de64e | 14 | use Symfony\Component\Routing\Annotation\Route; |
4d4147b2 | 15 | use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint; |
4d85d7e9 | 16 | use Wallabag\CoreBundle\Entity\Config; |
f19f9f62 | 17 | use Wallabag\CoreBundle\Entity\TaggingRule; |
d9085c63 | 18 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
f808b016 | 19 | use Wallabag\CoreBundle\Form\Type\ConfigType; |
531c8d0a | 20 | use Wallabag\CoreBundle\Form\Type\FeedType; |
34be2d5d | 21 | use Wallabag\CoreBundle\Form\Type\TaggingRuleImportType; |
619cc453 JB |
22 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; |
23 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | |
0c83fd59 | 24 | use Wallabag\CoreBundle\Tools\Utils; |
4d85d7e9 J |
25 | |
26 | class ConfigController extends Controller | |
27 | { | |
28 | /** | |
4d85d7e9 | 29 | * @Route("/config", name="config") |
4d85d7e9 J |
30 | */ |
31 | public function indexAction(Request $request) | |
32 | { | |
d9085c63 | 33 | $em = $this->getDoctrine()->getManager(); |
4d85d7e9 | 34 | $config = $this->getConfig(); |
fcb1fba5 | 35 | $userManager = $this->container->get('fos_user.user_manager'); |
c0d9eba0 | 36 | $user = $this->getUser(); |
4d85d7e9 | 37 | |
32da2a70 | 38 | // handle basic config detail (this form is defined as a service) |
4094ea47 | 39 | $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]); |
d9085c63 | 40 | $configForm->handleRequest($request); |
4d85d7e9 | 41 | |
21e7ccef | 42 | if ($configForm->isSubmitted() && $configForm->isValid()) { |
4d85d7e9 J |
43 | $em->persist($config); |
44 | $em->flush(); | |
45 | ||
ece4718f NL |
46 | $request->getSession()->set('_locale', $config->getLanguage()); |
47 | ||
32da2a70 J |
48 | // switch active theme |
49 | $activeTheme = $this->get('liip_theme.active_theme'); | |
50 | $activeTheme->setName($config->getTheme()); | |
51 | ||
a6b242a1 | 52 | $this->addFlash( |
4d85d7e9 | 53 | 'notice', |
4204a06b | 54 | 'flashes.config.notice.config_saved' |
4d85d7e9 J |
55 | ); |
56 | ||
57 | return $this->redirect($this->generateUrl('config')); | |
58 | } | |
59 | ||
d9085c63 | 60 | // handle changing password |
f808b016 | 61 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config') . '#set4']); |
d9085c63 J |
62 | $pwdForm->handleRequest($request); |
63 | ||
21e7ccef | 64 | if ($pwdForm->isSubmitted() && $pwdForm->isValid()) { |
a4f42c59 | 65 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { |
4204a06b | 66 | $message = 'flashes.config.notice.password_not_updated_demo'; |
6c9f50a6 | 67 | } else { |
4204a06b | 68 | $message = 'flashes.config.notice.password_updated'; |
b6c00b0b | 69 | |
d8d56448 NL |
70 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); |
71 | $userManager->updateUser($user, true); | |
6c9f50a6 | 72 | } |
d9085c63 | 73 | |
a6b242a1 | 74 | $this->addFlash('notice', $message); |
b6c00b0b | 75 | |
f808b016 | 76 | return $this->redirect($this->generateUrl('config') . '#set4'); |
d9085c63 J |
77 | } |
78 | ||
c0d9eba0 | 79 | // handle changing user information |
4094ea47 JB |
80 | $userForm = $this->createForm(UserInformationType::class, $user, [ |
81 | 'validation_groups' => ['Profile'], | |
f808b016 | 82 | 'action' => $this->generateUrl('config') . '#set3', |
4094ea47 | 83 | ]); |
c0d9eba0 J |
84 | $userForm->handleRequest($request); |
85 | ||
21e7ccef | 86 | if ($userForm->isSubmitted() && $userForm->isValid()) { |
fcb1fba5 | 87 | $userManager->updateUser($user, true); |
c0d9eba0 | 88 | |
a6b242a1 | 89 | $this->addFlash( |
c0d9eba0 | 90 | 'notice', |
4204a06b | 91 | 'flashes.config.notice.user_updated' |
c0d9eba0 J |
92 | ); |
93 | ||
f808b016 | 94 | return $this->redirect($this->generateUrl('config') . '#set3'); |
c0d9eba0 J |
95 | } |
96 | ||
531c8d0a TC |
97 | // handle feed information |
98 | $feedForm = $this->createForm(FeedType::class, $config, ['action' => $this->generateUrl('config') . '#set2']); | |
99 | $feedForm->handleRequest($request); | |
0c83fd59 | 100 | |
531c8d0a | 101 | if ($feedForm->isSubmitted() && $feedForm->isValid()) { |
0c83fd59 J |
102 | $em->persist($config); |
103 | $em->flush(); | |
104 | ||
a6b242a1 | 105 | $this->addFlash( |
0c83fd59 | 106 | 'notice', |
531c8d0a | 107 | 'flashes.config.notice.feed_updated' |
0c83fd59 J |
108 | ); |
109 | ||
f808b016 | 110 | return $this->redirect($this->generateUrl('config') . '#set2'); |
0c83fd59 J |
111 | } |
112 | ||
f19f9f62 KG |
113 | // handle tagging rule |
114 | $taggingRule = new TaggingRule(); | |
f808b016 | 115 | $action = $this->generateUrl('config') . '#set5'; |
bf3dc999 JB |
116 | |
117 | if ($request->query->has('tagging-rule')) { | |
118 | $taggingRule = $this->getDoctrine() | |
119 | ->getRepository('WallabagCoreBundle:TaggingRule') | |
120 | ->find($request->query->get('tagging-rule')); | |
121 | ||
122 | if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) { | |
123 | return $this->redirect($action); | |
124 | } | |
125 | ||
f808b016 | 126 | $action = $this->generateUrl('config') . '?tagging-rule=' . $taggingRule->getId() . '#set5'; |
bf3dc999 JB |
127 | } |
128 | ||
129 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); | |
f19f9f62 KG |
130 | $newTaggingRule->handleRequest($request); |
131 | ||
21e7ccef | 132 | if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) { |
f19f9f62 KG |
133 | $taggingRule->setConfig($config); |
134 | $em->persist($taggingRule); | |
135 | $em->flush(); | |
136 | ||
a6b242a1 | 137 | $this->addFlash( |
f19f9f62 | 138 | 'notice', |
4204a06b | 139 | 'flashes.config.notice.tagging_rules_updated' |
f19f9f62 KG |
140 | ); |
141 | ||
f808b016 | 142 | return $this->redirect($this->generateUrl('config') . '#set5'); |
f19f9f62 KG |
143 | } |
144 | ||
34be2d5d JB |
145 | // handle tagging rules import |
146 | $taggingRulesImportform = $this->createForm(TaggingRuleImportType::class); | |
147 | $taggingRulesImportform->handleRequest($request); | |
148 | ||
149 | if ($taggingRulesImportform->isSubmitted() && $taggingRulesImportform->isValid()) { | |
150 | $message = 'flashes.config.notice.tagging_rules_not_imported'; | |
151 | $file = $taggingRulesImportform->get('file')->getData(); | |
152 | ||
153 | if (null !== $file && $file->isValid() && \in_array($file->getClientMimeType(), ['application/json', 'application/octet-stream'], true)) { | |
154 | $content = json_decode(file_get_contents($file->getPathname()), true); | |
155 | ||
156 | if (\is_array($content)) { | |
157 | foreach ($content as $rule) { | |
158 | $taggingRule = new TaggingRule(); | |
159 | $taggingRule->setRule($rule['rule']); | |
160 | $taggingRule->setTags($rule['tags']); | |
161 | $taggingRule->setConfig($config); | |
162 | $em->persist($taggingRule); | |
163 | } | |
164 | ||
165 | $em->flush(); | |
166 | ||
167 | $message = 'flashes.config.notice.tagging_rules_imported'; | |
168 | } | |
169 | } | |
170 | ||
171 | $this->addFlash('notice', $message); | |
172 | ||
173 | return $this->redirect($this->generateUrl('config') . '#set5'); | |
174 | } | |
175 | ||
4094ea47 JB |
176 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ |
177 | 'form' => [ | |
0c83fd59 | 178 | 'config' => $configForm->createView(), |
531c8d0a | 179 | 'feed' => $feedForm->createView(), |
0c83fd59 J |
180 | 'pwd' => $pwdForm->createView(), |
181 | 'user' => $userForm->createView(), | |
f19f9f62 | 182 | 'new_tagging_rule' => $newTaggingRule->createView(), |
34be2d5d | 183 | 'import_tagging_rule' => $taggingRulesImportform->createView(), |
4094ea47 | 184 | ], |
531c8d0a | 185 | 'feed' => [ |
0c83fd59 | 186 | 'username' => $user->getUsername(), |
531c8d0a | 187 | 'token' => $config->getFeedToken(), |
4094ea47 | 188 | ], |
63e40f2d | 189 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), |
be9d693e | 190 | 'wallabag_url' => $this->getParameter('domain_name'), |
a0c5eb00 | 191 | 'enabled_users' => $this->get('wallabag_user.user_repository')->getSumEnabledUsers(), |
4094ea47 | 192 | ]); |
4d85d7e9 J |
193 | } |
194 | ||
4ff1efa4 NL |
195 | /** |
196 | * Disable 2FA using email. | |
197 | * | |
198 | * @Route("/config/otp/email/disable", name="disable_otp_email") | |
199 | */ | |
200 | public function disableOtpEmailAction() | |
201 | { | |
202 | if (!$this->getParameter('twofactor_auth')) { | |
203 | return $this->createNotFoundException('two_factor not enabled'); | |
204 | } | |
205 | ||
206 | $user = $this->getUser(); | |
207 | $user->setEmailTwoFactor(false); | |
208 | ||
209 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | |
210 | ||
211 | $this->addFlash( | |
212 | 'notice', | |
213 | 'flashes.config.notice.otp_disabled' | |
214 | ); | |
215 | ||
216 | return $this->redirect($this->generateUrl('config') . '#set3'); | |
217 | } | |
218 | ||
a0c5eb00 JB |
219 | /** |
220 | * Enable 2FA using email. | |
221 | * | |
a0c5eb00 JB |
222 | * @Route("/config/otp/email", name="config_otp_email") |
223 | */ | |
c416ed48 | 224 | public function otpEmailAction() |
a0c5eb00 JB |
225 | { |
226 | if (!$this->getParameter('twofactor_auth')) { | |
227 | return $this->createNotFoundException('two_factor not enabled'); | |
228 | } | |
229 | ||
230 | $user = $this->getUser(); | |
231 | ||
232 | $user->setGoogleAuthenticatorSecret(null); | |
233 | $user->setBackupCodes(null); | |
234 | $user->setEmailTwoFactor(true); | |
235 | ||
236 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | |
237 | ||
238 | $this->addFlash( | |
239 | 'notice', | |
240 | 'flashes.config.notice.otp_enabled' | |
241 | ); | |
242 | ||
243 | return $this->redirect($this->generateUrl('config') . '#set3'); | |
244 | } | |
245 | ||
4ff1efa4 NL |
246 | /** |
247 | * Disable 2FA using OTP app. | |
248 | * | |
249 | * @Route("/config/otp/app/disable", name="disable_otp_app") | |
250 | */ | |
251 | public function disableOtpAppAction() | |
252 | { | |
253 | if (!$this->getParameter('twofactor_auth')) { | |
254 | return $this->createNotFoundException('two_factor not enabled'); | |
255 | } | |
256 | ||
257 | $user = $this->getUser(); | |
258 | ||
259 | $user->setGoogleAuthenticatorSecret(''); | |
260 | $user->setBackupCodes(null); | |
261 | ||
262 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | |
263 | ||
264 | $this->addFlash( | |
265 | 'notice', | |
266 | 'flashes.config.notice.otp_disabled' | |
267 | ); | |
268 | ||
269 | return $this->redirect($this->generateUrl('config') . '#set3'); | |
270 | } | |
271 | ||
a0c5eb00 JB |
272 | /** |
273 | * Enable 2FA using OTP app, user will need to confirm the generated code from the app. | |
274 | * | |
275 | * @Route("/config/otp/app", name="config_otp_app") | |
276 | */ | |
277 | public function otpAppAction() | |
278 | { | |
279 | if (!$this->getParameter('twofactor_auth')) { | |
280 | return $this->createNotFoundException('two_factor not enabled'); | |
281 | } | |
282 | ||
283 | $user = $this->getUser(); | |
4654a83b | 284 | $secret = $this->get('scheb_two_factor.security.google_authenticator')->generateSecret(); |
a0c5eb00 | 285 | |
4654a83b JB |
286 | $user->setGoogleAuthenticatorSecret($secret); |
287 | $user->setEmailTwoFactor(false); | |
a0c5eb00 | 288 | |
4654a83b JB |
289 | $backupCodes = (new BackupCodes())->toArray(); |
290 | $backupCodesHashed = array_map( | |
291 | function ($backupCode) { | |
292 | return password_hash($backupCode, PASSWORD_DEFAULT); | |
293 | }, | |
294 | $backupCodes | |
295 | ); | |
a0c5eb00 | 296 | |
4654a83b JB |
297 | $user->setBackupCodes($backupCodesHashed); |
298 | ||
299 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | |
a0c5eb00 | 300 | |
4ff1efa4 NL |
301 | $this->addFlash( |
302 | 'notice', | |
303 | 'flashes.config.notice.otp_enabled' | |
304 | ); | |
305 | ||
a0c5eb00 | 306 | return $this->render('WallabagCoreBundle:Config:otp_app.html.twig', [ |
4654a83b | 307 | 'backupCodes' => $backupCodes, |
a0c5eb00 JB |
308 | 'qr_code' => $this->get('scheb_two_factor.security.google_authenticator')->getQRContent($user), |
309 | ]); | |
310 | } | |
311 | ||
312 | /** | |
313 | * Cancelling 2FA using OTP app. | |
314 | * | |
315 | * @Route("/config/otp/app/cancel", name="config_otp_app_cancel") | |
316 | */ | |
317 | public function otpAppCancelAction() | |
318 | { | |
319 | if (!$this->getParameter('twofactor_auth')) { | |
320 | return $this->createNotFoundException('two_factor not enabled'); | |
321 | } | |
322 | ||
323 | $user = $this->getUser(); | |
324 | $user->setGoogleAuthenticatorSecret(null); | |
325 | $user->setBackupCodes(null); | |
326 | ||
327 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | |
328 | ||
329 | return $this->redirect($this->generateUrl('config') . '#set3'); | |
330 | } | |
331 | ||
332 | /** | |
333 | * Validate OTP code. | |
334 | * | |
a0c5eb00 JB |
335 | * @Route("/config/otp/app/check", name="config_otp_app_check") |
336 | */ | |
337 | public function otpAppCheckAction(Request $request) | |
338 | { | |
339 | $isValid = $this->get('scheb_two_factor.security.google_authenticator')->checkCode( | |
340 | $this->getUser(), | |
341 | $request->get('_auth_code') | |
342 | ); | |
343 | ||
344 | if (true === $isValid) { | |
345 | $this->addFlash( | |
346 | 'notice', | |
347 | 'flashes.config.notice.otp_enabled' | |
348 | ); | |
349 | ||
350 | return $this->redirect($this->generateUrl('config') . '#set3'); | |
351 | } | |
352 | ||
353 | $this->addFlash( | |
354 | 'two_factor', | |
355 | 'scheb_two_factor.code_invalid' | |
356 | ); | |
357 | ||
358 | return $this->redirect($this->generateUrl('config_otp_app')); | |
359 | } | |
360 | ||
0c83fd59 | 361 | /** |
0c83fd59 J |
362 | * @Route("/generate-token", name="generate_token") |
363 | * | |
98568055 | 364 | * @return RedirectResponse|JsonResponse |
0c83fd59 J |
365 | */ |
366 | public function generateTokenAction(Request $request) | |
367 | { | |
368 | $config = $this->getConfig(); | |
531c8d0a | 369 | $config->setFeedToken(Utils::generateToken()); |
0c83fd59 J |
370 | |
371 | $em = $this->getDoctrine()->getManager(); | |
372 | $em->persist($config); | |
373 | $em->flush(); | |
374 | ||
375 | if ($request->isXmlHttpRequest()) { | |
531c8d0a | 376 | return new JsonResponse(['token' => $config->getFeedToken()]); |
0c83fd59 J |
377 | } |
378 | ||
a6b242a1 | 379 | $this->addFlash( |
c7a4f74f | 380 | 'notice', |
531c8d0a | 381 | 'flashes.config.notice.feed_token_updated' |
c7a4f74f JB |
382 | ); |
383 | ||
f808b016 | 384 | return $this->redirect($this->generateUrl('config') . '#set2'); |
0c83fd59 J |
385 | } |
386 | ||
c4bf12aa | 387 | /** |
c4bf12aa JB |
388 | * @Route("/revoke-token", name="revoke_token") |
389 | * | |
390 | * @return RedirectResponse|JsonResponse | |
391 | */ | |
392 | public function revokeTokenAction(Request $request) | |
393 | { | |
394 | $config = $this->getConfig(); | |
395 | $config->setFeedToken(null); | |
396 | ||
397 | $em = $this->getDoctrine()->getManager(); | |
398 | $em->persist($config); | |
399 | $em->flush(); | |
400 | ||
401 | if ($request->isXmlHttpRequest()) { | |
402 | return new JsonResponse(); | |
403 | } | |
404 | ||
405 | $this->addFlash( | |
406 | 'notice', | |
407 | 'flashes.config.notice.feed_token_revoked' | |
408 | ); | |
409 | ||
410 | return $this->redirect($this->generateUrl('config') . '#set2'); | |
411 | } | |
412 | ||
52e423f3 KG |
413 | /** |
414 | * Deletes a tagging rule and redirect to the config homepage. | |
415 | * | |
52e423f3 KG |
416 | * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule") |
417 | * | |
98568055 | 418 | * @return RedirectResponse |
52e423f3 | 419 | */ |
5f8a7857 | 420 | public function deleteTaggingRuleAction(TaggingRule $rule) |
52e423f3 | 421 | { |
8799bde0 | 422 | $this->validateRuleAction($rule); |
52e423f3 KG |
423 | |
424 | $em = $this->getDoctrine()->getManager(); | |
425 | $em->remove($rule); | |
426 | $em->flush(); | |
427 | ||
a6b242a1 | 428 | $this->addFlash( |
52e423f3 | 429 | 'notice', |
4204a06b | 430 | 'flashes.config.notice.tagging_rules_deleted' |
52e423f3 KG |
431 | ); |
432 | ||
f808b016 | 433 | return $this->redirect($this->generateUrl('config') . '#set5'); |
52e423f3 KG |
434 | } |
435 | ||
bf3dc999 JB |
436 | /** |
437 | * Edit a tagging rule. | |
438 | * | |
bf3dc999 JB |
439 | * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule") |
440 | * | |
441 | * @return RedirectResponse | |
442 | */ | |
443 | public function editTaggingRuleAction(TaggingRule $rule) | |
8799bde0 JB |
444 | { |
445 | $this->validateRuleAction($rule); | |
446 | ||
f808b016 | 447 | return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5'); |
8799bde0 JB |
448 | } |
449 | ||
206bade5 JB |
450 | /** |
451 | * Remove all annotations OR tags OR entries for the current user. | |
452 | * | |
453 | * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset") | |
454 | * | |
455 | * @return RedirectResponse | |
456 | */ | |
457 | public function resetAction($type) | |
458 | { | |
206bade5 JB |
459 | switch ($type) { |
460 | case 'annotations': | |
191564b7 JB |
461 | $this->getDoctrine() |
462 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
463 | ->removeAllByUserId($this->getUser()->getId()); | |
206bade5 | 464 | break; |
206bade5 | 465 | case 'tags': |
191564b7 JB |
466 | $this->removeAllTagsByUserId($this->getUser()->getId()); |
467 | break; | |
191564b7 | 468 | case 'entries': |
6da1aebc | 469 | // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff |
191564b7 | 470 | // otherwise they won't be removed ... |
7ab5eb95 | 471 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { |
191564b7 | 472 | $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); |
206bade5 JB |
473 | } |
474 | ||
8c61fd12 | 475 | // manually remove tags to avoid orphan tag |
f71e55ac JB |
476 | $this->removeAllTagsByUserId($this->getUser()->getId()); |
477 | ||
25203e50 | 478 | $this->get('wallabag_core.entry_repository')->removeAllByUserId($this->getUser()->getId()); |
6da1aebc TC |
479 | break; |
480 | case 'archived': | |
7ab5eb95 | 481 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { |
6da1aebc TC |
482 | $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId()); |
483 | } | |
484 | ||
485 | // manually remove tags to avoid orphan tag | |
486 | $this->removeTagsForArchivedByUserId($this->getUser()->getId()); | |
487 | ||
25203e50 | 488 | $this->get('wallabag_core.entry_repository')->removeArchivedByUserId($this->getUser()->getId()); |
6da1aebc | 489 | break; |
206bade5 JB |
490 | } |
491 | ||
a6b242a1 | 492 | $this->addFlash( |
206bade5 | 493 | 'notice', |
f808b016 | 494 | 'flashes.config.notice.' . $type . '_reset' |
206bade5 JB |
495 | ); |
496 | ||
f808b016 JB |
497 | return $this->redirect($this->generateUrl('config') . '#set3'); |
498 | } | |
499 | ||
500 | /** | |
501 | * Delete account for current user. | |
502 | * | |
503 | * @Route("/account/delete", name="delete_account") | |
504 | * | |
f808b016 JB |
505 | * @throws AccessDeniedHttpException |
506 | * | |
507 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
508 | */ | |
509 | public function deleteAccountAction(Request $request) | |
510 | { | |
511 | $enabledUsers = $this->get('wallabag_user.user_repository') | |
512 | ->getSumEnabledUsers(); | |
513 | ||
514 | if ($enabledUsers <= 1) { | |
515 | throw new AccessDeniedHttpException(); | |
516 | } | |
517 | ||
518 | $user = $this->getUser(); | |
519 | ||
520 | // logout current user | |
521 | $this->get('security.token_storage')->setToken(null); | |
522 | $request->getSession()->invalidate(); | |
523 | ||
524 | $em = $this->get('fos_user.user_manager'); | |
525 | $em->deleteUser($user); | |
526 | ||
527 | return $this->redirect($this->generateUrl('fos_user_security_login')); | |
528 | } | |
529 | ||
530 | /** | |
531 | * Switch view mode for current user. | |
532 | * | |
533 | * @Route("/config/view-mode", name="switch_view_mode") | |
534 | * | |
f808b016 JB |
535 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
536 | */ | |
537 | public function changeViewModeAction(Request $request) | |
538 | { | |
539 | $user = $this->getUser(); | |
540 | $user->getConfig()->setListMode(!$user->getConfig()->getListMode()); | |
541 | ||
542 | $em = $this->getDoctrine()->getManager(); | |
543 | $em->persist($user); | |
544 | $em->flush(); | |
545 | ||
546 | return $this->redirect($request->headers->get('referer')); | |
206bade5 JB |
547 | } |
548 | ||
be417ef2 NL |
549 | /** |
550 | * Change the locale for the current user. | |
551 | * | |
8d4ed0df | 552 | * @param string $language |
be417ef2 NL |
553 | * |
554 | * @Route("/locale/{language}", name="changeLocale") | |
555 | * | |
556 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
557 | */ | |
558 | public function setLocaleAction(Request $request, $language = null) | |
559 | { | |
4d4147b2 JB |
560 | $errors = $this->get('validator')->validate($language, (new LocaleConstraint())); |
561 | ||
562 | if (0 === \count($errors)) { | |
563 | $request->getSession()->set('_locale', $language); | |
be417ef2 NL |
564 | } |
565 | ||
4d4147b2 | 566 | return $this->redirect($request->headers->get('referer', $this->generateUrl('homepage'))); |
be417ef2 NL |
567 | } |
568 | ||
34be2d5d JB |
569 | /** |
570 | * Export tagging rules for the logged in user. | |
571 | * | |
572 | * @Route("/tagging-rule/export", name="export_tagging_rule") | |
573 | * | |
574 | * @return Response | |
575 | */ | |
576 | public function exportTaggingRulesAction() | |
577 | { | |
578 | $data = SerializerBuilder::create()->build()->serialize( | |
579 | $this->getUser()->getConfig()->getTaggingRules(), | |
580 | 'json', | |
581 | SerializationContext::create()->setGroups(['export_tagging_rule']) | |
582 | ); | |
583 | ||
584 | return Response::create( | |
585 | $data, | |
586 | 200, | |
587 | [ | |
588 | 'Content-type' => 'application/json', | |
589 | 'Content-Disposition' => 'attachment; filename="tagging_rules_' . $this->getUser()->getUsername() . '.json"', | |
590 | 'Content-Transfer-Encoding' => 'UTF-8', | |
591 | ] | |
592 | ); | |
593 | } | |
594 | ||
191564b7 | 595 | /** |
e682a70f | 596 | * Remove all tags for given tags and a given user and cleanup orphan tags. |
191564b7 | 597 | * |
e682a70f NL |
598 | * @param array $tags |
599 | * @param int $userId | |
191564b7 | 600 | */ |
e682a70f | 601 | private function removeAllTagsByStatusAndUserId($tags, $userId) |
191564b7 | 602 | { |
191564b7 JB |
603 | if (empty($tags)) { |
604 | return; | |
605 | } | |
606 | ||
25203e50 | 607 | $this->get('wallabag_core.entry_repository') |
191564b7 | 608 | ->removeTags($userId, $tags); |
f71e55ac | 609 | |
8c61fd12 | 610 | // cleanup orphan tags |
f71e55ac JB |
611 | $em = $this->getDoctrine()->getManager(); |
612 | ||
613 | foreach ($tags as $tag) { | |
2a1ceb67 | 614 | if (0 === \count($tag->getEntries())) { |
f71e55ac JB |
615 | $em->remove($tag); |
616 | } | |
617 | } | |
618 | ||
619 | $em->flush(); | |
191564b7 JB |
620 | } |
621 | ||
e682a70f NL |
622 | /** |
623 | * Remove all tags for a given user and cleanup orphan tags. | |
624 | * | |
625 | * @param int $userId | |
626 | */ | |
627 | private function removeAllTagsByUserId($userId) | |
628 | { | |
25203e50 | 629 | $tags = $this->get('wallabag_core.tag_repository')->findAllTags($userId); |
e682a70f NL |
630 | $this->removeAllTagsByStatusAndUserId($tags, $userId); |
631 | } | |
632 | ||
6da1aebc TC |
633 | /** |
634 | * Remove all tags for a given user and cleanup orphan tags. | |
635 | * | |
636 | * @param int $userId | |
637 | */ | |
638 | private function removeTagsForArchivedByUserId($userId) | |
639 | { | |
25203e50 | 640 | $tags = $this->get('wallabag_core.tag_repository')->findForArchivedArticlesByUser($userId); |
e682a70f | 641 | $this->removeAllTagsByStatusAndUserId($tags, $userId); |
6da1aebc TC |
642 | } |
643 | ||
644 | private function removeAnnotationsForArchivedByUserId($userId) | |
645 | { | |
646 | $em = $this->getDoctrine()->getManager(); | |
647 | ||
648 | $archivedEntriesAnnotations = $this->getDoctrine() | |
649 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
13a592a1 | 650 | ->findAllArchivedEntriesByUser($userId); |
6da1aebc TC |
651 | |
652 | foreach ($archivedEntriesAnnotations as $archivedEntriesAnnotation) { | |
653 | $em->remove($archivedEntriesAnnotation); | |
654 | } | |
655 | ||
656 | $em->flush(); | |
657 | } | |
658 | ||
8799bde0 | 659 | /** |
2455472e | 660 | * Validate that a rule can be edited/deleted by the current user. |
8799bde0 JB |
661 | */ |
662 | private function validateRuleAction(TaggingRule $rule) | |
bf3dc999 | 663 | { |
f808b016 | 664 | if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) { |
bf3dc999 JB |
665 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); |
666 | } | |
bf3dc999 JB |
667 | } |
668 | ||
d9085c63 J |
669 | /** |
670 | * Retrieve config for the current user. | |
671 | * If no config were found, create a new one. | |
672 | * | |
4094ea47 | 673 | * @return Config |
d9085c63 | 674 | */ |
4d85d7e9 J |
675 | private function getConfig() |
676 | { | |
677 | $config = $this->getDoctrine() | |
678 | ->getRepository('WallabagCoreBundle:Config') | |
679 | ->findOneByUser($this->getUser()); | |
680 | ||
ca17abce | 681 | // should NEVER HAPPEN ... |
4d85d7e9 J |
682 | if (!$config) { |
683 | $config = new Config($this->getUser()); | |
684 | } | |
685 | ||
686 | return $config; | |
687 | } | |
688 | } |