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\ConfigType
;
14 use Wallabag\CoreBundle\Form\Type\ChangePasswordType
;
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->isValid()) {
39 $em->persist($config);
42 // switch active theme
43 $activeTheme = $this->get('liip_theme.active_theme');
44 $activeTheme->setName($config->getTheme());
46 $this->get('session')->getFlashBag()->add(
48 'flashes.config.notice.config_saved'
51 return $this->redirect($this->generateUrl('config'));
54 // handle changing password
55 $pwdForm = $this->createForm(ChangePasswordType
::class, null, ['action' => $this->generateUrl('config').'#set4']);
56 $pwdForm->handleRequest($request);
58 if ($pwdForm->isValid()) {
59 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
60 $message = 'flashes.config.notice.password_not_updated_demo';
62 $message = 'flashes.config.notice.password_updated';
64 $user->setPlainPassword($pwdForm->get('new_password')->getData());
65 $userManager->updateUser($user, true);
68 $this->get('session')->getFlashBag()->add('notice', $message);
70 return $this->redirect($this->generateUrl('config').'#set4');
73 // handle changing user information
74 $userForm = $this->createForm(UserInformationType
::class, $user, [
75 'validation_groups' => ['Profile'],
76 'action' => $this->generateUrl('config').'#set3',
78 $userForm->handleRequest($request);
80 if ($userForm->isValid()) {
81 $userManager->updateUser($user, true);
83 $this->get('session')->getFlashBag()->add(
85 'flashes.config.notice.user_updated'
88 return $this->redirect($this->generateUrl('config').'#set3');
91 // handle rss information
92 $rssForm = $this->createForm(RssType
::class, $config, ['action' => $this->generateUrl('config').'#set2']);
93 $rssForm->handleRequest($request);
95 if ($rssForm->isValid()) {
96 $em->persist($config);
99 $this->get('session')->getFlashBag()->add(
101 'flashes.config.notice.rss_updated'
104 return $this->redirect($this->generateUrl('config').'#set2');
107 // handle tagging rule
108 $taggingRule = new TaggingRule();
109 $action = $this->generateUrl('config').'#set5';
111 if ($request->query
->has('tagging-rule')) {
112 $taggingRule = $this->getDoctrine()
113 ->getRepository('WallabagCoreBundle:TaggingRule')
114 ->find($request->query
->get('tagging-rule'));
116 if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
117 return $this->redirect($action);
120 $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5';
123 $newTaggingRule = $this->createForm(TaggingRuleType
::class, $taggingRule, ['action' => $action]);
124 $newTaggingRule->handleRequest($request);
126 if ($newTaggingRule->isValid()) {
127 $taggingRule->setConfig($config);
128 $em->persist($taggingRule);
131 $this->get('session')->getFlashBag()->add(
133 'flashes.config.notice.tagging_rules_updated'
136 return $this->redirect($this->generateUrl('config').'#set5');
139 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
141 'config' => $configForm->createView(),
142 'rss' => $rssForm->createView(),
143 'pwd' => $pwdForm->createView(),
144 'user' => $userForm->createView(),
145 'new_tagging_rule' => $newTaggingRule->createView(),
148 'username' => $user->getUsername(),
149 'token' => $config->getRssToken(),
151 'twofactor_auth' => $this->getParameter('twofactor_auth'),
152 'wallabag_url' => $this->get('craue_config')->get('wallabag_url'),
153 'enabled_users' => $this->getDoctrine()
154 ->getRepository('WallabagUserBundle:User')
155 ->getSumEnabledUsers(),
160 * @param Request $request
162 * @Route("/generate-token", name="generate_token")
164 * @return RedirectResponse|JsonResponse
166 public function generateTokenAction(Request
$request)
168 $config = $this->getConfig();
169 $config->setRssToken(Utils
::generateToken());
171 $em = $this->getDoctrine()->getManager();
172 $em->persist($config);
175 if ($request->isXmlHttpRequest()) {
176 return new JsonResponse(['token' => $config->getRssToken()]);
179 $this->get('session')->getFlashBag()->add(
181 'flashes.config.notice.rss_token_updated'
184 return $this->redirect($this->generateUrl('config').'#set2');
188 * Deletes a tagging rule and redirect to the config homepage.
190 * @param TaggingRule $rule
192 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
194 * @return RedirectResponse
196 public function deleteTaggingRuleAction(TaggingRule
$rule)
198 $this->validateRuleAction($rule);
200 $em = $this->getDoctrine()->getManager();
204 $this->get('session')->getFlashBag()->add(
206 'flashes.config.notice.tagging_rules_deleted'
209 return $this->redirect($this->generateUrl('config').'#set5');
213 * Edit a tagging rule.
215 * @param TaggingRule $rule
217 * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
219 * @return RedirectResponse
221 public function editTaggingRuleAction(TaggingRule
$rule)
223 $this->validateRuleAction($rule);
225 return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5');
229 * Remove all annotations OR tags OR entries for the current user.
231 * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset")
233 * @return RedirectResponse
235 public function resetAction($type)
237 $em = $this->getDoctrine()->getManager();
242 ->getRepository('WallabagAnnotationBundle:Annotation')
243 ->removeAllByUserId($this->getUser()->getId());
247 $this->removeAllTagsByUserId($this->getUser()->getId());
251 // SQLite doesn't care about cascading remove, so we need to manually remove associated stuf
252 // otherwise they won't be removed ...
253 if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver
) {
254 $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId());
257 // manually remove tags to avoid orphan tag
258 $this->removeAllTagsByUserId($this->getUser()->getId());
261 ->getRepository('WallabagCoreBundle:Entry')
262 ->removeAllByUserId($this->getUser()->getId());
265 $this->get('session')->getFlashBag()->add(
267 'flashes.config.notice.'.$type.'_reset'
270 return $this->redirect($this->generateUrl('config').'#set3');
274 * Remove all tags for a given user and cleanup orphan tags.
278 private function removeAllTagsByUserId($userId)
280 $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findAllTags($userId);
287 ->getRepository('WallabagCoreBundle:Entry')
288 ->removeTags($userId, $tags);
290 // cleanup orphan tags
291 $em = $this->getDoctrine()->getManager();
293 foreach ($tags as $tag) {
294 if (count($tag->getEntries()) === 0) {
303 * Validate that a rule can be edited/deleted by the current user.
305 * @param TaggingRule $rule
307 private function validateRuleAction(TaggingRule
$rule)
309 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
310 throw $this->createAccessDeniedException('You can not access this tagging rule.');
315 * Retrieve config for the current user.
316 * If no config were found, create a new one.
320 private function getConfig()
322 $config = $this->getDoctrine()
323 ->getRepository('WallabagCoreBundle:Config')
324 ->findOneByUser($this->getUser());
326 // should NEVER HAPPEN ...
328 $config = new Config($this->getUser());
335 * Delete account for current user.
337 * @Route("/account/delete", name="delete_account")
339 * @param Request $request
341 * @throws AccessDeniedHttpException
343 * @return \Symfony\Component\HttpFoundation\RedirectResponse
345 public function deleteAccountAction(Request
$request)
347 $enabledUsers = $this->getDoctrine()
348 ->getRepository('WallabagUserBundle:User')
349 ->getSumEnabledUsers();
351 if ($enabledUsers <= 1) {
352 throw new AccessDeniedHttpException();
355 $user = $this->getUser();
357 // logout current user
358 $this->get('security.token_storage')->setToken(null);
359 $request->getSession()->invalidate();
361 $em = $this->get('fos_user.user_manager');
362 $em->deleteUser($user);
364 return $this->redirect($this->generateUrl('fos_user_security_login'));