3 namespace Wallabag\CoreBundle\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
6 use Symfony\Component\HttpFoundation\JsonResponse
;
7 use Symfony\Component\HttpFoundation\RedirectResponse
;
8 use Symfony\Component\HttpFoundation\Request
;
9 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
;
10 use Symfony\Component\Routing\Annotation\Route
;
11 use Symfony\Component\Validator\Constraints\Locale
as LocaleConstraint
;
12 use Wallabag\CoreBundle\Entity\Config
;
13 use Wallabag\CoreBundle\Entity\TaggingRule
;
14 use Wallabag\CoreBundle\Form\Type\ChangePasswordType
;
15 use Wallabag\CoreBundle\Form\Type\ConfigType
;
16 use Wallabag\CoreBundle\Form\Type\RssType
;
17 use Wallabag\CoreBundle\Form\Type\TaggingRuleType
;
18 use Wallabag\CoreBundle\Form\Type\UserInformationType
;
19 use Wallabag\CoreBundle\Tools\Utils
;
21 class ConfigController
extends Controller
24 * @param Request $request
26 * @Route("/config", name="config")
28 public function indexAction(Request
$request)
30 $em = $this->getDoctrine()->getManager();
31 $config = $this->getConfig();
32 $userManager = $this->container
->get('fos_user.user_manager');
33 $user = $this->getUser();
35 // handle basic config detail (this form is defined as a service)
36 $configForm = $this->createForm(ConfigType
::class, $config, ['action' => $this->generateUrl('config')]);
37 $configForm->handleRequest($request);
39 if ($configForm->isSubmitted() && $configForm->isValid()) {
40 $em->persist($config);
43 $request->getSession()->set('_locale', $config->getLanguage());
45 // switch active theme
46 $activeTheme = $this->get('liip_theme.active_theme');
47 $activeTheme->setName($config->getTheme());
49 $this->get('session')->getFlashBag()->add(
51 'flashes.config.notice.config_saved'
54 return $this->redirect($this->generateUrl('config'));
57 // handle changing password
58 $pwdForm = $this->createForm(ChangePasswordType
::class, null, ['action' => $this->generateUrl('config') . '#set4']);
59 $pwdForm->handleRequest($request);
61 if ($pwdForm->isSubmitted() && $pwdForm->isValid()) {
62 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
63 $message = 'flashes.config.notice.password_not_updated_demo';
65 $message = 'flashes.config.notice.password_updated';
67 $user->setPlainPassword($pwdForm->get('new_password')->getData());
68 $userManager->updateUser($user, true);
71 $this->get('session')->getFlashBag()->add('notice', $message);
73 return $this->redirect($this->generateUrl('config') . '#set4');
76 // handle changing user information
77 $userForm = $this->createForm(UserInformationType
::class, $user, [
78 'validation_groups' => ['Profile'],
79 'action' => $this->generateUrl('config') . '#set3',
81 $userForm->handleRequest($request);
83 if ($userForm->isSubmitted() && $userForm->isValid()) {
84 $userManager->updateUser($user, true);
86 $this->get('session')->getFlashBag()->add(
88 'flashes.config.notice.user_updated'
91 return $this->redirect($this->generateUrl('config') . '#set3');
94 // handle rss information
95 $rssForm = $this->createForm(RssType
::class, $config, ['action' => $this->generateUrl('config') . '#set2']);
96 $rssForm->handleRequest($request);
98 if ($rssForm->isSubmitted() && $rssForm->isValid()) {
99 $em->persist($config);
102 $this->get('session')->getFlashBag()->add(
104 'flashes.config.notice.rss_updated'
107 return $this->redirect($this->generateUrl('config') . '#set2');
110 // handle tagging rule
111 $taggingRule = new TaggingRule();
112 $action = $this->generateUrl('config') . '#set5';
114 if ($request->query
->has('tagging-rule')) {
115 $taggingRule = $this->getDoctrine()
116 ->getRepository('WallabagCoreBundle:TaggingRule')
117 ->find($request->query
->get('tagging-rule'));
119 if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
120 return $this->redirect($action);
123 $action = $this->generateUrl('config') . '?tagging-rule=' . $taggingRule->getId() . '#set5';
126 $newTaggingRule = $this->createForm(TaggingRuleType
::class, $taggingRule, ['action' => $action]);
127 $newTaggingRule->handleRequest($request);
129 if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) {
130 $taggingRule->setConfig($config);
131 $em->persist($taggingRule);
134 $this->get('session')->getFlashBag()->add(
136 'flashes.config.notice.tagging_rules_updated'
139 return $this->redirect($this->generateUrl('config') . '#set5');
142 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
144 'config' => $configForm->createView(),
145 'rss' => $rssForm->createView(),
146 'pwd' => $pwdForm->createView(),
147 'user' => $userForm->createView(),
148 'new_tagging_rule' => $newTaggingRule->createView(),
151 'username' => $user->getUsername(),
152 'token' => $config->getRssToken(),
154 'twofactor_auth' => $this->getParameter('twofactor_auth'),
155 'wallabag_url' => $this->getParameter('domain_name'),
156 'enabled_users' => $this->get('wallabag_user.user_repository')
157 ->getSumEnabledUsers(),
162 * @param Request $request
164 * @Route("/generate-token", name="generate_token")
166 * @return RedirectResponse|JsonResponse
168 public function generateTokenAction(Request
$request)
170 $config = $this->getConfig();
171 $config->setRssToken(Utils
::generateToken());
173 $em = $this->getDoctrine()->getManager();
174 $em->persist($config);
177 if ($request->isXmlHttpRequest()) {
178 return new JsonResponse(['token' => $config->getRssToken()]);
181 $this->get('session')->getFlashBag()->add(
183 'flashes.config.notice.rss_token_updated'
186 return $this->redirect($this->generateUrl('config') . '#set2');
190 * Deletes a tagging rule and redirect to the config homepage.
192 * @param TaggingRule $rule
194 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
196 * @return RedirectResponse
198 public function deleteTaggingRuleAction(TaggingRule
$rule)
200 $this->validateRuleAction($rule);
202 $em = $this->getDoctrine()->getManager();
206 $this->get('session')->getFlashBag()->add(
208 'flashes.config.notice.tagging_rules_deleted'
211 return $this->redirect($this->generateUrl('config') . '#set5');
215 * Edit a tagging rule.
217 * @param TaggingRule $rule
219 * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
221 * @return RedirectResponse
223 public function editTaggingRuleAction(TaggingRule
$rule)
225 $this->validateRuleAction($rule);
227 return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5');
231 * Remove all annotations OR tags OR entries for the current user.
233 * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset")
235 * @return RedirectResponse
237 public function resetAction($type)
242 ->getRepository('WallabagAnnotationBundle:Annotation')
243 ->removeAllByUserId($this->getUser()->getId());
246 $this->removeAllTagsByUserId($this->getUser()->getId());
249 // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff
250 // otherwise they won't be removed ...
251 if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
) {
252 $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId());
255 // manually remove tags to avoid orphan tag
256 $this->removeAllTagsByUserId($this->getUser()->getId());
258 $this->get('wallabag_core.entry_repository')->removeAllByUserId($this->getUser()->getId());
261 if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
) {
262 $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId());
265 // manually remove tags to avoid orphan tag
266 $this->removeTagsForArchivedByUserId($this->getUser()->getId());
268 $this->get('wallabag_core.entry_repository')->removeArchivedByUserId($this->getUser()->getId());
272 $this->get('session')->getFlashBag()->add(
274 'flashes.config.notice.' . $type . '_reset'
277 return $this->redirect($this->generateUrl('config') . '#set3');
281 * Delete account for current user.
283 * @Route("/account/delete", name="delete_account")
285 * @param Request $request
287 * @throws AccessDeniedHttpException
289 * @return \Symfony\Component\HttpFoundation\RedirectResponse
291 public function deleteAccountAction(Request
$request)
293 $enabledUsers = $this->get('wallabag_user.user_repository')
294 ->getSumEnabledUsers();
296 if ($enabledUsers <= 1) {
297 throw new AccessDeniedHttpException();
300 $user = $this->getUser();
302 // logout current user
303 $this->get('security.token_storage')->setToken(null);
304 $request->getSession()->invalidate();
306 $em = $this->get('fos_user.user_manager');
307 $em->deleteUser($user);
309 return $this->redirect($this->generateUrl('fos_user_security_login'));
313 * Switch view mode for current user.
315 * @Route("/config/view-mode", name="switch_view_mode")
317 * @param Request $request
319 * @return \Symfony\Component\HttpFoundation\RedirectResponse
321 public function changeViewModeAction(Request
$request)
323 $user = $this->getUser();
324 $user->getConfig()->setListMode(!$user->getConfig()->getListMode());
326 $em = $this->getDoctrine()->getManager();
330 return $this->redirect($request->headers
->get('referer'));
334 * Change the locale for the current user.
336 * @param Request $request
337 * @param string $language
339 * @Route("/locale/{language}", name="changeLocale")
341 * @return \Symfony\Component\HttpFoundation\RedirectResponse
343 public function setLocaleAction(Request
$request, $language = null)
345 $errors = $this->get('validator')->validate($language, (new LocaleConstraint()));
347 if (0 === \
count($errors)) {
348 $request->getSession()->set('_locale', $language);
351 return $this->redirect($request->headers
->get('referer', $this->generateUrl('homepage')));
355 * Remove all tags for given tags and a given user and cleanup orphan tags.
360 private function removeAllTagsByStatusAndUserId($tags, $userId)
366 $this->get('wallabag_core.entry_repository')
367 ->removeTags($userId, $tags);
369 // cleanup orphan tags
370 $em = $this->getDoctrine()->getManager();
372 foreach ($tags as $tag) {
373 if (0 === \
count($tag->getEntries())) {
382 * Remove all tags for a given user and cleanup orphan tags.
386 private function removeAllTagsByUserId($userId)
388 $tags = $this->get('wallabag_core.tag_repository')->findAllTags($userId);
389 $this->removeAllTagsByStatusAndUserId($tags, $userId);
393 * Remove all tags for a given user and cleanup orphan tags.
397 private function removeTagsForArchivedByUserId($userId)
399 $tags = $this->get('wallabag_core.tag_repository')->findForArchivedArticlesByUser($userId);
400 $this->removeAllTagsByStatusAndUserId($tags, $userId);
403 private function removeAnnotationsForArchivedByUserId($userId)
405 $em = $this->getDoctrine()->getManager();
407 $archivedEntriesAnnotations = $this->getDoctrine()
408 ->getRepository('WallabagAnnotationBundle:Annotation')
409 ->findAllArchivedEntriesByUser($userId);
411 foreach ($archivedEntriesAnnotations as $archivedEntriesAnnotation) {
412 $em->remove($archivedEntriesAnnotation);
419 * Validate that a rule can be edited/deleted by the current user.
421 * @param TaggingRule $rule
423 private function validateRuleAction(TaggingRule
$rule)
425 if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) {
426 throw $this->createAccessDeniedException('You can not access this tagging rule.');
431 * Retrieve config for the current user.
432 * If no config were found, create a new one.
436 private function getConfig()
438 $config = $this->getDoctrine()
439 ->getRepository('WallabagCoreBundle:Config')
440 ->findOneByUser($this->getUser());
442 // should NEVER HAPPEN ...
444 $config = new Config($this->getUser());