]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ConfigController.php
Fix tests & deprecation notice
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ConfigController.php
CommitLineData
4d85d7e9
J
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
0c83fd59 7use Symfony\Component\HttpFoundation\JsonResponse;
98568055 8use Symfony\Component\HttpFoundation\RedirectResponse;
619cc453 9use Symfony\Component\HttpFoundation\Request;
bb0c78f4 10use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
4d85d7e9 11use Wallabag\CoreBundle\Entity\Config;
f19f9f62 12use Wallabag\CoreBundle\Entity\TaggingRule;
5c895a7f 13use Wallabag\CoreBundle\Form\Type\ConfigType;
d9085c63 14use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
0c83fd59 15use Wallabag\CoreBundle\Form\Type\RssType;
619cc453
JB
16use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
17use Wallabag\CoreBundle\Form\Type\UserInformationType;
0c83fd59 18use Wallabag\CoreBundle\Tools\Utils;
4d85d7e9
J
19
20class ConfigController extends Controller
21{
22 /**
23 * @param Request $request
24 *
25 * @Route("/config", name="config")
4d85d7e9
J
26 */
27 public function indexAction(Request $request)
28 {
d9085c63 29 $em = $this->getDoctrine()->getManager();
4d85d7e9 30 $config = $this->getConfig();
fcb1fba5 31 $userManager = $this->container->get('fos_user.user_manager');
c0d9eba0 32 $user = $this->getUser();
4d85d7e9 33
32da2a70 34 // handle basic config detail (this form is defined as a service)
4094ea47 35 $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]);
d9085c63 36 $configForm->handleRequest($request);
4d85d7e9 37
21e7ccef 38 if ($configForm->isSubmitted() && $configForm->isValid()) {
4d85d7e9
J
39 $em->persist($config);
40 $em->flush();
41
ece4718f
NL
42 $request->getSession()->set('_locale', $config->getLanguage());
43
32da2a70
J
44 // switch active theme
45 $activeTheme = $this->get('liip_theme.active_theme');
46 $activeTheme->setName($config->getTheme());
47
4d85d7e9
J
48 $this->get('session')->getFlashBag()->add(
49 'notice',
4204a06b 50 'flashes.config.notice.config_saved'
4d85d7e9
J
51 );
52
53 return $this->redirect($this->generateUrl('config'));
54 }
55
d9085c63 56 // handle changing password
4094ea47 57 $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']);
d9085c63
J
58 $pwdForm->handleRequest($request);
59
21e7ccef 60 if ($pwdForm->isSubmitted() && $pwdForm->isValid()) {
a4f42c59 61 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
4204a06b 62 $message = 'flashes.config.notice.password_not_updated_demo';
6c9f50a6 63 } else {
4204a06b 64 $message = 'flashes.config.notice.password_updated';
b6c00b0b 65
d8d56448
NL
66 $user->setPlainPassword($pwdForm->get('new_password')->getData());
67 $userManager->updateUser($user, true);
6c9f50a6 68 }
d9085c63 69
b6c00b0b
JB
70 $this->get('session')->getFlashBag()->add('notice', $message);
71
c7a4f74f 72 return $this->redirect($this->generateUrl('config').'#set4');
d9085c63
J
73 }
74
c0d9eba0 75 // handle changing user information
4094ea47
JB
76 $userForm = $this->createForm(UserInformationType::class, $user, [
77 'validation_groups' => ['Profile'],
33fe61f9 78 'action' => $this->generateUrl('config').'#set3',
4094ea47 79 ]);
c0d9eba0
J
80 $userForm->handleRequest($request);
81
21e7ccef 82 if ($userForm->isSubmitted() && $userForm->isValid()) {
fcb1fba5 83 $userManager->updateUser($user, true);
c0d9eba0
J
84
85 $this->get('session')->getFlashBag()->add(
86 'notice',
4204a06b 87 'flashes.config.notice.user_updated'
c0d9eba0
J
88 );
89
c7a4f74f 90 return $this->redirect($this->generateUrl('config').'#set3');
c0d9eba0
J
91 }
92
0c83fd59 93 // handle rss information
4094ea47 94 $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']);
0c83fd59
J
95 $rssForm->handleRequest($request);
96
21e7ccef 97 if ($rssForm->isSubmitted() && $rssForm->isValid()) {
0c83fd59
J
98 $em->persist($config);
99 $em->flush();
100
101 $this->get('session')->getFlashBag()->add(
102 'notice',
4204a06b 103 'flashes.config.notice.rss_updated'
0c83fd59
J
104 );
105
c7a4f74f 106 return $this->redirect($this->generateUrl('config').'#set2');
0c83fd59
J
107 }
108
f19f9f62
KG
109 // handle tagging rule
110 $taggingRule = new TaggingRule();
bf3dc999
JB
111 $action = $this->generateUrl('config').'#set5';
112
113 if ($request->query->has('tagging-rule')) {
114 $taggingRule = $this->getDoctrine()
115 ->getRepository('WallabagCoreBundle:TaggingRule')
116 ->find($request->query->get('tagging-rule'));
117
118 if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
119 return $this->redirect($action);
120 }
121
122 $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5';
123 }
124
125 $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]);
f19f9f62
KG
126 $newTaggingRule->handleRequest($request);
127
21e7ccef 128 if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) {
f19f9f62
KG
129 $taggingRule->setConfig($config);
130 $em->persist($taggingRule);
131 $em->flush();
132
133 $this->get('session')->getFlashBag()->add(
134 'notice',
4204a06b 135 'flashes.config.notice.tagging_rules_updated'
f19f9f62
KG
136 );
137
c7a4f74f 138 return $this->redirect($this->generateUrl('config').'#set5');
f19f9f62
KG
139 }
140
4094ea47
JB
141 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
142 'form' => [
0c83fd59
J
143 'config' => $configForm->createView(),
144 'rss' => $rssForm->createView(),
145 'pwd' => $pwdForm->createView(),
146 'user' => $userForm->createView(),
f19f9f62 147 'new_tagging_rule' => $newTaggingRule->createView(),
4094ea47
JB
148 ],
149 'rss' => [
0c83fd59
J
150 'username' => $user->getUsername(),
151 'token' => $config->getRssToken(),
4094ea47 152 ],
63e40f2d 153 'twofactor_auth' => $this->getParameter('twofactor_auth'),
e61ee560 154 'wallabag_url' => $this->get('craue_config')->get('wallabag_url'),
bb0c78f4
NL
155 'enabled_users' => $this->getDoctrine()
156 ->getRepository('WallabagUserBundle:User')
157 ->getSumEnabledUsers(),
4094ea47 158 ]);
4d85d7e9
J
159 }
160
0c83fd59
J
161 /**
162 * @param Request $request
163 *
164 * @Route("/generate-token", name="generate_token")
165 *
98568055 166 * @return RedirectResponse|JsonResponse
0c83fd59
J
167 */
168 public function generateTokenAction(Request $request)
169 {
170 $config = $this->getConfig();
171 $config->setRssToken(Utils::generateToken());
172
173 $em = $this->getDoctrine()->getManager();
174 $em->persist($config);
175 $em->flush();
176
177 if ($request->isXmlHttpRequest()) {
4094ea47 178 return new JsonResponse(['token' => $config->getRssToken()]);
0c83fd59
J
179 }
180
c7a4f74f
JB
181 $this->get('session')->getFlashBag()->add(
182 'notice',
4204a06b 183 'flashes.config.notice.rss_token_updated'
c7a4f74f
JB
184 );
185
186 return $this->redirect($this->generateUrl('config').'#set2');
0c83fd59
J
187 }
188
52e423f3
KG
189 /**
190 * Deletes a tagging rule and redirect to the config homepage.
191 *
192 * @param TaggingRule $rule
193 *
194 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
195 *
98568055 196 * @return RedirectResponse
52e423f3 197 */
5f8a7857 198 public function deleteTaggingRuleAction(TaggingRule $rule)
52e423f3 199 {
8799bde0 200 $this->validateRuleAction($rule);
52e423f3
KG
201
202 $em = $this->getDoctrine()->getManager();
203 $em->remove($rule);
204 $em->flush();
205
206 $this->get('session')->getFlashBag()->add(
207 'notice',
4204a06b 208 'flashes.config.notice.tagging_rules_deleted'
52e423f3
KG
209 );
210
c7a4f74f 211 return $this->redirect($this->generateUrl('config').'#set5');
52e423f3
KG
212 }
213
bf3dc999
JB
214 /**
215 * Edit a tagging rule.
216 *
217 * @param TaggingRule $rule
218 *
219 * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
220 *
221 * @return RedirectResponse
222 */
223 public function editTaggingRuleAction(TaggingRule $rule)
8799bde0
JB
224 {
225 $this->validateRuleAction($rule);
226
227 return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5');
228 }
229
206bade5
JB
230 /**
231 * Remove all annotations OR tags OR entries for the current user.
232 *
233 * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset")
234 *
235 * @return RedirectResponse
236 */
237 public function resetAction($type)
238 {
206bade5
JB
239 switch ($type) {
240 case 'annotations':
191564b7
JB
241 $this->getDoctrine()
242 ->getRepository('WallabagAnnotationBundle:Annotation')
243 ->removeAllByUserId($this->getUser()->getId());
206bade5
JB
244 break;
245
246 case 'tags':
191564b7
JB
247 $this->removeAllTagsByUserId($this->getUser()->getId());
248 break;
206bade5 249
191564b7
JB
250 case 'entries':
251 // SQLite doesn't care about cascading remove, so we need to manually remove associated stuf
252 // otherwise they won't be removed ...
253 if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
254 $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId());
206bade5
JB
255 }
256
8c61fd12 257 // manually remove tags to avoid orphan tag
f71e55ac
JB
258 $this->removeAllTagsByUserId($this->getUser()->getId());
259
206bade5
JB
260 $this->getDoctrine()
261 ->getRepository('WallabagCoreBundle:Entry')
191564b7 262 ->removeAllByUserId($this->getUser()->getId());
206bade5
JB
263 }
264
265 $this->get('session')->getFlashBag()->add(
266 'notice',
267 'flashes.config.notice.'.$type.'_reset'
268 );
269
270 return $this->redirect($this->generateUrl('config').'#set3');
271 }
272
191564b7 273 /**
8c61fd12 274 * Remove all tags for a given user and cleanup orphan tags.
191564b7 275 *
8c61fd12 276 * @param int $userId
191564b7
JB
277 */
278 private function removeAllTagsByUserId($userId)
279 {
280 $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findAllTags($userId);
281
282 if (empty($tags)) {
283 return;
284 }
285
286 $this->getDoctrine()
287 ->getRepository('WallabagCoreBundle:Entry')
288 ->removeTags($userId, $tags);
f71e55ac 289
8c61fd12 290 // cleanup orphan tags
f71e55ac
JB
291 $em = $this->getDoctrine()->getManager();
292
293 foreach ($tags as $tag) {
294 if (count($tag->getEntries()) === 0) {
295 $em->remove($tag);
296 }
297 }
298
299 $em->flush();
191564b7
JB
300 }
301
8799bde0 302 /**
2455472e 303 * Validate that a rule can be edited/deleted by the current user.
8799bde0 304 *
2455472e 305 * @param TaggingRule $rule
8799bde0
JB
306 */
307 private function validateRuleAction(TaggingRule $rule)
bf3dc999
JB
308 {
309 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
310 throw $this->createAccessDeniedException('You can not access this tagging rule.');
311 }
bf3dc999
JB
312 }
313
d9085c63
J
314 /**
315 * Retrieve config for the current user.
316 * If no config were found, create a new one.
317 *
4094ea47 318 * @return Config
d9085c63 319 */
4d85d7e9
J
320 private function getConfig()
321 {
322 $config = $this->getDoctrine()
323 ->getRepository('WallabagCoreBundle:Config')
324 ->findOneByUser($this->getUser());
325
ca17abce 326 // should NEVER HAPPEN ...
4d85d7e9
J
327 if (!$config) {
328 $config = new Config($this->getUser());
329 }
330
331 return $config;
332 }
e4b46f77 333
7ac3e575
JB
334 /**
335 * Delete account for current user.
336 *
337 * @Route("/account/delete", name="delete_account")
338 *
339 * @param Request $request
340 *
341 * @throws AccessDeniedHttpException
342 *
343 * @return \Symfony\Component\HttpFoundation\RedirectResponse
344 */
eed812af
JB
345 public function deleteAccountAction(Request $request)
346 {
347 $enabledUsers = $this->getDoctrine()
348 ->getRepository('WallabagUserBundle:User')
349 ->getSumEnabledUsers();
350
351 if ($enabledUsers <= 1) {
352 throw new AccessDeniedHttpException();
353 }
354
355 $user = $this->getUser();
356
357 // logout current user
358 $this->get('security.token_storage')->setToken(null);
359 $request->getSession()->invalidate();
360
361 $em = $this->get('fos_user.user_manager');
362 $em->deleteUser($user);
363
364 return $this->redirect($this->generateUrl('fos_user_security_login'));
365 }
9f01d0fd
NL
366
367 /**
368 * Switch view mode for current user.
369 *
370 * @Route("/config/view-mode", name="switch_view_mode")
371 *
372 * @param Request $request
373 *
374 * @return \Symfony\Component\HttpFoundation\RedirectResponse
375 */
376 public function changeViewModeAction(Request $request)
377 {
378 $user = $this->getUser();
9aa99128 379 $user->getConfig()->setListMode(!$user->getConfig()->getListMode());
9f01d0fd
NL
380
381 $em = $this->getDoctrine()->getManager();
382 $em->persist($user);
383 $em->flush();
384
385 return $this->redirect($request->headers->get('referer'));
386 }
4d85d7e9 387}