3 namespace Wallabag\CoreBundle\Controller
;
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
7 use Symfony\Component\HttpFoundation\JsonResponse
;
8 use Symfony\Component\HttpFoundation\RedirectResponse
;
9 use Symfony\Component\HttpFoundation\Request
;
10 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
;
11 use Wallabag\CoreBundle\Entity\Config
;
12 use Wallabag\CoreBundle\Entity\TaggingRule
;
13 use Wallabag\CoreBundle\Form\Type\ChangePasswordType
;
14 use Wallabag\CoreBundle\Form\Type\ConfigType
;
15 use Wallabag\CoreBundle\Form\Type\RssType
;
16 use Wallabag\CoreBundle\Form\Type\TaggingRuleType
;
17 use Wallabag\CoreBundle\Form\Type\UserInformationType
;
18 use Wallabag\CoreBundle\Tools\Utils
;
20 class ConfigController
extends Controller
23 * @param Request $request
25 * @Route("/config", name="config")
27 public function indexAction(Request
$request)
29 $em = $this->getDoctrine()->getManager();
30 $config = $this->getConfig();
31 $userManager = $this->container
->get('fos_user.user_manager');
32 $user = $this->getUser();
34 // handle basic config detail (this form is defined as a service)
35 $configForm = $this->createForm(ConfigType
::class, $config, ['action' => $this->generateUrl('config')]);
36 $configForm->handleRequest($request);
38 if ($configForm->isSubmitted() && $configForm->isValid()) {
39 $em->persist($config);
42 $request->getSession()->set('_locale', $config->getLanguage());
44 // switch active theme
45 $activeTheme = $this->get('liip_theme.active_theme');
46 $activeTheme->setName($config->getTheme());
48 $this->get('session')->getFlashBag()->add(
50 'flashes.config.notice.config_saved'
53 return $this->redirect($this->generateUrl('config'));
56 // handle changing password
57 $pwdForm = $this->createForm(ChangePasswordType
::class, null, ['action' => $this->generateUrl('config') . '#set4']);
58 $pwdForm->handleRequest($request);
60 if ($pwdForm->isSubmitted() && $pwdForm->isValid()) {
61 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
62 $message = 'flashes.config.notice.password_not_updated_demo';
64 $message = 'flashes.config.notice.password_updated';
66 $user->setPlainPassword($pwdForm->get('new_password')->getData());
67 $userManager->updateUser($user, true);
70 $this->get('session')->getFlashBag()->add('notice', $message);
72 return $this->redirect($this->generateUrl('config') . '#set4');
75 // handle changing user information
76 $userForm = $this->createForm(UserInformationType
::class, $user, [
77 'validation_groups' => ['Profile'],
78 'action' => $this->generateUrl('config') . '#set3',
80 $userForm->handleRequest($request);
82 if ($userForm->isSubmitted() && $userForm->isValid()) {
83 $userManager->updateUser($user, true);
85 $this->get('session')->getFlashBag()->add(
87 'flashes.config.notice.user_updated'
90 return $this->redirect($this->generateUrl('config') . '#set3');
93 // handle rss information
94 $rssForm = $this->createForm(RssType
::class, $config, ['action' => $this->generateUrl('config') . '#set2']);
95 $rssForm->handleRequest($request);
97 if ($rssForm->isSubmitted() && $rssForm->isValid()) {
98 $em->persist($config);
101 $this->get('session')->getFlashBag()->add(
103 'flashes.config.notice.rss_updated'
106 return $this->redirect($this->generateUrl('config') . '#set2');
109 // handle tagging rule
110 $taggingRule = new TaggingRule();
111 $action = $this->generateUrl('config') . '#set5';
113 if ($request->query
->has('tagging-rule')) {
114 $taggingRule = $this->getDoctrine()
115 ->getRepository('WallabagCoreBundle:TaggingRule')
116 ->find($request->query
->get('tagging-rule'));
118 if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
119 return $this->redirect($action);
122 $action = $this->generateUrl('config') . '?tagging-rule=' . $taggingRule->getId() . '#set5';
125 $newTaggingRule = $this->createForm(TaggingRuleType
::class, $taggingRule, ['action' => $action]);
126 $newTaggingRule->handleRequest($request);
128 if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) {
129 $taggingRule->setConfig($config);
130 $em->persist($taggingRule);
133 $this->get('session')->getFlashBag()->add(
135 'flashes.config.notice.tagging_rules_updated'
138 return $this->redirect($this->generateUrl('config') . '#set5');
141 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
143 'config' => $configForm->createView(),
144 'rss' => $rssForm->createView(),
145 'pwd' => $pwdForm->createView(),
146 'user' => $userForm->createView(),
147 'new_tagging_rule' => $newTaggingRule->createView(),
150 'username' => $user->getUsername(),
151 'token' => $config->getRssToken(),
153 'twofactor_auth' => $this->getParameter('twofactor_auth'),
154 'wallabag_url' => $this->getParameter('domain_name'),
155 'enabled_users' => $this->get('wallabag_user.user_repository')
156 ->getSumEnabledUsers(),
161 * @param Request $request
163 * @Route("/generate-token", name="generate_token")
165 * @return RedirectResponse|JsonResponse
167 public function generateTokenAction(Request
$request)
169 $config = $this->getConfig();
170 $config->setRssToken(Utils
::generateToken());
172 $em = $this->getDoctrine()->getManager();
173 $em->persist($config);
176 if ($request->isXmlHttpRequest()) {
177 return new JsonResponse(['token' => $config->getRssToken()]);
180 $this->get('session')->getFlashBag()->add(
182 'flashes.config.notice.rss_token_updated'
185 return $this->redirect($this->generateUrl('config') . '#set2');
189 * Deletes a tagging rule and redirect to the config homepage.
191 * @param TaggingRule $rule
193 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
195 * @return RedirectResponse
197 public function deleteTaggingRuleAction(TaggingRule
$rule)
199 $this->validateRuleAction($rule);
201 $em = $this->getDoctrine()->getManager();
205 $this->get('session')->getFlashBag()->add(
207 'flashes.config.notice.tagging_rules_deleted'
210 return $this->redirect($this->generateUrl('config') . '#set5');
214 * Edit a tagging rule.
216 * @param TaggingRule $rule
218 * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
220 * @return RedirectResponse
222 public function editTaggingRuleAction(TaggingRule
$rule)
224 $this->validateRuleAction($rule);
226 return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5');
230 * Remove all annotations OR tags OR entries for the current user.
232 * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset")
234 * @return RedirectResponse
236 public function resetAction($type)
241 ->getRepository('WallabagAnnotationBundle:Annotation')
242 ->removeAllByUserId($this->getUser()->getId());
245 $this->removeAllTagsByUserId($this->getUser()->getId());
248 // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff
249 // otherwise they won't be removed ...
250 if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
) {
251 $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId());
254 // manually remove tags to avoid orphan tag
255 $this->removeAllTagsByUserId($this->getUser()->getId());
257 $this->get('wallabag_core.entry_repository')->removeAllByUserId($this->getUser()->getId());
260 if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
) {
261 $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId());
264 // manually remove tags to avoid orphan tag
265 $this->removeTagsForArchivedByUserId($this->getUser()->getId());
267 $this->get('wallabag_core.entry_repository')->removeArchivedByUserId($this->getUser()->getId());
271 $this->get('session')->getFlashBag()->add(
273 'flashes.config.notice.' . $type . '_reset'
276 return $this->redirect($this->generateUrl('config') . '#set3');
280 * Delete account for current user.
282 * @Route("/account/delete", name="delete_account")
284 * @param Request $request
286 * @throws AccessDeniedHttpException
288 * @return \Symfony\Component\HttpFoundation\RedirectResponse
290 public function deleteAccountAction(Request
$request)
292 $enabledUsers = $this->get('wallabag_user.user_repository')
293 ->getSumEnabledUsers();
295 if ($enabledUsers <= 1) {
296 throw new AccessDeniedHttpException();
299 $user = $this->getUser();
301 // logout current user
302 $this->get('security.token_storage')->setToken(null);
303 $request->getSession()->invalidate();
305 $em = $this->get('fos_user.user_manager');
306 $em->deleteUser($user);
308 return $this->redirect($this->generateUrl('fos_user_security_login'));
312 * Switch view mode for current user.
314 * @Route("/config/view-mode", name="switch_view_mode")
316 * @param Request $request
318 * @return \Symfony\Component\HttpFoundation\RedirectResponse
320 public function changeViewModeAction(Request
$request)
322 $user = $this->getUser();
323 $user->getConfig()->setListMode(!$user->getConfig()->getListMode());
325 $em = $this->getDoctrine()->getManager();
329 return $this->redirect($request->headers
->get('referer'));
333 * Remove all tags for given tags and a given user and cleanup orphan tags.
338 private function removeAllTagsByStatusAndUserId($tags, $userId)
344 $this->get('wallabag_core.entry_repository')
345 ->removeTags($userId, $tags);
347 // cleanup orphan tags
348 $em = $this->getDoctrine()->getManager();
350 foreach ($tags as $tag) {
351 if (count($tag->getEntries()) === 0) {
360 * Remove all tags for a given user and cleanup orphan tags.
364 private function removeAllTagsByUserId($userId)
366 $tags = $this->get('wallabag_core.tag_repository')->findAllTags($userId);
367 $this->removeAllTagsByStatusAndUserId($tags, $userId);
371 * Remove all tags for a given user and cleanup orphan tags.
375 private function removeTagsForArchivedByUserId($userId)
377 $tags = $this->get('wallabag_core.tag_repository')->findForArchivedArticlesByUser($userId);
378 $this->removeAllTagsByStatusAndUserId($tags, $userId);
381 private function removeAnnotationsForArchivedByUserId($userId)
383 $em = $this->getDoctrine()->getManager();
385 $archivedEntriesAnnotations = $this->getDoctrine()
386 ->getRepository('WallabagAnnotationBundle:Annotation')
387 ->findAllArchivedEntriesByUser($userId);
389 foreach ($archivedEntriesAnnotations as $archivedEntriesAnnotation) {
390 $em->remove($archivedEntriesAnnotation);
397 * Validate that a rule can be edited/deleted by the current user.
399 * @param TaggingRule $rule
401 private function validateRuleAction(TaggingRule
$rule)
403 if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) {
404 throw $this->createAccessDeniedException('You can not access this tagging rule.');
409 * Retrieve config for the current user.
410 * If no config were found, create a new one.
414 private function getConfig()
416 $config = $this->getDoctrine()
417 ->getRepository('WallabagCoreBundle:Config')
418 ->findOneByUser($this->getUser());
420 // should NEVER HAPPEN ...
422 $config = new Config($this->getUser());