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\NewUserType
;
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 $newTaggingRule = $this->createForm(TaggingRuleType
::class, $taggingRule, ['action' => $this->generateUrl('config').'#set5']);
110 $newTaggingRule->handleRequest($request);
112 if ($newTaggingRule->isValid()) {
113 $taggingRule->setConfig($config);
114 $em->persist($taggingRule);
117 $this->get('session')->getFlashBag()->add(
119 'flashes.config.notice.tagging_rules_updated'
122 return $this->redirect($this->generateUrl('config').'#set5');
125 // handle adding new user
126 $newUser = $userManager->createUser();
127 // enable created user by default
128 $newUser->setEnabled(true);
129 $newUserForm = $this->createForm(NewUserType
::class, $newUser, [
130 'validation_groups' => ['Profile'],
131 'action' => $this->generateUrl('config').'#set6',
133 $newUserForm->handleRequest($request);
135 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
136 $userManager->updateUser($newUser, true);
138 $config = new Config($newUser);
139 $config->setTheme($this->getParameter('wallabag_core.theme'));
140 $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page'));
141 $config->setRssLimit($this->getParameter('wallabag_core.rss_limit'));
142 $config->setLanguage($this->getParameter('wallabag_core.language'));
143 $config->setReadingSpeed($this->getParameter('wallabag_core.reading_speed'));
145 $em->persist($config);
149 $this->get('session')->getFlashBag()->add(
151 $this->get('translator')->trans('flashes.config.notice.user_added', ['%username%' => $newUser->getUsername()])
154 return $this->redirect($this->generateUrl('config').'#set6');
157 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
159 'config' => $configForm->createView(),
160 'rss' => $rssForm->createView(),
161 'pwd' => $pwdForm->createView(),
162 'user' => $userForm->createView(),
163 'new_user' => $newUserForm->createView(),
164 'new_tagging_rule' => $newTaggingRule->createView(),
167 'username' => $user->getUsername(),
168 'token' => $config->getRssToken(),
170 'twofactor_auth' => $this->getParameter('twofactor_auth'),
175 * @param Request $request
177 * @Route("/generate-token", name="generate_token")
179 * @return RedirectResponse|JsonResponse
181 public function generateTokenAction(Request
$request)
183 $config = $this->getConfig();
184 $config->setRssToken(Utils
::generateToken());
186 $em = $this->getDoctrine()->getManager();
187 $em->persist($config);
190 if ($request->isXmlHttpRequest()) {
191 return new JsonResponse(['token' => $config->getRssToken()]);
194 $this->get('session')->getFlashBag()->add(
196 'flashes.config.notice.rss_token_updated'
199 return $this->redirect($this->generateUrl('config').'#set2');
203 * Deletes a tagging rule and redirect to the config homepage.
205 * @param TaggingRule $rule
207 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
209 * @return RedirectResponse
211 public function deleteTaggingRuleAction(TaggingRule
$rule)
213 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
214 throw $this->createAccessDeniedException('You can not access this tagging rule.');
217 $em = $this->getDoctrine()->getManager();
221 $this->get('session')->getFlashBag()->add(
223 'flashes.config.notice.tagging_rules_deleted'
226 return $this->redirect($this->generateUrl('config').'#set5');
230 * Retrieve config for the current user.
231 * If no config were found, create a new one.
235 private function getConfig()
237 $config = $this->getDoctrine()
238 ->getRepository('WallabagCoreBundle:Config')
239 ->findOneByUser($this->getUser());
242 $config = new Config($this->getUser());