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 $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->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->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->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->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->get('craue_config')->get('wallabag_url'),
155 'enabled_users' => $this->getDoctrine()
156 ->getRepository('WallabagUserBundle:User')
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());
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'));