]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
Merge pull request #1642 from wallabag/v2-escape-preview
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ConfigController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
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;
19
20 class ConfigController extends Controller
21 {
22 /**
23 * @param Request $request
24 *
25 * @Route("/config", name="config")
26 */
27 public function indexAction(Request $request)
28 {
29 $em = $this->getDoctrine()->getManager();
30 $config = $this->getConfig();
31 $userManager = $this->container->get('fos_user.user_manager');
32 $user = $this->getUser();
33
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);
37
38 if ($configForm->isValid()) {
39 $em->persist($config);
40 $em->flush();
41
42 // switch active theme
43 $activeTheme = $this->get('liip_theme.active_theme');
44 $activeTheme->setName($config->getTheme());
45
46 $this->get('session')->getFlashBag()->add(
47 'notice',
48 'Config saved. Some parameters will be considered after disconnection.'
49 );
50
51 return $this->redirect($this->generateUrl('config'));
52 }
53
54 // handle changing password
55 $pwdForm = $this->createForm(ChangePasswordType::class, null, array('action' => $this->generateUrl('config').'#set4'));
56 $pwdForm->handleRequest($request);
57
58 if ($pwdForm->isValid()) {
59 $user->setPlainPassword($pwdForm->get('new_password')->getData());
60 $userManager->updateUser($user, true);
61
62 $this->get('session')->getFlashBag()->add(
63 'notice',
64 'Password updated'
65 );
66
67 return $this->redirect($this->generateUrl('config'));
68 }
69
70 // handle changing user information
71 $userForm = $this->createForm(UserInformationType::class, $user, array(
72 'validation_groups' => array('Profile'),
73 'action' => $this->generateUrl('config').'#set3',
74 ));
75 $userForm->handleRequest($request);
76
77 if ($userForm->isValid()) {
78 $userManager->updateUser($user, true);
79
80 $this->get('session')->getFlashBag()->add(
81 'notice',
82 'Information updated'
83 );
84
85 return $this->redirect($this->generateUrl('config'));
86 }
87
88 // handle rss information
89 $rssForm = $this->createForm(RssType::class, $config, array('action' => $this->generateUrl('config').'#set2'));
90 $rssForm->handleRequest($request);
91
92 if ($rssForm->isValid()) {
93 $em->persist($config);
94 $em->flush();
95
96 $this->get('session')->getFlashBag()->add(
97 'notice',
98 'RSS information updated'
99 );
100
101 return $this->redirect($this->generateUrl('config'));
102 }
103
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);
108
109 if ($newTaggingRule->isValid()) {
110 $taggingRule->setConfig($config);
111 $em->persist($taggingRule);
112 $em->flush();
113
114 $this->get('session')->getFlashBag()->add(
115 'notice',
116 'Tagging rules updated'
117 );
118
119 return $this->redirect($this->generateUrl('config'));
120 }
121
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').'#set6',
129 ));
130 $newUserForm->handleRequest($request);
131
132 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
133 $userManager->updateUser($newUser, true);
134
135 $config = new Config($newUser);
136 $config->setTheme($this->getParameter('wallabag_core.theme'));
137 $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page'));
138 $config->setRssLimit($this->getParameter('wallabag_core.rss_limit'));
139 $config->setLanguage($this->getParameter('wallabag_core.language'));
140
141 $em->persist($config);
142
143 $em->flush();
144
145 $this->get('session')->getFlashBag()->add(
146 'notice',
147 sprintf('User "%s" added', $newUser->getUsername())
148 );
149
150 return $this->redirect($this->generateUrl('config'));
151 }
152
153 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
154 'form' => 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(),
161 ),
162 'rss' => array(
163 'username' => $user->getUsername(),
164 'token' => $config->getRssToken(),
165 ),
166 'twofactor_auth' => $this->getParameter('twofactor_auth'),
167 ));
168 }
169
170 /**
171 * @param Request $request
172 *
173 * @Route("/generate-token", name="generate_token")
174 *
175 * @return JsonResponse
176 */
177 public function generateTokenAction(Request $request)
178 {
179 $config = $this->getConfig();
180 $config->setRssToken(Utils::generateToken());
181
182 $em = $this->getDoctrine()->getManager();
183 $em->persist($config);
184 $em->flush();
185
186 if ($request->isXmlHttpRequest()) {
187 return new JsonResponse(array('token' => $config->getRssToken()));
188 }
189
190 return $request->headers->get('referer') ? $this->redirect($request->headers->get('referer')) : $this->redirectToRoute('config');
191 }
192
193 /**
194 * Deletes a tagging rule and redirect to the config homepage.
195 *
196 * @param TaggingRule $rule
197 *
198 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
199 *
200 * @return \Symfony\Component\HttpFoundation\RedirectResponse
201 */
202 public function deleteTaggingRuleAction(TaggingRule $rule)
203 {
204 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
205 throw $this->createAccessDeniedException('You can not access this tagging ryle.');
206 }
207
208 $em = $this->getDoctrine()->getManager();
209 $em->remove($rule);
210 $em->flush();
211
212 $this->get('session')->getFlashBag()->add(
213 'notice',
214 'Tagging rule deleted'
215 );
216
217 return $this->redirect($this->generateUrl('config'));
218 }
219
220 /**
221 * Retrieve config for the current user.
222 * If no config were found, create a new one.
223 *
224 * @return Wallabag\CoreBundle\Entity\Config
225 */
226 private function getConfig()
227 {
228 $config = $this->getDoctrine()
229 ->getRepository('WallabagCoreBundle:Config')
230 ->findOneByUser($this->getUser());
231
232 if (!$config) {
233 $config = new Config($this->getUser());
234 }
235
236 return $config;
237 }
238 }