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