diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/ConfigController.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/ConfigController.php | 141 |
1 files changed, 136 insertions, 5 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index 46fb9503..907bf78e 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php | |||
@@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |||
7 | use Symfony\Component\HttpFoundation\JsonResponse; | 7 | use Symfony\Component\HttpFoundation\JsonResponse; |
8 | use Symfony\Component\HttpFoundation\RedirectResponse; | 8 | use Symfony\Component\HttpFoundation\RedirectResponse; |
9 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | ||
10 | use Wallabag\CoreBundle\Entity\Config; | 11 | use Wallabag\CoreBundle\Entity\Config; |
11 | use Wallabag\CoreBundle\Entity\TaggingRule; | 12 | use Wallabag\CoreBundle\Entity\TaggingRule; |
12 | use Wallabag\CoreBundle\Form\Type\ConfigType; | 13 | use Wallabag\CoreBundle\Form\Type\ConfigType; |
@@ -34,7 +35,7 @@ class ConfigController extends Controller | |||
34 | $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]); | 35 | $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]); |
35 | $configForm->handleRequest($request); | 36 | $configForm->handleRequest($request); |
36 | 37 | ||
37 | if ($configForm->isValid()) { | 38 | if ($configForm->isSubmitted() && $configForm->isValid()) { |
38 | $em->persist($config); | 39 | $em->persist($config); |
39 | $em->flush(); | 40 | $em->flush(); |
40 | 41 | ||
@@ -56,7 +57,7 @@ class ConfigController extends Controller | |||
56 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']); | 57 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']); |
57 | $pwdForm->handleRequest($request); | 58 | $pwdForm->handleRequest($request); |
58 | 59 | ||
59 | if ($pwdForm->isValid()) { | 60 | if ($pwdForm->isSubmitted() && $pwdForm->isValid()) { |
60 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { | 61 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { |
61 | $message = 'flashes.config.notice.password_not_updated_demo'; | 62 | $message = 'flashes.config.notice.password_not_updated_demo'; |
62 | } else { | 63 | } else { |
@@ -78,7 +79,7 @@ class ConfigController extends Controller | |||
78 | ]); | 79 | ]); |
79 | $userForm->handleRequest($request); | 80 | $userForm->handleRequest($request); |
80 | 81 | ||
81 | if ($userForm->isValid()) { | 82 | if ($userForm->isSubmitted() && $userForm->isValid()) { |
82 | $userManager->updateUser($user, true); | 83 | $userManager->updateUser($user, true); |
83 | 84 | ||
84 | $this->get('session')->getFlashBag()->add( | 85 | $this->get('session')->getFlashBag()->add( |
@@ -93,7 +94,7 @@ class ConfigController extends Controller | |||
93 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']); | 94 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']); |
94 | $rssForm->handleRequest($request); | 95 | $rssForm->handleRequest($request); |
95 | 96 | ||
96 | if ($rssForm->isValid()) { | 97 | if ($rssForm->isSubmitted() && $rssForm->isValid()) { |
97 | $em->persist($config); | 98 | $em->persist($config); |
98 | $em->flush(); | 99 | $em->flush(); |
99 | 100 | ||
@@ -124,7 +125,7 @@ class ConfigController extends Controller | |||
124 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); | 125 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); |
125 | $newTaggingRule->handleRequest($request); | 126 | $newTaggingRule->handleRequest($request); |
126 | 127 | ||
127 | if ($newTaggingRule->isValid()) { | 128 | if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) { |
128 | $taggingRule->setConfig($config); | 129 | $taggingRule->setConfig($config); |
129 | $em->persist($taggingRule); | 130 | $em->persist($taggingRule); |
130 | $em->flush(); | 131 | $em->flush(); |
@@ -150,6 +151,10 @@ class ConfigController extends Controller | |||
150 | 'token' => $config->getRssToken(), | 151 | 'token' => $config->getRssToken(), |
151 | ], | 152 | ], |
152 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | 153 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), |
154 | 'wallabag_url' => $this->get('craue_config')->get('wallabag_url'), | ||
155 | 'enabled_users' => $this->getDoctrine() | ||
156 | ->getRepository('WallabagUserBundle:User') | ||
157 | ->getSumEnabledUsers(), | ||
153 | ]); | 158 | ]); |
154 | } | 159 | } |
155 | 160 | ||
@@ -223,6 +228,78 @@ class ConfigController extends Controller | |||
223 | } | 228 | } |
224 | 229 | ||
225 | /** | 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 | { | ||
239 | switch ($type) { | ||
240 | case 'annotations': | ||
241 | $this->getDoctrine() | ||
242 | ->getRepository('WallabagAnnotationBundle:Annotation') | ||
243 | ->removeAllByUserId($this->getUser()->getId()); | ||
244 | break; | ||
245 | |||
246 | case 'tags': | ||
247 | $this->removeAllTagsByUserId($this->getUser()->getId()); | ||
248 | break; | ||
249 | |||
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()); | ||
255 | } | ||
256 | |||
257 | // manually remove tags to avoid orphan tag | ||
258 | $this->removeAllTagsByUserId($this->getUser()->getId()); | ||
259 | |||
260 | $this->getDoctrine() | ||
261 | ->getRepository('WallabagCoreBundle:Entry') | ||
262 | ->removeAllByUserId($this->getUser()->getId()); | ||
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 | |||
273 | /** | ||
274 | * Remove all tags for a given user and cleanup orphan tags. | ||
275 | * | ||
276 | * @param int $userId | ||
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); | ||
289 | |||
290 | // cleanup orphan tags | ||
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(); | ||
300 | } | ||
301 | |||
302 | /** | ||
226 | * Validate that a rule can be edited/deleted by the current user. | 303 | * Validate that a rule can be edited/deleted by the current user. |
227 | * | 304 | * |
228 | * @param TaggingRule $rule | 305 | * @param TaggingRule $rule |
@@ -253,4 +330,58 @@ class ConfigController extends Controller | |||
253 | 330 | ||
254 | return $config; | 331 | return $config; |
255 | } | 332 | } |
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 | } | ||
256 | } | 387 | } |