]>
Commit | Line | Data |
---|---|---|
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\RedirectResponse; | |
9 | use Symfony\Component\HttpFoundation\Request; | |
10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | |
11 | use Wallabag\CoreBundle\Entity\Config; | |
12 | use Wallabag\CoreBundle\Entity\TaggingRule; | |
13 | use Wallabag\CoreBundle\Form\Type\ConfigType; | |
14 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; | |
15 | use Wallabag\CoreBundle\Form\Type\RssType; | |
16 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; | |
17 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | |
18 | use Wallabag\CoreBundle\Tools\Utils; | |
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, ['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 | 'flashes.config.notice.config_saved' | |
49 | ); | |
50 | ||
51 | return $this->redirect($this->generateUrl('config')); | |
52 | } | |
53 | ||
54 | // handle changing password | |
55 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']); | |
56 | $pwdForm->handleRequest($request); | |
57 | ||
58 | if ($pwdForm->isValid()) { | |
59 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { | |
60 | $message = 'flashes.config.notice.password_not_updated_demo'; | |
61 | } else { | |
62 | $message = 'flashes.config.notice.password_updated'; | |
63 | ||
64 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); | |
65 | $userManager->updateUser($user, true); | |
66 | } | |
67 | ||
68 | $this->get('session')->getFlashBag()->add('notice', $message); | |
69 | ||
70 | return $this->redirect($this->generateUrl('config').'#set4'); | |
71 | } | |
72 | ||
73 | // handle changing user information | |
74 | $userForm = $this->createForm(UserInformationType::class, $user, [ | |
75 | 'validation_groups' => ['Profile'], | |
76 | 'action' => $this->generateUrl('config').'#set3', | |
77 | ]); | |
78 | $userForm->handleRequest($request); | |
79 | ||
80 | if ($userForm->isValid()) { | |
81 | $userManager->updateUser($user, true); | |
82 | ||
83 | $this->get('session')->getFlashBag()->add( | |
84 | 'notice', | |
85 | 'flashes.config.notice.user_updated' | |
86 | ); | |
87 | ||
88 | return $this->redirect($this->generateUrl('config').'#set3'); | |
89 | } | |
90 | ||
91 | // handle rss information | |
92 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']); | |
93 | $rssForm->handleRequest($request); | |
94 | ||
95 | if ($rssForm->isValid()) { | |
96 | $em->persist($config); | |
97 | $em->flush(); | |
98 | ||
99 | $this->get('session')->getFlashBag()->add( | |
100 | 'notice', | |
101 | 'flashes.config.notice.rss_updated' | |
102 | ); | |
103 | ||
104 | return $this->redirect($this->generateUrl('config').'#set2'); | |
105 | } | |
106 | ||
107 | // handle tagging rule | |
108 | $taggingRule = new TaggingRule(); | |
109 | $action = $this->generateUrl('config').'#set5'; | |
110 | ||
111 | if ($request->query->has('tagging-rule')) { | |
112 | $taggingRule = $this->getDoctrine() | |
113 | ->getRepository('WallabagCoreBundle:TaggingRule') | |
114 | ->find($request->query->get('tagging-rule')); | |
115 | ||
116 | if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) { | |
117 | return $this->redirect($action); | |
118 | } | |
119 | ||
120 | $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5'; | |
121 | } | |
122 | ||
123 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); | |
124 | $newTaggingRule->handleRequest($request); | |
125 | ||
126 | if ($newTaggingRule->isValid()) { | |
127 | $taggingRule->setConfig($config); | |
128 | $em->persist($taggingRule); | |
129 | $em->flush(); | |
130 | ||
131 | $this->get('session')->getFlashBag()->add( | |
132 | 'notice', | |
133 | 'flashes.config.notice.tagging_rules_updated' | |
134 | ); | |
135 | ||
136 | return $this->redirect($this->generateUrl('config').'#set5'); | |
137 | } | |
138 | ||
139 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ | |
140 | 'form' => [ | |
141 | 'config' => $configForm->createView(), | |
142 | 'rss' => $rssForm->createView(), | |
143 | 'pwd' => $pwdForm->createView(), | |
144 | 'user' => $userForm->createView(), | |
145 | 'new_tagging_rule' => $newTaggingRule->createView(), | |
146 | ], | |
147 | 'rss' => [ | |
148 | 'username' => $user->getUsername(), | |
149 | 'token' => $config->getRssToken(), | |
150 | ], | |
151 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
152 | 'enabled_users' => $this->getDoctrine() | |
153 | ->getRepository('WallabagUserBundle:User') | |
154 | ->getSumEnabledUsers(), | |
155 | ]); | |
156 | } | |
157 | ||
158 | /** | |
159 | * @param Request $request | |
160 | * | |
161 | * @Route("/generate-token", name="generate_token") | |
162 | * | |
163 | * @return RedirectResponse|JsonResponse | |
164 | */ | |
165 | public function generateTokenAction(Request $request) | |
166 | { | |
167 | $config = $this->getConfig(); | |
168 | $config->setRssToken(Utils::generateToken()); | |
169 | ||
170 | $em = $this->getDoctrine()->getManager(); | |
171 | $em->persist($config); | |
172 | $em->flush(); | |
173 | ||
174 | if ($request->isXmlHttpRequest()) { | |
175 | return new JsonResponse(['token' => $config->getRssToken()]); | |
176 | } | |
177 | ||
178 | $this->get('session')->getFlashBag()->add( | |
179 | 'notice', | |
180 | 'flashes.config.notice.rss_token_updated' | |
181 | ); | |
182 | ||
183 | return $this->redirect($this->generateUrl('config').'#set2'); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Deletes a tagging rule and redirect to the config homepage. | |
188 | * | |
189 | * @param TaggingRule $rule | |
190 | * | |
191 | * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule") | |
192 | * | |
193 | * @return RedirectResponse | |
194 | */ | |
195 | public function deleteTaggingRuleAction(TaggingRule $rule) | |
196 | { | |
197 | $this->validateRuleAction($rule); | |
198 | ||
199 | $em = $this->getDoctrine()->getManager(); | |
200 | $em->remove($rule); | |
201 | $em->flush(); | |
202 | ||
203 | $this->get('session')->getFlashBag()->add( | |
204 | 'notice', | |
205 | 'flashes.config.notice.tagging_rules_deleted' | |
206 | ); | |
207 | ||
208 | return $this->redirect($this->generateUrl('config').'#set5'); | |
209 | } | |
210 | ||
211 | /** | |
212 | * Edit a tagging rule. | |
213 | * | |
214 | * @param TaggingRule $rule | |
215 | * | |
216 | * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule") | |
217 | * | |
218 | * @return RedirectResponse | |
219 | */ | |
220 | public function editTaggingRuleAction(TaggingRule $rule) | |
221 | { | |
222 | $this->validateRuleAction($rule); | |
223 | ||
224 | return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5'); | |
225 | } | |
226 | ||
227 | /** | |
228 | * Remove all annotations OR tags OR entries for the current user. | |
229 | * | |
230 | * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset") | |
231 | * | |
232 | * @return RedirectResponse | |
233 | */ | |
234 | public function resetAction($type) | |
235 | { | |
236 | $em = $this->getDoctrine()->getManager(); | |
237 | ||
238 | switch ($type) { | |
239 | case 'annotations': | |
240 | $this->getDoctrine() | |
241 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
242 | ->removeAllByUserId($this->getUser()->getId()); | |
243 | break; | |
244 | ||
245 | case 'tags': | |
246 | $this->removeAllTagsByUserId($this->getUser()->getId()); | |
247 | break; | |
248 | ||
249 | case 'entries': | |
250 | // SQLite doesn't care about cascading remove, so we need to manually remove associated stuf | |
251 | // otherwise they won't be removed ... | |
252 | if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { | |
253 | $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); | |
254 | } | |
255 | ||
256 | // manually remove tags first to avoid orphan tag | |
257 | $this->removeAllTagsByUserId($this->getUser()->getId()); | |
258 | ||
259 | $this->getDoctrine() | |
260 | ->getRepository('WallabagCoreBundle:Entry') | |
261 | ->removeAllByUserId($this->getUser()->getId()); | |
262 | } | |
263 | ||
264 | $this->get('session')->getFlashBag()->add( | |
265 | 'notice', | |
266 | 'flashes.config.notice.'.$type.'_reset' | |
267 | ); | |
268 | ||
269 | return $this->redirect($this->generateUrl('config').'#set3'); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Remove all tags for a given user and cleanup orphan tags | |
274 | * | |
275 | * @param int $userId | |
276 | */ | |
277 | private function removeAllTagsByUserId($userId) | |
278 | { | |
279 | $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findAllTags($userId); | |
280 | ||
281 | if (empty($tags)) { | |
282 | return; | |
283 | } | |
284 | ||
285 | $this->getDoctrine() | |
286 | ->getRepository('WallabagCoreBundle:Entry') | |
287 | ->removeTags($userId, $tags); | |
288 | ||
289 | $em = $this->getDoctrine()->getManager(); | |
290 | ||
291 | foreach ($tags as $tag) { | |
292 | if (count($tag->getEntries()) === 0) { | |
293 | $em->remove($tag); | |
294 | } | |
295 | } | |
296 | ||
297 | $em->flush(); | |
298 | } | |
299 | ||
300 | /** | |
301 | * Validate that a rule can be edited/deleted by the current user. | |
302 | * | |
303 | * @param TaggingRule $rule | |
304 | */ | |
305 | private function validateRuleAction(TaggingRule $rule) | |
306 | { | |
307 | if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) { | |
308 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); | |
309 | } | |
310 | } | |
311 | ||
312 | /** | |
313 | * Retrieve config for the current user. | |
314 | * If no config were found, create a new one. | |
315 | * | |
316 | * @return Config | |
317 | */ | |
318 | private function getConfig() | |
319 | { | |
320 | $config = $this->getDoctrine() | |
321 | ->getRepository('WallabagCoreBundle:Config') | |
322 | ->findOneByUser($this->getUser()); | |
323 | ||
324 | // should NEVER HAPPEN ... | |
325 | if (!$config) { | |
326 | $config = new Config($this->getUser()); | |
327 | } | |
328 | ||
329 | return $config; | |
330 | } | |
331 | ||
332 | /** | |
333 | * Delete account for current user. | |
334 | * | |
335 | * @Route("/account/delete", name="delete_account") | |
336 | * | |
337 | * @param Request $request | |
338 | * | |
339 | * @throws AccessDeniedHttpException | |
340 | * | |
341 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
342 | */ | |
343 | public function deleteAccountAction(Request $request) | |
344 | { | |
345 | $enabledUsers = $this->getDoctrine() | |
346 | ->getRepository('WallabagUserBundle:User') | |
347 | ->getSumEnabledUsers(); | |
348 | ||
349 | if ($enabledUsers <= 1) { | |
350 | throw new AccessDeniedHttpException(); | |
351 | } | |
352 | ||
353 | $user = $this->getUser(); | |
354 | ||
355 | // logout current user | |
356 | $this->get('security.token_storage')->setToken(null); | |
357 | $request->getSession()->invalidate(); | |
358 | ||
359 | $em = $this->get('fos_user.user_manager'); | |
360 | $em->deleteUser($user); | |
361 | ||
362 | return $this->redirect($this->generateUrl('fos_user_security_login')); | |
363 | } | |
364 | } |