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