]>
Commit | Line | Data |
---|---|---|
4d85d7e9 J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
4d85d7e9 | 5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
0c83fd59 | 6 | use Symfony\Component\HttpFoundation\JsonResponse; |
98568055 | 7 | use Symfony\Component\HttpFoundation\RedirectResponse; |
619cc453 | 8 | use Symfony\Component\HttpFoundation\Request; |
bb0c78f4 | 9 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
115de64e | 10 | use Symfony\Component\Routing\Annotation\Route; |
4d4147b2 | 11 | use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint; |
4d85d7e9 | 12 | use Wallabag\CoreBundle\Entity\Config; |
f19f9f62 | 13 | use Wallabag\CoreBundle\Entity\TaggingRule; |
d9085c63 | 14 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
f808b016 | 15 | use Wallabag\CoreBundle\Form\Type\ConfigType; |
0c83fd59 | 16 | use Wallabag\CoreBundle\Form\Type\RssType; |
619cc453 JB |
17 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; |
18 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | |
0c83fd59 | 19 | use Wallabag\CoreBundle\Tools\Utils; |
4d85d7e9 J |
20 | |
21 | class ConfigController extends Controller | |
22 | { | |
23 | /** | |
24 | * @param Request $request | |
25 | * | |
26 | * @Route("/config", name="config") | |
4d85d7e9 J |
27 | */ |
28 | public function indexAction(Request $request) | |
29 | { | |
d9085c63 | 30 | $em = $this->getDoctrine()->getManager(); |
4d85d7e9 | 31 | $config = $this->getConfig(); |
fcb1fba5 | 32 | $userManager = $this->container->get('fos_user.user_manager'); |
c0d9eba0 | 33 | $user = $this->getUser(); |
4d85d7e9 | 34 | |
32da2a70 | 35 | // handle basic config detail (this form is defined as a service) |
4094ea47 | 36 | $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]); |
d9085c63 | 37 | $configForm->handleRequest($request); |
4d85d7e9 | 38 | |
21e7ccef | 39 | if ($configForm->isSubmitted() && $configForm->isValid()) { |
4d85d7e9 J |
40 | $em->persist($config); |
41 | $em->flush(); | |
42 | ||
ece4718f NL |
43 | $request->getSession()->set('_locale', $config->getLanguage()); |
44 | ||
32da2a70 J |
45 | // switch active theme |
46 | $activeTheme = $this->get('liip_theme.active_theme'); | |
47 | $activeTheme->setName($config->getTheme()); | |
48 | ||
a6b242a1 | 49 | $this->addFlash( |
4d85d7e9 | 50 | 'notice', |
4204a06b | 51 | 'flashes.config.notice.config_saved' |
4d85d7e9 J |
52 | ); |
53 | ||
54 | return $this->redirect($this->generateUrl('config')); | |
55 | } | |
56 | ||
d9085c63 | 57 | // handle changing password |
f808b016 | 58 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config') . '#set4']); |
d9085c63 J |
59 | $pwdForm->handleRequest($request); |
60 | ||
21e7ccef | 61 | if ($pwdForm->isSubmitted() && $pwdForm->isValid()) { |
a4f42c59 | 62 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { |
4204a06b | 63 | $message = 'flashes.config.notice.password_not_updated_demo'; |
6c9f50a6 | 64 | } else { |
4204a06b | 65 | $message = 'flashes.config.notice.password_updated'; |
b6c00b0b | 66 | |
d8d56448 NL |
67 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); |
68 | $userManager->updateUser($user, true); | |
6c9f50a6 | 69 | } |
d9085c63 | 70 | |
a6b242a1 | 71 | $this->addFlash('notice', $message); |
b6c00b0b | 72 | |
f808b016 | 73 | return $this->redirect($this->generateUrl('config') . '#set4'); |
d9085c63 J |
74 | } |
75 | ||
c0d9eba0 | 76 | // handle changing user information |
4094ea47 JB |
77 | $userForm = $this->createForm(UserInformationType::class, $user, [ |
78 | 'validation_groups' => ['Profile'], | |
f808b016 | 79 | 'action' => $this->generateUrl('config') . '#set3', |
4094ea47 | 80 | ]); |
c0d9eba0 J |
81 | $userForm->handleRequest($request); |
82 | ||
a6b242a1 | 83 | // `googleTwoFactor` isn't a field within the User entity, we need to define it's value in a different way |
2dfbe9e5 | 84 | if ($this->getParameter('twofactor_auth') && true === $user->isGoogleAuthenticatorEnabled() && false === $userForm->isSubmitted()) { |
a6b242a1 JB |
85 | $userForm->get('googleTwoFactor')->setData(true); |
86 | } | |
87 | ||
21e7ccef | 88 | if ($userForm->isSubmitted() && $userForm->isValid()) { |
a6b242a1 | 89 | // handle creation / reset of the OTP secret if checkbox changed from the previous state |
2dfbe9e5 JB |
90 | if ($this->getParameter('twofactor_auth')) { |
91 | if (true === $userForm->get('googleTwoFactor')->getData() && false === $user->isGoogleAuthenticatorEnabled()) { | |
92 | $secret = $this->get('scheb_two_factor.security.google_authenticator')->generateSecret(); | |
a6b242a1 | 93 | |
2dfbe9e5 JB |
94 | $user->setGoogleAuthenticatorSecret($secret); |
95 | $user->setEmailTwoFactor(false); | |
a6b242a1 | 96 | |
2dfbe9e5 JB |
97 | $this->addFlash('OtpQrCode', $this->get('scheb_two_factor.security.google_authenticator')->getQRContent($user)); |
98 | } elseif (false === $userForm->get('googleTwoFactor')->getData() && true === $user->isGoogleAuthenticatorEnabled()) { | |
99 | $user->setGoogleAuthenticatorSecret(null); | |
100 | } | |
a6b242a1 JB |
101 | } |
102 | ||
fcb1fba5 | 103 | $userManager->updateUser($user, true); |
c0d9eba0 | 104 | |
a6b242a1 | 105 | $this->addFlash( |
c0d9eba0 | 106 | 'notice', |
4204a06b | 107 | 'flashes.config.notice.user_updated' |
c0d9eba0 J |
108 | ); |
109 | ||
f808b016 | 110 | return $this->redirect($this->generateUrl('config') . '#set3'); |
c0d9eba0 J |
111 | } |
112 | ||
0c83fd59 | 113 | // handle rss information |
f808b016 | 114 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config') . '#set2']); |
0c83fd59 J |
115 | $rssForm->handleRequest($request); |
116 | ||
21e7ccef | 117 | if ($rssForm->isSubmitted() && $rssForm->isValid()) { |
0c83fd59 J |
118 | $em->persist($config); |
119 | $em->flush(); | |
120 | ||
a6b242a1 | 121 | $this->addFlash( |
0c83fd59 | 122 | 'notice', |
4204a06b | 123 | 'flashes.config.notice.rss_updated' |
0c83fd59 J |
124 | ); |
125 | ||
f808b016 | 126 | return $this->redirect($this->generateUrl('config') . '#set2'); |
0c83fd59 J |
127 | } |
128 | ||
f19f9f62 KG |
129 | // handle tagging rule |
130 | $taggingRule = new TaggingRule(); | |
f808b016 | 131 | $action = $this->generateUrl('config') . '#set5'; |
bf3dc999 JB |
132 | |
133 | if ($request->query->has('tagging-rule')) { | |
134 | $taggingRule = $this->getDoctrine() | |
135 | ->getRepository('WallabagCoreBundle:TaggingRule') | |
136 | ->find($request->query->get('tagging-rule')); | |
137 | ||
138 | if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) { | |
139 | return $this->redirect($action); | |
140 | } | |
141 | ||
f808b016 | 142 | $action = $this->generateUrl('config') . '?tagging-rule=' . $taggingRule->getId() . '#set5'; |
bf3dc999 JB |
143 | } |
144 | ||
145 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); | |
f19f9f62 KG |
146 | $newTaggingRule->handleRequest($request); |
147 | ||
21e7ccef | 148 | if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) { |
f19f9f62 KG |
149 | $taggingRule->setConfig($config); |
150 | $em->persist($taggingRule); | |
151 | $em->flush(); | |
152 | ||
a6b242a1 | 153 | $this->addFlash( |
f19f9f62 | 154 | 'notice', |
4204a06b | 155 | 'flashes.config.notice.tagging_rules_updated' |
f19f9f62 KG |
156 | ); |
157 | ||
f808b016 | 158 | return $this->redirect($this->generateUrl('config') . '#set5'); |
f19f9f62 KG |
159 | } |
160 | ||
4094ea47 JB |
161 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ |
162 | 'form' => [ | |
0c83fd59 J |
163 | 'config' => $configForm->createView(), |
164 | 'rss' => $rssForm->createView(), | |
165 | 'pwd' => $pwdForm->createView(), | |
166 | 'user' => $userForm->createView(), | |
f19f9f62 | 167 | 'new_tagging_rule' => $newTaggingRule->createView(), |
4094ea47 JB |
168 | ], |
169 | 'rss' => [ | |
0c83fd59 J |
170 | 'username' => $user->getUsername(), |
171 | 'token' => $config->getRssToken(), | |
4094ea47 | 172 | ], |
63e40f2d | 173 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), |
be9d693e | 174 | 'wallabag_url' => $this->getParameter('domain_name'), |
25203e50 | 175 | 'enabled_users' => $this->get('wallabag_user.user_repository') |
bb0c78f4 | 176 | ->getSumEnabledUsers(), |
4094ea47 | 177 | ]); |
4d85d7e9 J |
178 | } |
179 | ||
0c83fd59 J |
180 | /** |
181 | * @param Request $request | |
182 | * | |
183 | * @Route("/generate-token", name="generate_token") | |
184 | * | |
98568055 | 185 | * @return RedirectResponse|JsonResponse |
0c83fd59 J |
186 | */ |
187 | public function generateTokenAction(Request $request) | |
188 | { | |
189 | $config = $this->getConfig(); | |
190 | $config->setRssToken(Utils::generateToken()); | |
191 | ||
192 | $em = $this->getDoctrine()->getManager(); | |
193 | $em->persist($config); | |
194 | $em->flush(); | |
195 | ||
196 | if ($request->isXmlHttpRequest()) { | |
4094ea47 | 197 | return new JsonResponse(['token' => $config->getRssToken()]); |
0c83fd59 J |
198 | } |
199 | ||
a6b242a1 | 200 | $this->addFlash( |
c7a4f74f | 201 | 'notice', |
4204a06b | 202 | 'flashes.config.notice.rss_token_updated' |
c7a4f74f JB |
203 | ); |
204 | ||
f808b016 | 205 | return $this->redirect($this->generateUrl('config') . '#set2'); |
0c83fd59 J |
206 | } |
207 | ||
52e423f3 KG |
208 | /** |
209 | * Deletes a tagging rule and redirect to the config homepage. | |
210 | * | |
211 | * @param TaggingRule $rule | |
212 | * | |
213 | * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule") | |
214 | * | |
98568055 | 215 | * @return RedirectResponse |
52e423f3 | 216 | */ |
5f8a7857 | 217 | public function deleteTaggingRuleAction(TaggingRule $rule) |
52e423f3 | 218 | { |
8799bde0 | 219 | $this->validateRuleAction($rule); |
52e423f3 KG |
220 | |
221 | $em = $this->getDoctrine()->getManager(); | |
222 | $em->remove($rule); | |
223 | $em->flush(); | |
224 | ||
a6b242a1 | 225 | $this->addFlash( |
52e423f3 | 226 | 'notice', |
4204a06b | 227 | 'flashes.config.notice.tagging_rules_deleted' |
52e423f3 KG |
228 | ); |
229 | ||
f808b016 | 230 | return $this->redirect($this->generateUrl('config') . '#set5'); |
52e423f3 KG |
231 | } |
232 | ||
bf3dc999 JB |
233 | /** |
234 | * Edit a tagging rule. | |
235 | * | |
236 | * @param TaggingRule $rule | |
237 | * | |
238 | * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule") | |
239 | * | |
240 | * @return RedirectResponse | |
241 | */ | |
242 | public function editTaggingRuleAction(TaggingRule $rule) | |
8799bde0 JB |
243 | { |
244 | $this->validateRuleAction($rule); | |
245 | ||
f808b016 | 246 | return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5'); |
8799bde0 JB |
247 | } |
248 | ||
206bade5 JB |
249 | /** |
250 | * Remove all annotations OR tags OR entries for the current user. | |
251 | * | |
252 | * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset") | |
253 | * | |
254 | * @return RedirectResponse | |
255 | */ | |
256 | public function resetAction($type) | |
257 | { | |
206bade5 JB |
258 | switch ($type) { |
259 | case 'annotations': | |
191564b7 JB |
260 | $this->getDoctrine() |
261 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
262 | ->removeAllByUserId($this->getUser()->getId()); | |
206bade5 | 263 | break; |
206bade5 | 264 | case 'tags': |
191564b7 JB |
265 | $this->removeAllTagsByUserId($this->getUser()->getId()); |
266 | break; | |
191564b7 | 267 | case 'entries': |
6da1aebc | 268 | // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff |
191564b7 | 269 | // otherwise they won't be removed ... |
7ab5eb95 | 270 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { |
191564b7 | 271 | $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); |
206bade5 JB |
272 | } |
273 | ||
8c61fd12 | 274 | // manually remove tags to avoid orphan tag |
f71e55ac JB |
275 | $this->removeAllTagsByUserId($this->getUser()->getId()); |
276 | ||
25203e50 | 277 | $this->get('wallabag_core.entry_repository')->removeAllByUserId($this->getUser()->getId()); |
6da1aebc TC |
278 | break; |
279 | case 'archived': | |
7ab5eb95 | 280 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { |
6da1aebc TC |
281 | $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId()); |
282 | } | |
283 | ||
284 | // manually remove tags to avoid orphan tag | |
285 | $this->removeTagsForArchivedByUserId($this->getUser()->getId()); | |
286 | ||
25203e50 | 287 | $this->get('wallabag_core.entry_repository')->removeArchivedByUserId($this->getUser()->getId()); |
6da1aebc | 288 | break; |
206bade5 JB |
289 | } |
290 | ||
a6b242a1 | 291 | $this->addFlash( |
206bade5 | 292 | 'notice', |
f808b016 | 293 | 'flashes.config.notice.' . $type . '_reset' |
206bade5 JB |
294 | ); |
295 | ||
f808b016 JB |
296 | return $this->redirect($this->generateUrl('config') . '#set3'); |
297 | } | |
298 | ||
299 | /** | |
300 | * Delete account for current user. | |
301 | * | |
302 | * @Route("/account/delete", name="delete_account") | |
303 | * | |
304 | * @param Request $request | |
305 | * | |
306 | * @throws AccessDeniedHttpException | |
307 | * | |
308 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
309 | */ | |
310 | public function deleteAccountAction(Request $request) | |
311 | { | |
312 | $enabledUsers = $this->get('wallabag_user.user_repository') | |
313 | ->getSumEnabledUsers(); | |
314 | ||
315 | if ($enabledUsers <= 1) { | |
316 | throw new AccessDeniedHttpException(); | |
317 | } | |
318 | ||
319 | $user = $this->getUser(); | |
320 | ||
321 | // logout current user | |
322 | $this->get('security.token_storage')->setToken(null); | |
323 | $request->getSession()->invalidate(); | |
324 | ||
325 | $em = $this->get('fos_user.user_manager'); | |
326 | $em->deleteUser($user); | |
327 | ||
328 | return $this->redirect($this->generateUrl('fos_user_security_login')); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Switch view mode for current user. | |
333 | * | |
334 | * @Route("/config/view-mode", name="switch_view_mode") | |
335 | * | |
336 | * @param Request $request | |
337 | * | |
338 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
339 | */ | |
340 | public function changeViewModeAction(Request $request) | |
341 | { | |
342 | $user = $this->getUser(); | |
343 | $user->getConfig()->setListMode(!$user->getConfig()->getListMode()); | |
344 | ||
345 | $em = $this->getDoctrine()->getManager(); | |
346 | $em->persist($user); | |
347 | $em->flush(); | |
348 | ||
349 | return $this->redirect($request->headers->get('referer')); | |
206bade5 JB |
350 | } |
351 | ||
be417ef2 NL |
352 | /** |
353 | * Change the locale for the current user. | |
354 | * | |
355 | * @param Request $request | |
356 | * @param string $language | |
357 | * | |
358 | * @Route("/locale/{language}", name="changeLocale") | |
359 | * | |
360 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
361 | */ | |
362 | public function setLocaleAction(Request $request, $language = null) | |
363 | { | |
4d4147b2 JB |
364 | $errors = $this->get('validator')->validate($language, (new LocaleConstraint())); |
365 | ||
366 | if (0 === \count($errors)) { | |
367 | $request->getSession()->set('_locale', $language); | |
be417ef2 NL |
368 | } |
369 | ||
4d4147b2 | 370 | return $this->redirect($request->headers->get('referer', $this->generateUrl('homepage'))); |
be417ef2 NL |
371 | } |
372 | ||
191564b7 | 373 | /** |
e682a70f | 374 | * Remove all tags for given tags and a given user and cleanup orphan tags. |
191564b7 | 375 | * |
e682a70f NL |
376 | * @param array $tags |
377 | * @param int $userId | |
191564b7 | 378 | */ |
e682a70f | 379 | private function removeAllTagsByStatusAndUserId($tags, $userId) |
191564b7 | 380 | { |
191564b7 JB |
381 | if (empty($tags)) { |
382 | return; | |
383 | } | |
384 | ||
25203e50 | 385 | $this->get('wallabag_core.entry_repository') |
191564b7 | 386 | ->removeTags($userId, $tags); |
f71e55ac | 387 | |
8c61fd12 | 388 | // cleanup orphan tags |
f71e55ac JB |
389 | $em = $this->getDoctrine()->getManager(); |
390 | ||
391 | foreach ($tags as $tag) { | |
2a1ceb67 | 392 | if (0 === \count($tag->getEntries())) { |
f71e55ac JB |
393 | $em->remove($tag); |
394 | } | |
395 | } | |
396 | ||
397 | $em->flush(); | |
191564b7 JB |
398 | } |
399 | ||
e682a70f NL |
400 | /** |
401 | * Remove all tags for a given user and cleanup orphan tags. | |
402 | * | |
403 | * @param int $userId | |
404 | */ | |
405 | private function removeAllTagsByUserId($userId) | |
406 | { | |
25203e50 | 407 | $tags = $this->get('wallabag_core.tag_repository')->findAllTags($userId); |
e682a70f NL |
408 | $this->removeAllTagsByStatusAndUserId($tags, $userId); |
409 | } | |
410 | ||
6da1aebc TC |
411 | /** |
412 | * Remove all tags for a given user and cleanup orphan tags. | |
413 | * | |
414 | * @param int $userId | |
415 | */ | |
416 | private function removeTagsForArchivedByUserId($userId) | |
417 | { | |
25203e50 | 418 | $tags = $this->get('wallabag_core.tag_repository')->findForArchivedArticlesByUser($userId); |
e682a70f | 419 | $this->removeAllTagsByStatusAndUserId($tags, $userId); |
6da1aebc TC |
420 | } |
421 | ||
422 | private function removeAnnotationsForArchivedByUserId($userId) | |
423 | { | |
424 | $em = $this->getDoctrine()->getManager(); | |
425 | ||
426 | $archivedEntriesAnnotations = $this->getDoctrine() | |
427 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
13a592a1 | 428 | ->findAllArchivedEntriesByUser($userId); |
6da1aebc TC |
429 | |
430 | foreach ($archivedEntriesAnnotations as $archivedEntriesAnnotation) { | |
431 | $em->remove($archivedEntriesAnnotation); | |
432 | } | |
433 | ||
434 | $em->flush(); | |
435 | } | |
436 | ||
8799bde0 | 437 | /** |
2455472e | 438 | * Validate that a rule can be edited/deleted by the current user. |
8799bde0 | 439 | * |
2455472e | 440 | * @param TaggingRule $rule |
8799bde0 JB |
441 | */ |
442 | private function validateRuleAction(TaggingRule $rule) | |
bf3dc999 | 443 | { |
f808b016 | 444 | if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) { |
bf3dc999 JB |
445 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); |
446 | } | |
bf3dc999 JB |
447 | } |
448 | ||
d9085c63 J |
449 | /** |
450 | * Retrieve config for the current user. | |
451 | * If no config were found, create a new one. | |
452 | * | |
4094ea47 | 453 | * @return Config |
d9085c63 | 454 | */ |
4d85d7e9 J |
455 | private function getConfig() |
456 | { | |
457 | $config = $this->getDoctrine() | |
458 | ->getRepository('WallabagCoreBundle:Config') | |
459 | ->findOneByUser($this->getUser()); | |
460 | ||
ca17abce | 461 | // should NEVER HAPPEN ... |
4d85d7e9 J |
462 | if (!$config) { |
463 | $config = new Config($this->getUser()); | |
464 | } | |
465 | ||
466 | return $config; | |
467 | } | |
468 | } |