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 $request->getSession()->set('_locale', $config->getLanguage());
43 // switch active theme
44 $activeTheme = $this->get('liip_theme.active_theme');
45 $activeTheme->setName($config->getTheme());
47 $this->get('session')->getFlashBag()->add(
49 'flashes.config.notice.config_saved'
52 return $this->redirect($this->generateUrl('config'));
55 // handle changing password
56 $pwdForm = $this->createForm(ChangePasswordType
::class, null, ['action' => $this->generateUrl('config').'#set4']);
57 $pwdForm->handleRequest($request);
59 if ($pwdForm->isValid()) {
60 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
61 $message = 'flashes.config.notice.password_not_updated_demo';
63 $message = 'flashes.config.notice.password_updated';
65 $user->setPlainPassword($pwdForm->get('new_password')->getData());
66 $userManager->updateUser($user, true);
69 $this->get('session')->getFlashBag()->add('notice', $message);
71 return $this->redirect($this->generateUrl('config').'#set4');
74 // handle changing user information
75 $userForm = $this->createForm(UserInformationType
::class, $user, [
76 'validation_groups' => ['Profile'],
77 'action' => $this->generateUrl('config').'#set3',
79 $userForm->handleRequest($request);
81 if ($userForm->isValid()) {
82 $userManager->updateUser($user, true);
84 $this->get('session')->getFlashBag()->add(
86 'flashes.config.notice.user_updated'
89 return $this->redirect($this->generateUrl('config').'#set3');
92 // handle rss information
93 $rssForm = $this->createForm(RssType
::class, $config, ['action' => $this->generateUrl('config').'#set2']);
94 $rssForm->handleRequest($request);
96 if ($rssForm->isValid()) {
97 $em->persist($config);
100 $this->get('session')->getFlashBag()->add(
102 'flashes.config.notice.rss_updated'
105 return $this->redirect($this->generateUrl('config').'#set2');
108 // handle tagging rule
109 $taggingRule = new TaggingRule();
110 $action = $this->generateUrl('config').'#set5';
112 if ($request->query
->has('tagging-rule')) {
113 $taggingRule = $this->getDoctrine()
114 ->getRepository('WallabagCoreBundle:TaggingRule')
115 ->find($request->query
->get('tagging-rule'));
117 if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
118 return $this->redirect($action);
121 $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5';
124 $newTaggingRule = $this->createForm(TaggingRuleType
::class, $taggingRule, ['action' => $action]);
125 $newTaggingRule->handleRequest($request);
127 if ($newTaggingRule->isValid()) {
128 $taggingRule->setConfig($config);
129 $em->persist($taggingRule);
132 $this->get('session')->getFlashBag()->add(
134 'flashes.config.notice.tagging_rules_updated'
137 return $this->redirect($this->generateUrl('config').'#set5');
140 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
142 'config' => $configForm->createView(),
143 'rss' => $rssForm->createView(),
144 'pwd' => $pwdForm->createView(),
145 'user' => $userForm->createView(),
146 'new_tagging_rule' => $newTaggingRule->createView(),
149 'username' => $user->getUsername(),
150 'token' => $config->getRssToken(),
152 'twofactor_auth' => $this->getParameter('twofactor_auth'),
157 * @param Request $request
159 * @Route("/generate-token", name="generate_token")
161 * @return RedirectResponse|JsonResponse
163 public function generateTokenAction(Request
$request)
165 $config = $this->getConfig();
166 $config->setRssToken(Utils
::generateToken());
168 $em = $this->getDoctrine()->getManager();
169 $em->persist($config);
172 if ($request->isXmlHttpRequest()) {
173 return new JsonResponse(['token' => $config->getRssToken()]);
176 $this->get('session')->getFlashBag()->add(
178 'flashes.config.notice.rss_token_updated'
181 return $this->redirect($this->generateUrl('config').'#set2');
185 * Deletes a tagging rule and redirect to the config homepage.
187 * @param TaggingRule $rule
189 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
191 * @return RedirectResponse
193 public function deleteTaggingRuleAction(TaggingRule
$rule)
195 $this->validateRuleAction($rule);
197 $em = $this->getDoctrine()->getManager();
201 $this->get('session')->getFlashBag()->add(
203 'flashes.config.notice.tagging_rules_deleted'
206 return $this->redirect($this->generateUrl('config').'#set5');
210 * Edit a tagging rule.
212 * @param TaggingRule $rule
214 * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
216 * @return RedirectResponse
218 public function editTaggingRuleAction(TaggingRule
$rule)
220 $this->validateRuleAction($rule);
222 return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5');
226 * Validate that a rule can be edited/deleted by the current user.
228 * @param TaggingRule $rule
230 private function validateRuleAction(TaggingRule
$rule)
232 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
233 throw $this->createAccessDeniedException('You can not access this tagging rule.');
238 * Retrieve config for the current user.
239 * If no config were found, create a new one.
243 private function getConfig()
245 $config = $this->getDoctrine()
246 ->getRepository('WallabagCoreBundle:Config')
247 ->findOneByUser($this->getUser());
249 // should NEVER HAPPEN ...
251 $config = new Config($this->getUser());