]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
6 | use Symfony\Component\HttpFoundation\JsonResponse; | |
7 | use Symfony\Component\HttpFoundation\RedirectResponse; | |
8 | use Symfony\Component\HttpFoundation\Request; | |
9 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | |
10 | use Symfony\Component\Routing\Annotation\Route; | |
11 | use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint; | |
12 | use Wallabag\CoreBundle\Entity\Config; | |
13 | use Wallabag\CoreBundle\Entity\TaggingRule; | |
14 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; | |
15 | use Wallabag\CoreBundle\Form\Type\ConfigType; | |
16 | use Wallabag\CoreBundle\Form\Type\RssType; | |
17 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; | |
18 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | |
19 | use Wallabag\CoreBundle\Tools\Utils; | |
20 | ||
21 | class ConfigController extends Controller | |
22 | { | |
23 | /** | |
24 | * @param Request $request | |
25 | * | |
26 | * @Route("/config", name="config") | |
27 | */ | |
28 | public function indexAction(Request $request) | |
29 | { | |
30 | $em = $this->getDoctrine()->getManager(); | |
31 | $config = $this->getConfig(); | |
32 | $userManager = $this->container->get('fos_user.user_manager'); | |
33 | $user = $this->getUser(); | |
34 | ||
35 | // handle basic config detail (this form is defined as a service) | |
36 | $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]); | |
37 | $configForm->handleRequest($request); | |
38 | ||
39 | if ($configForm->isSubmitted() && $configForm->isValid()) { | |
40 | $em->persist($config); | |
41 | $em->flush(); | |
42 | ||
43 | $request->getSession()->set('_locale', $config->getLanguage()); | |
44 | ||
45 | // switch active theme | |
46 | $activeTheme = $this->get('liip_theme.active_theme'); | |
47 | $activeTheme->setName($config->getTheme()); | |
48 | ||
49 | $this->get('session')->getFlashBag()->add( | |
50 | 'notice', | |
51 | 'flashes.config.notice.config_saved' | |
52 | ); | |
53 | ||
54 | return $this->redirect($this->generateUrl('config')); | |
55 | } | |
56 | ||
57 | // handle changing password | |
58 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config') . '#set4']); | |
59 | $pwdForm->handleRequest($request); | |
60 | ||
61 | if ($pwdForm->isSubmitted() && $pwdForm->isValid()) { | |
62 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { | |
63 | $message = 'flashes.config.notice.password_not_updated_demo'; | |
64 | } else { | |
65 | $message = 'flashes.config.notice.password_updated'; | |
66 | ||
67 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); | |
68 | $userManager->updateUser($user, true); | |
69 | } | |
70 | ||
71 | $this->get('session')->getFlashBag()->add('notice', $message); | |
72 | ||
73 | return $this->redirect($this->generateUrl('config') . '#set4'); | |
74 | } | |
75 | ||
76 | // handle changing user information | |
77 | $userForm = $this->createForm(UserInformationType::class, $user, [ | |
78 | 'validation_groups' => ['Profile'], | |
79 | 'action' => $this->generateUrl('config') . '#set3', | |
80 | ]); | |
81 | $userForm->handleRequest($request); | |
82 | ||
83 | if ($userForm->isSubmitted() && $userForm->isValid()) { | |
84 | $userManager->updateUser($user, true); | |
85 | ||
86 | $this->get('session')->getFlashBag()->add( | |
87 | 'notice', | |
88 | 'flashes.config.notice.user_updated' | |
89 | ); | |
90 | ||
91 | return $this->redirect($this->generateUrl('config') . '#set3'); | |
92 | } | |
93 | ||
94 | // handle rss information | |
95 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config') . '#set2']); | |
96 | $rssForm->handleRequest($request); | |
97 | ||
98 | if ($rssForm->isSubmitted() && $rssForm->isValid()) { | |
99 | $em->persist($config); | |
100 | $em->flush(); | |
101 | ||
102 | $this->get('session')->getFlashBag()->add( | |
103 | 'notice', | |
104 | 'flashes.config.notice.rss_updated' | |
105 | ); | |
106 | ||
107 | return $this->redirect($this->generateUrl('config') . '#set2'); | |
108 | } | |
109 | ||
110 | // handle tagging rule | |
111 | $taggingRule = new TaggingRule(); | |
112 | $action = $this->generateUrl('config') . '#set5'; | |
113 | ||
114 | if ($request->query->has('tagging-rule')) { | |
115 | $taggingRule = $this->getDoctrine() | |
116 | ->getRepository('WallabagCoreBundle:TaggingRule') | |
117 | ->find($request->query->get('tagging-rule')); | |
118 | ||
119 | if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) { | |
120 | return $this->redirect($action); | |
121 | } | |
122 | ||
123 | $action = $this->generateUrl('config') . '?tagging-rule=' . $taggingRule->getId() . '#set5'; | |
124 | } | |
125 | ||
126 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); | |
127 | $newTaggingRule->handleRequest($request); | |
128 | ||
129 | if ($newTaggingRule->isSubmitted() && $newTaggingRule->isValid()) { | |
130 | $taggingRule->setConfig($config); | |
131 | $em->persist($taggingRule); | |
132 | $em->flush(); | |
133 | ||
134 | $this->get('session')->getFlashBag()->add( | |
135 | 'notice', | |
136 | 'flashes.config.notice.tagging_rules_updated' | |
137 | ); | |
138 | ||
139 | return $this->redirect($this->generateUrl('config') . '#set5'); | |
140 | } | |
141 | ||
142 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ | |
143 | 'form' => [ | |
144 | 'config' => $configForm->createView(), | |
145 | 'rss' => $rssForm->createView(), | |
146 | 'pwd' => $pwdForm->createView(), | |
147 | 'user' => $userForm->createView(), | |
148 | 'new_tagging_rule' => $newTaggingRule->createView(), | |
149 | ], | |
150 | 'rss' => [ | |
151 | 'username' => $user->getUsername(), | |
152 | 'token' => $config->getRssToken(), | |
153 | ], | |
154 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
155 | 'wallabag_url' => $this->getParameter('domain_name'), | |
156 | 'enabled_users' => $this->get('wallabag_user.user_repository') | |
157 | ->getSumEnabledUsers(), | |
158 | ]); | |
159 | } | |
160 | ||
161 | /** | |
162 | * @param Request $request | |
163 | * | |
164 | * @Route("/generate-token", name="generate_token") | |
165 | * | |
166 | * @return RedirectResponse|JsonResponse | |
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()) { | |
178 | return new JsonResponse(['token' => $config->getRssToken()]); | |
179 | } | |
180 | ||
181 | $this->get('session')->getFlashBag()->add( | |
182 | 'notice', | |
183 | 'flashes.config.notice.rss_token_updated' | |
184 | ); | |
185 | ||
186 | return $this->redirect($this->generateUrl('config') . '#set2'); | |
187 | } | |
188 | ||
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 | * | |
196 | * @return RedirectResponse | |
197 | */ | |
198 | public function deleteTaggingRuleAction(TaggingRule $rule) | |
199 | { | |
200 | $this->validateRuleAction($rule); | |
201 | ||
202 | $em = $this->getDoctrine()->getManager(); | |
203 | $em->remove($rule); | |
204 | $em->flush(); | |
205 | ||
206 | $this->get('session')->getFlashBag()->add( | |
207 | 'notice', | |
208 | 'flashes.config.notice.tagging_rules_deleted' | |
209 | ); | |
210 | ||
211 | return $this->redirect($this->generateUrl('config') . '#set5'); | |
212 | } | |
213 | ||
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) | |
224 | { | |
225 | $this->validateRuleAction($rule); | |
226 | ||
227 | return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5'); | |
228 | } | |
229 | ||
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 | case 'tags': | |
246 | $this->removeAllTagsByUserId($this->getUser()->getId()); | |
247 | break; | |
248 | case 'entries': | |
249 | // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff | |
250 | // otherwise they won't be removed ... | |
251 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { | |
252 | $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); | |
253 | } | |
254 | ||
255 | // manually remove tags to avoid orphan tag | |
256 | $this->removeAllTagsByUserId($this->getUser()->getId()); | |
257 | ||
258 | $this->get('wallabag_core.entry_repository')->removeAllByUserId($this->getUser()->getId()); | |
259 | break; | |
260 | case 'archived': | |
261 | if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) { | |
262 | $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId()); | |
263 | } | |
264 | ||
265 | // manually remove tags to avoid orphan tag | |
266 | $this->removeTagsForArchivedByUserId($this->getUser()->getId()); | |
267 | ||
268 | $this->get('wallabag_core.entry_repository')->removeArchivedByUserId($this->getUser()->getId()); | |
269 | break; | |
270 | } | |
271 | ||
272 | $this->get('session')->getFlashBag()->add( | |
273 | 'notice', | |
274 | 'flashes.config.notice.' . $type . '_reset' | |
275 | ); | |
276 | ||
277 | return $this->redirect($this->generateUrl('config') . '#set3'); | |
278 | } | |
279 | ||
280 | /** | |
281 | * Delete account for current user. | |
282 | * | |
283 | * @Route("/account/delete", name="delete_account") | |
284 | * | |
285 | * @param Request $request | |
286 | * | |
287 | * @throws AccessDeniedHttpException | |
288 | * | |
289 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
290 | */ | |
291 | public function deleteAccountAction(Request $request) | |
292 | { | |
293 | $enabledUsers = $this->get('wallabag_user.user_repository') | |
294 | ->getSumEnabledUsers(); | |
295 | ||
296 | if ($enabledUsers <= 1) { | |
297 | throw new AccessDeniedHttpException(); | |
298 | } | |
299 | ||
300 | $user = $this->getUser(); | |
301 | ||
302 | // logout current user | |
303 | $this->get('security.token_storage')->setToken(null); | |
304 | $request->getSession()->invalidate(); | |
305 | ||
306 | $em = $this->get('fos_user.user_manager'); | |
307 | $em->deleteUser($user); | |
308 | ||
309 | return $this->redirect($this->generateUrl('fos_user_security_login')); | |
310 | } | |
311 | ||
312 | /** | |
313 | * Switch view mode for current user. | |
314 | * | |
315 | * @Route("/config/view-mode", name="switch_view_mode") | |
316 | * | |
317 | * @param Request $request | |
318 | * | |
319 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
320 | */ | |
321 | public function changeViewModeAction(Request $request) | |
322 | { | |
323 | $user = $this->getUser(); | |
324 | $user->getConfig()->setListMode(!$user->getConfig()->getListMode()); | |
325 | ||
326 | $em = $this->getDoctrine()->getManager(); | |
327 | $em->persist($user); | |
328 | $em->flush(); | |
329 | ||
330 | return $this->redirect($request->headers->get('referer')); | |
331 | } | |
332 | ||
333 | /** | |
334 | * Change the locale for the current user. | |
335 | * | |
336 | * @param Request $request | |
337 | * @param string $language | |
338 | * | |
339 | * @Route("/locale/{language}", name="changeLocale") | |
340 | * | |
341 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
342 | */ | |
343 | public function setLocaleAction(Request $request, $language = null) | |
344 | { | |
345 | $errors = $this->get('validator')->validate($language, (new LocaleConstraint())); | |
346 | ||
347 | if (0 === \count($errors)) { | |
348 | $request->getSession()->set('_locale', $language); | |
349 | } | |
350 | ||
351 | return $this->redirect($request->headers->get('referer', $this->generateUrl('homepage'))); | |
352 | } | |
353 | ||
354 | /** | |
355 | * Remove all tags for given tags and a given user and cleanup orphan tags. | |
356 | * | |
357 | * @param array $tags | |
358 | * @param int $userId | |
359 | */ | |
360 | private function removeAllTagsByStatusAndUserId($tags, $userId) | |
361 | { | |
362 | if (empty($tags)) { | |
363 | return; | |
364 | } | |
365 | ||
366 | $this->get('wallabag_core.entry_repository') | |
367 | ->removeTags($userId, $tags); | |
368 | ||
369 | // cleanup orphan tags | |
370 | $em = $this->getDoctrine()->getManager(); | |
371 | ||
372 | foreach ($tags as $tag) { | |
373 | if (0 === \count($tag->getEntries())) { | |
374 | $em->remove($tag); | |
375 | } | |
376 | } | |
377 | ||
378 | $em->flush(); | |
379 | } | |
380 | ||
381 | /** | |
382 | * Remove all tags for a given user and cleanup orphan tags. | |
383 | * | |
384 | * @param int $userId | |
385 | */ | |
386 | private function removeAllTagsByUserId($userId) | |
387 | { | |
388 | $tags = $this->get('wallabag_core.tag_repository')->findAllTags($userId); | |
389 | $this->removeAllTagsByStatusAndUserId($tags, $userId); | |
390 | } | |
391 | ||
392 | /** | |
393 | * Remove all tags for a given user and cleanup orphan tags. | |
394 | * | |
395 | * @param int $userId | |
396 | */ | |
397 | private function removeTagsForArchivedByUserId($userId) | |
398 | { | |
399 | $tags = $this->get('wallabag_core.tag_repository')->findForArchivedArticlesByUser($userId); | |
400 | $this->removeAllTagsByStatusAndUserId($tags, $userId); | |
401 | } | |
402 | ||
403 | private function removeAnnotationsForArchivedByUserId($userId) | |
404 | { | |
405 | $em = $this->getDoctrine()->getManager(); | |
406 | ||
407 | $archivedEntriesAnnotations = $this->getDoctrine() | |
408 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
409 | ->findAllArchivedEntriesByUser($userId); | |
410 | ||
411 | foreach ($archivedEntriesAnnotations as $archivedEntriesAnnotation) { | |
412 | $em->remove($archivedEntriesAnnotation); | |
413 | } | |
414 | ||
415 | $em->flush(); | |
416 | } | |
417 | ||
418 | /** | |
419 | * Validate that a rule can be edited/deleted by the current user. | |
420 | * | |
421 | * @param TaggingRule $rule | |
422 | */ | |
423 | private function validateRuleAction(TaggingRule $rule) | |
424 | { | |
425 | if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) { | |
426 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); | |
427 | } | |
428 | } | |
429 | ||
430 | /** | |
431 | * Retrieve config for the current user. | |
432 | * If no config were found, create a new one. | |
433 | * | |
434 | * @return Config | |
435 | */ | |
436 | private function getConfig() | |
437 | { | |
438 | $config = $this->getDoctrine() | |
439 | ->getRepository('WallabagCoreBundle:Config') | |
440 | ->findOneByUser($this->getUser()); | |
441 | ||
442 | // should NEVER HAPPEN ... | |
443 | if (!$config) { | |
444 | $config = new Config($this->getUser()); | |
445 | } | |
446 | ||
447 | return $config; | |
448 | } | |
449 | } |