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\Request
;
9 use Wallabag\CoreBundle\Entity\Config
;
10 use Wallabag\CoreBundle\Entity\TaggingRule
;
11 use Wallabag\CoreBundle\Form\Type\ConfigType
;
12 use Wallabag\CoreBundle\Form\Type\ChangePasswordType
;
13 use Wallabag\CoreBundle\Form\Type\NewUserType
;
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
;
18 use Wallabag\UserBundle\Entity\User
;
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, array('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 'Config saved. Some parameters will be considered after disconnection.'
51 return $this->redirect($this->generateUrl('config'));
54 // handle changing password
55 $pwdForm = $this->createForm(ChangePasswordType
::class, null, array('action' => $this->generateUrl('config').'#set4'));
56 $pwdForm->handleRequest($request);
58 if ($pwdForm->isValid()) {
59 $user->setPlainPassword($pwdForm->get('new_password')->getData());
60 $userManager->updateUser($user, true);
62 $this->get('session')->getFlashBag()->add(
67 return $this->redirect($this->generateUrl('config'));
70 // handle changing user information
71 $userForm = $this->createForm(UserInformationType
::class, $user, array(
72 'validation_groups' => array('Profile'),
73 'action' => $this->generateUrl('config').'#set3',
75 $userForm->handleRequest($request);
77 if ($userForm->isValid()) {
78 $userManager->updateUser($user, true);
80 $this->get('session')->getFlashBag()->add(
85 return $this->redirect($this->generateUrl('config'));
88 // handle rss information
89 $rssForm = $this->createForm(RssType
::class, $config, array('action' => $this->generateUrl('config').'#set2'));
90 $rssForm->handleRequest($request);
92 if ($rssForm->isValid()) {
93 $em->persist($config);
96 $this->get('session')->getFlashBag()->add(
98 'RSS information updated'
101 return $this->redirect($this->generateUrl('config'));
104 // handle tagging rule
105 $taggingRule = new TaggingRule();
106 $newTaggingRule = $this->createForm(TaggingRuleType
::class, $taggingRule, array('action' => $this->generateUrl('config').'#set5'));
107 $newTaggingRule->handleRequest($request);
109 if ($newTaggingRule->isValid()) {
110 $taggingRule->setConfig($config);
111 $em->persist($taggingRule);
114 $this->get('session')->getFlashBag()->add(
116 'Tagging rules updated'
119 return $this->redirect($this->generateUrl('config'));
122 // handle adding new user
123 $newUser = $userManager->createUser();
124 // enable created user by default
125 $newUser->setEnabled(true);
126 $newUserForm = $this->createForm(NewUserType
::class, $newUser, array(
127 'validation_groups' => array('Profile'),
128 'action' => $this->generateUrl('config').'#set5',
130 $newUserForm->handleRequest($request);
132 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
133 $userManager->updateUser($newUser, true);
135 $config = new Config($newUser);
136 $config->setTheme($this->container
->getParameter('theme'));
137 $config->setItemsPerPage($this->container
->getParameter('items_on_page'));
138 $config->setRssLimit($this->container
->getParameter('rss_limit'));
139 $config->setLanguage($this->container
->getParameter('language'));
141 $em->persist($config);
145 $this->get('session')->getFlashBag()->add(
147 sprintf('User "%s" added', $newUser->getUsername())
150 return $this->redirect($this->generateUrl('config'));
153 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
155 'config' => $configForm->createView(),
156 'rss' => $rssForm->createView(),
157 'pwd' => $pwdForm->createView(),
158 'user' => $userForm->createView(),
159 'new_user' => $newUserForm->createView(),
160 'new_tagging_rule' => $newTaggingRule->createView(),
163 'username' => $user->getUsername(),
164 'token' => $config->getRssToken(),
170 * @param Request $request
172 * @Route("/generate-token", name="generate_token")
174 * @return JsonResponse
176 public function generateTokenAction(Request
$request)
178 $config = $this->getConfig();
179 $config->setRssToken(Utils
::generateToken());
181 $em = $this->getDoctrine()->getManager();
182 $em->persist($config);
185 if ($request->isXmlHttpRequest()) {
186 return new JsonResponse(array('token' => $config->getRssToken()));
189 return $request->headers
->get('referer') ? $this->redirect($request->headers
->get('referer')) : $this->redirectToRoute('config');
193 * Deletes a tagging rule and redirect to the config homepage.
195 * @param TaggingRule $rule
197 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
199 * @return \Symfony\Component\HttpFoundation\RedirectResponse
201 public function deleteTaggingRule(TaggingRule
$rule)
203 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
204 throw $this->createAccessDeniedException('You can not access this tagging ryle.');
207 $em = $this->getDoctrine()->getManager();
211 $this->get('session')->getFlashBag()->add(
213 'Tagging rule deleted'
216 return $this->redirect($this->generateUrl('config'));
220 * Retrieve config for the current user.
221 * If no config were found, create a new one.
223 * @return Wallabag\CoreBundle\Entity\Config
225 private function getConfig()
227 $config = $this->getDoctrine()
228 ->getRepository('WallabagCoreBundle:Config')
229 ->findOneByUser($this->getUser());
232 $config = new Config($this->getUser());