diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/ConfigController.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/ConfigController.php | 214 |
1 files changed, 127 insertions, 87 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index 907bf78e..a89bb780 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php | |||
@@ -10,8 +10,8 @@ use Symfony\Component\HttpFoundation\Request; | |||
10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | 10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
11 | use Wallabag\CoreBundle\Entity\Config; | 11 | use Wallabag\CoreBundle\Entity\Config; |
12 | use Wallabag\CoreBundle\Entity\TaggingRule; | 12 | use Wallabag\CoreBundle\Entity\TaggingRule; |
13 | use Wallabag\CoreBundle\Form\Type\ConfigType; | ||
14 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; | 13 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
14 | use Wallabag\CoreBundle\Form\Type\ConfigType; | ||
15 | use Wallabag\CoreBundle\Form\Type\RssType; | 15 | use Wallabag\CoreBundle\Form\Type\RssType; |
16 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; | 16 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; |
17 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | 17 | use Wallabag\CoreBundle\Form\Type\UserInformationType; |
@@ -54,7 +54,7 @@ class ConfigController extends Controller | |||
54 | } | 54 | } |
55 | 55 | ||
56 | // handle changing password | 56 | // handle changing password |
57 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']); | 57 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config') . '#set4']); |
58 | $pwdForm->handleRequest($request); | 58 | $pwdForm->handleRequest($request); |
59 | 59 | ||
60 | if ($pwdForm->isSubmitted() && $pwdForm->isValid()) { | 60 | if ($pwdForm->isSubmitted() && $pwdForm->isValid()) { |
@@ -69,13 +69,13 @@ class ConfigController extends Controller | |||
69 | 69 | ||
70 | $this->get('session')->getFlashBag()->add('notice', $message); | 70 | $this->get('session')->getFlashBag()->add('notice', $message); |
71 | 71 | ||
72 | return $this->redirect($this->generateUrl('config').'#set4'); | 72 | return $this->redirect($this->generateUrl('config') . '#set4'); |
73 | } | 73 | } |
74 | 74 | ||
75 | // handle changing user information | 75 | // handle changing user information |
76 | $userForm = $this->createForm(UserInformationType::class, $user, [ | 76 | $userForm = $this->createForm(UserInformationType::class, $user, [ |
77 | 'validation_groups' => ['Profile'], | 77 | 'validation_groups' => ['Profile'], |
78 | 'action' => $this->generateUrl('config').'#set3', | 78 | 'action' => $this->generateUrl('config') . '#set3', |
79 | ]); | 79 | ]); |
80 | $userForm->handleRequest($request); | 80 | $userForm->handleRequest($request); |
81 | 81 | ||
@@ -87,11 +87,11 @@ class ConfigController extends Controller | |||
87 | 'flashes.config.notice.user_updated' | 87 | 'flashes.config.notice.user_updated' |
88 | ); | 88 | ); |
89 | 89 | ||
90 | return $this->redirect($this->generateUrl('config').'#set3'); | 90 | return $this->redirect($this->generateUrl('config') . '#set3'); |
91 | } | 91 | } |
92 | 92 | ||
93 | // handle rss information | 93 | // handle rss information |
94 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']); | 94 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config') . '#set2']); |
95 | $rssForm->handleRequest($request); | 95 | $rssForm->handleRequest($request); |
96 | 96 | ||
97 | if ($rssForm->isSubmitted() && $rssForm->isValid()) { | 97 | if ($rssForm->isSubmitted() && $rssForm->isValid()) { |
@@ -103,12 +103,12 @@ class ConfigController extends Controller | |||
103 | 'flashes.config.notice.rss_updated' | 103 | 'flashes.config.notice.rss_updated' |
104 | ); | 104 | ); |
105 | 105 | ||
106 | return $this->redirect($this->generateUrl('config').'#set2'); | 106 | return $this->redirect($this->generateUrl('config') . '#set2'); |
107 | } | 107 | } |
108 | 108 | ||
109 | // handle tagging rule | 109 | // handle tagging rule |
110 | $taggingRule = new TaggingRule(); | 110 | $taggingRule = new TaggingRule(); |
111 | $action = $this->generateUrl('config').'#set5'; | 111 | $action = $this->generateUrl('config') . '#set5'; |
112 | 112 | ||
113 | if ($request->query->has('tagging-rule')) { | 113 | if ($request->query->has('tagging-rule')) { |
114 | $taggingRule = $this->getDoctrine() | 114 | $taggingRule = $this->getDoctrine() |
@@ -119,7 +119,7 @@ class ConfigController extends Controller | |||
119 | return $this->redirect($action); | 119 | return $this->redirect($action); |
120 | } | 120 | } |
121 | 121 | ||
122 | $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5'; | 122 | $action = $this->generateUrl('config') . '?tagging-rule=' . $taggingRule->getId() . '#set5'; |
123 | } | 123 | } |
124 | 124 | ||
125 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); | 125 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); |
@@ -135,7 +135,7 @@ class ConfigController extends Controller | |||
135 | 'flashes.config.notice.tagging_rules_updated' | 135 | 'flashes.config.notice.tagging_rules_updated' |
136 | ); | 136 | ); |
137 | 137 | ||
138 | return $this->redirect($this->generateUrl('config').'#set5'); | 138 | return $this->redirect($this->generateUrl('config') . '#set5'); |
139 | } | 139 | } |
140 | 140 | ||
141 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ | 141 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ |
@@ -151,9 +151,8 @@ class ConfigController extends Controller | |||
151 | 'token' => $config->getRssToken(), | 151 | 'token' => $config->getRssToken(), |
152 | ], | 152 | ], |
153 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | 153 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), |
154 | 'wallabag_url' => $this->get('craue_config')->get('wallabag_url'), | 154 | 'wallabag_url' => $this->getParameter('domain_name'), |
155 | 'enabled_users' => $this->getDoctrine() | 155 | 'enabled_users' => $this->get('wallabag_user.user_repository') |
156 | ->getRepository('WallabagUserBundle:User') | ||
157 | ->getSumEnabledUsers(), | 156 | ->getSumEnabledUsers(), |
158 | ]); | 157 | ]); |
159 | } | 158 | } |
@@ -183,7 +182,7 @@ class ConfigController extends Controller | |||
183 | 'flashes.config.notice.rss_token_updated' | 182 | 'flashes.config.notice.rss_token_updated' |
184 | ); | 183 | ); |
185 | 184 | ||
186 | return $this->redirect($this->generateUrl('config').'#set2'); | 185 | return $this->redirect($this->generateUrl('config') . '#set2'); |
187 | } | 186 | } |
188 | 187 | ||
189 | /** | 188 | /** |
@@ -208,7 +207,7 @@ class ConfigController extends Controller | |||
208 | 'flashes.config.notice.tagging_rules_deleted' | 207 | 'flashes.config.notice.tagging_rules_deleted' |
209 | ); | 208 | ); |
210 | 209 | ||
211 | return $this->redirect($this->generateUrl('config').'#set5'); | 210 | return $this->redirect($this->generateUrl('config') . '#set5'); |
212 | } | 211 | } |
213 | 212 | ||
214 | /** | 213 | /** |
@@ -224,7 +223,7 @@ class ConfigController extends Controller | |||
224 | { | 223 | { |
225 | $this->validateRuleAction($rule); | 224 | $this->validateRuleAction($rule); |
226 | 225 | ||
227 | return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5'); | 226 | return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5'); |
228 | } | 227 | } |
229 | 228 | ||
230 | /** | 229 | /** |
@@ -242,56 +241,114 @@ class ConfigController extends Controller | |||
242 | ->getRepository('WallabagAnnotationBundle:Annotation') | 241 | ->getRepository('WallabagAnnotationBundle:Annotation') |
243 | ->removeAllByUserId($this->getUser()->getId()); | 242 | ->removeAllByUserId($this->getUser()->getId()); |
244 | break; | 243 | break; |
245 | |||
246 | case 'tags': | 244 | case 'tags': |
247 | $this->removeAllTagsByUserId($this->getUser()->getId()); | 245 | $this->removeAllTagsByUserId($this->getUser()->getId()); |
248 | break; | 246 | break; |
249 | |||
250 | case 'entries': | 247 | case 'entries': |
251 | // SQLite doesn't care about cascading remove, so we need to manually remove associated stuf | 248 | // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff |
252 | // otherwise they won't be removed ... | 249 | // otherwise they won't be removed ... |
253 | if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { | 250 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { |
254 | $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); | 251 | $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); |
255 | } | 252 | } |
256 | 253 | ||
257 | // manually remove tags to avoid orphan tag | 254 | // manually remove tags to avoid orphan tag |
258 | $this->removeAllTagsByUserId($this->getUser()->getId()); | 255 | $this->removeAllTagsByUserId($this->getUser()->getId()); |
259 | 256 | ||
260 | $this->getDoctrine() | 257 | $this->get('wallabag_core.entry_repository')->removeAllByUserId($this->getUser()->getId()); |
261 | ->getRepository('WallabagCoreBundle:Entry') | 258 | break; |
262 | ->removeAllByUserId($this->getUser()->getId()); | 259 | case 'archived': |
260 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { | ||
261 | $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId()); | ||
262 | } | ||
263 | |||
264 | // manually remove tags to avoid orphan tag | ||
265 | $this->removeTagsForArchivedByUserId($this->getUser()->getId()); | ||
266 | |||
267 | $this->get('wallabag_core.entry_repository')->removeArchivedByUserId($this->getUser()->getId()); | ||
268 | break; | ||
263 | } | 269 | } |
264 | 270 | ||
265 | $this->get('session')->getFlashBag()->add( | 271 | $this->get('session')->getFlashBag()->add( |
266 | 'notice', | 272 | 'notice', |
267 | 'flashes.config.notice.'.$type.'_reset' | 273 | 'flashes.config.notice.' . $type . '_reset' |
268 | ); | 274 | ); |
269 | 275 | ||
270 | return $this->redirect($this->generateUrl('config').'#set3'); | 276 | return $this->redirect($this->generateUrl('config') . '#set3'); |
271 | } | 277 | } |
272 | 278 | ||
273 | /** | 279 | /** |
274 | * Remove all tags for a given user and cleanup orphan tags. | 280 | * Delete account for current user. |
275 | * | 281 | * |
276 | * @param int $userId | 282 | * @Route("/account/delete", name="delete_account") |
283 | * | ||
284 | * @param Request $request | ||
285 | * | ||
286 | * @throws AccessDeniedHttpException | ||
287 | * | ||
288 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | ||
277 | */ | 289 | */ |
278 | private function removeAllTagsByUserId($userId) | 290 | public function deleteAccountAction(Request $request) |
291 | { | ||
292 | $enabledUsers = $this->get('wallabag_user.user_repository') | ||
293 | ->getSumEnabledUsers(); | ||
294 | |||
295 | if ($enabledUsers <= 1) { | ||
296 | throw new AccessDeniedHttpException(); | ||
297 | } | ||
298 | |||
299 | $user = $this->getUser(); | ||
300 | |||
301 | // logout current user | ||
302 | $this->get('security.token_storage')->setToken(null); | ||
303 | $request->getSession()->invalidate(); | ||
304 | |||
305 | $em = $this->get('fos_user.user_manager'); | ||
306 | $em->deleteUser($user); | ||
307 | |||
308 | return $this->redirect($this->generateUrl('fos_user_security_login')); | ||
309 | } | ||
310 | |||
311 | /** | ||
312 | * Switch view mode for current user. | ||
313 | * | ||
314 | * @Route("/config/view-mode", name="switch_view_mode") | ||
315 | * | ||
316 | * @param Request $request | ||
317 | * | ||
318 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | ||
319 | */ | ||
320 | public function changeViewModeAction(Request $request) | ||
279 | { | 321 | { |
280 | $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findAllTags($userId); | 322 | $user = $this->getUser(); |
323 | $user->getConfig()->setListMode(!$user->getConfig()->getListMode()); | ||
324 | |||
325 | $em = $this->getDoctrine()->getManager(); | ||
326 | $em->persist($user); | ||
327 | $em->flush(); | ||
281 | 328 | ||
329 | return $this->redirect($request->headers->get('referer')); | ||
330 | } | ||
331 | |||
332 | /** | ||
333 | * Remove all tags for given tags and a given user and cleanup orphan tags. | ||
334 | * | ||
335 | * @param array $tags | ||
336 | * @param int $userId | ||
337 | */ | ||
338 | private function removeAllTagsByStatusAndUserId($tags, $userId) | ||
339 | { | ||
282 | if (empty($tags)) { | 340 | if (empty($tags)) { |
283 | return; | 341 | return; |
284 | } | 342 | } |
285 | 343 | ||
286 | $this->getDoctrine() | 344 | $this->get('wallabag_core.entry_repository') |
287 | ->getRepository('WallabagCoreBundle:Entry') | ||
288 | ->removeTags($userId, $tags); | 345 | ->removeTags($userId, $tags); |
289 | 346 | ||
290 | // cleanup orphan tags | 347 | // cleanup orphan tags |
291 | $em = $this->getDoctrine()->getManager(); | 348 | $em = $this->getDoctrine()->getManager(); |
292 | 349 | ||
293 | foreach ($tags as $tag) { | 350 | foreach ($tags as $tag) { |
294 | if (count($tag->getEntries()) === 0) { | 351 | if (0 === count($tag->getEntries())) { |
295 | $em->remove($tag); | 352 | $em->remove($tag); |
296 | } | 353 | } |
297 | } | 354 | } |
@@ -300,13 +357,50 @@ class ConfigController extends Controller | |||
300 | } | 357 | } |
301 | 358 | ||
302 | /** | 359 | /** |
360 | * Remove all tags for a given user and cleanup orphan tags. | ||
361 | * | ||
362 | * @param int $userId | ||
363 | */ | ||
364 | private function removeAllTagsByUserId($userId) | ||
365 | { | ||
366 | $tags = $this->get('wallabag_core.tag_repository')->findAllTags($userId); | ||
367 | $this->removeAllTagsByStatusAndUserId($tags, $userId); | ||
368 | } | ||
369 | |||
370 | /** | ||
371 | * Remove all tags for a given user and cleanup orphan tags. | ||
372 | * | ||
373 | * @param int $userId | ||
374 | */ | ||
375 | private function removeTagsForArchivedByUserId($userId) | ||
376 | { | ||
377 | $tags = $this->get('wallabag_core.tag_repository')->findForArchivedArticlesByUser($userId); | ||
378 | $this->removeAllTagsByStatusAndUserId($tags, $userId); | ||
379 | } | ||
380 | |||
381 | private function removeAnnotationsForArchivedByUserId($userId) | ||
382 | { | ||
383 | $em = $this->getDoctrine()->getManager(); | ||
384 | |||
385 | $archivedEntriesAnnotations = $this->getDoctrine() | ||
386 | ->getRepository('WallabagAnnotationBundle:Annotation') | ||
387 | ->findAllArchivedEntriesByUser($userId); | ||
388 | |||
389 | foreach ($archivedEntriesAnnotations as $archivedEntriesAnnotation) { | ||
390 | $em->remove($archivedEntriesAnnotation); | ||
391 | } | ||
392 | |||
393 | $em->flush(); | ||
394 | } | ||
395 | |||
396 | /** | ||
303 | * Validate that a rule can be edited/deleted by the current user. | 397 | * Validate that a rule can be edited/deleted by the current user. |
304 | * | 398 | * |
305 | * @param TaggingRule $rule | 399 | * @param TaggingRule $rule |
306 | */ | 400 | */ |
307 | private function validateRuleAction(TaggingRule $rule) | 401 | private function validateRuleAction(TaggingRule $rule) |
308 | { | 402 | { |
309 | if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) { | 403 | if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) { |
310 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); | 404 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); |
311 | } | 405 | } |
312 | } | 406 | } |
@@ -330,58 +424,4 @@ class ConfigController extends Controller | |||
330 | 424 | ||
331 | return $config; | 425 | return $config; |
332 | } | 426 | } |
333 | |||
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 | */ | ||
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 | } | ||
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(); | ||
379 | $user->getConfig()->setListMode(!$user->getConfig()->getListMode()); | ||
380 | |||
381 | $em = $this->getDoctrine()->getManager(); | ||
382 | $em->persist($user); | ||
383 | $em->flush(); | ||
384 | |||
385 | return $this->redirect($request->headers->get('referer')); | ||
386 | } | ||
387 | } | 427 | } |