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 Wallabag\CoreBundle\Entity\Config
;
11 use Wallabag\CoreBundle\Entity\TaggingRule
;
12 use Wallabag\CoreBundle\Form\Type\ConfigType
;
13 use Wallabag\CoreBundle\Form\Type\ChangePasswordType
;
14 use Wallabag\CoreBundle\Form\Type\RssType
;
15 use Wallabag\CoreBundle\Form\Type\TaggingRuleType
;
16 use Wallabag\CoreBundle\Form\Type\UserInformationType
;
17 use Wallabag\CoreBundle\Tools\Utils
;
19 class ConfigController
extends Controller
22 * @param Request $request
24 * @Route("/config", name="config")
26 public function indexAction(Request
$request)
28 $em = $this->getDoctrine()->getManager();
29 $config = $this->getConfig();
30 $userManager = $this->container
->get('fos_user.user_manager');
31 $user = $this->getUser();
33 // handle basic config detail (this form is defined as a service)
34 $configForm = $this->createForm(ConfigType
::class, $config, ['action' => $this->generateUrl('config')]);
35 $configForm->handleRequest($request);
37 if ($configForm->isValid()) {
38 $em->persist($config);
41 // switch active theme
42 $activeTheme = $this->get('liip_theme.active_theme');
43 $activeTheme->setName($config->getTheme());
45 $this->get('session')->getFlashBag()->add(
47 'flashes.config.notice.config_saved'
50 return $this->redirect($this->generateUrl('config'));
53 // handle changing password
54 $pwdForm = $this->createForm(ChangePasswordType
::class, null, ['action' => $this->generateUrl('config').'#set4']);
55 $pwdForm->handleRequest($request);
57 if ($pwdForm->isValid()) {
58 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
59 $message = 'flashes.config.notice.password_not_updated_demo';
61 $message = 'flashes.config.notice.password_updated';
63 $user->setPlainPassword($pwdForm->get('new_password')->getData());
64 $userManager->updateUser($user, true);
67 $this->get('session')->getFlashBag()->add('notice', $message);
69 return $this->redirect($this->generateUrl('config').'#set4');
72 // handle changing user information
73 $userForm = $this->createForm(UserInformationType
::class, $user, [
74 'validation_groups' => ['Profile'],
75 'action' => $this->generateUrl('config').'#set3',
77 $userForm->handleRequest($request);
79 if ($userForm->isValid()) {
80 $userManager->updateUser($user, true);
82 $this->get('session')->getFlashBag()->add(
84 'flashes.config.notice.user_updated'
87 return $this->redirect($this->generateUrl('config').'#set3');
90 // handle rss information
91 $rssForm = $this->createForm(RssType
::class, $config, ['action' => $this->generateUrl('config').'#set2']);
92 $rssForm->handleRequest($request);
94 if ($rssForm->isValid()) {
95 $em->persist($config);
98 $this->get('session')->getFlashBag()->add(
100 'flashes.config.notice.rss_updated'
103 return $this->redirect($this->generateUrl('config').'#set2');
106 // handle tagging rule
107 $taggingRule = new TaggingRule();
108 $action = $this->generateUrl('config').'#set5';
110 if ($request->query
->has('tagging-rule')) {
111 $taggingRule = $this->getDoctrine()
112 ->getRepository('WallabagCoreBundle:TaggingRule')
113 ->find($request->query
->get('tagging-rule'));
115 if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
116 return $this->redirect($action);
119 $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5';
122 $newTaggingRule = $this->createForm(TaggingRuleType
::class, $taggingRule, ['action' => $action]);
123 $newTaggingRule->handleRequest($request);
125 if ($newTaggingRule->isValid()) {
126 $taggingRule->setConfig($config);
127 $em->persist($taggingRule);
130 $this->get('session')->getFlashBag()->add(
132 'flashes.config.notice.tagging_rules_updated'
135 return $this->redirect($this->generateUrl('config').'#set5');
138 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
140 'config' => $configForm->createView(),
141 'rss' => $rssForm->createView(),
142 'pwd' => $pwdForm->createView(),
143 'user' => $userForm->createView(),
144 'new_tagging_rule' => $newTaggingRule->createView(),
147 'username' => $user->getUsername(),
148 'token' => $config->getRssToken(),
150 'twofactor_auth' => $this->getParameter('twofactor_auth'),
155 * @param Request $request
157 * @Route("/generate-token", name="generate_token")
159 * @return RedirectResponse|JsonResponse
161 public function generateTokenAction(Request
$request)
163 $config = $this->getConfig();
164 $config->setRssToken(Utils
::generateToken());
166 $em = $this->getDoctrine()->getManager();
167 $em->persist($config);
170 if ($request->isXmlHttpRequest()) {
171 return new JsonResponse(['token' => $config->getRssToken()]);
174 $this->get('session')->getFlashBag()->add(
176 'flashes.config.notice.rss_token_updated'
179 return $this->redirect($this->generateUrl('config').'#set2');
183 * Deletes a tagging rule and redirect to the config homepage.
185 * @param TaggingRule $rule
187 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
189 * @return RedirectResponse
191 public function deleteTaggingRuleAction(TaggingRule
$rule)
193 $this->validateRuleAction($rule);
195 $em = $this->getDoctrine()->getManager();
199 $this->get('session')->getFlashBag()->add(
201 'flashes.config.notice.tagging_rules_deleted'
204 return $this->redirect($this->generateUrl('config').'#set5');
208 * Edit a tagging rule.
210 * @param TaggingRule $rule
212 * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
214 * @return RedirectResponse
216 public function editTaggingRuleAction(TaggingRule
$rule)
218 $this->validateRuleAction($rule);
220 return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5');
224 * Validate that a rule can be edited/deleted by the current user.
226 * @param TaggingRule $rule
228 private function validateRuleAction(TaggingRule
$rule)
230 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
231 throw $this->createAccessDeniedException('You can not access this tagging rule.');
236 * Retrieve config for the current user.
237 * If no config were found, create a new one.
241 private function getConfig()
243 $config = $this->getDoctrine()
244 ->getRepository('WallabagCoreBundle:Config')
245 ->findOneByUser($this->getUser());
247 // should NEVER HAPPEN ...
249 $config = new Config($this->getUser());