]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ConfigController.php
Merge pull request #2499 from wallabag/add-relation-client-user
[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
d9085c63 38 if ($configForm->isValid()) {
4d85d7e9
J
39 $em->persist($config);
40 $em->flush();
41
32da2a70
J
42 // switch active theme
43 $activeTheme = $this->get('liip_theme.active_theme');
44 $activeTheme->setName($config->getTheme());
45
4d85d7e9
J
46 $this->get('session')->getFlashBag()->add(
47 'notice',
4204a06b 48 'flashes.config.notice.config_saved'
4d85d7e9
J
49 );
50
51 return $this->redirect($this->generateUrl('config'));
52 }
53
d9085c63 54 // handle changing password
4094ea47 55 $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']);
d9085c63
J
56 $pwdForm->handleRequest($request);
57
58 if ($pwdForm->isValid()) {
a4f42c59 59 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
4204a06b 60 $message = 'flashes.config.notice.password_not_updated_demo';
6c9f50a6 61 } else {
4204a06b 62 $message = 'flashes.config.notice.password_updated';
b6c00b0b 63
d8d56448
NL
64 $user->setPlainPassword($pwdForm->get('new_password')->getData());
65 $userManager->updateUser($user, true);
6c9f50a6 66 }
d9085c63 67
b6c00b0b
JB
68 $this->get('session')->getFlashBag()->add('notice', $message);
69
c7a4f74f 70 return $this->redirect($this->generateUrl('config').'#set4');
d9085c63
J
71 }
72
c0d9eba0 73 // handle changing user information
4094ea47
JB
74 $userForm = $this->createForm(UserInformationType::class, $user, [
75 'validation_groups' => ['Profile'],
33fe61f9 76 'action' => $this->generateUrl('config').'#set3',
4094ea47 77 ]);
c0d9eba0
J
78 $userForm->handleRequest($request);
79
80 if ($userForm->isValid()) {
fcb1fba5 81 $userManager->updateUser($user, true);
c0d9eba0
J
82
83 $this->get('session')->getFlashBag()->add(
84 'notice',
4204a06b 85 'flashes.config.notice.user_updated'
c0d9eba0
J
86 );
87
c7a4f74f 88 return $this->redirect($this->generateUrl('config').'#set3');
c0d9eba0
J
89 }
90
0c83fd59 91 // handle rss information
4094ea47 92 $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']);
0c83fd59
J
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',
4204a06b 101 'flashes.config.notice.rss_updated'
0c83fd59
J
102 );
103
c7a4f74f 104 return $this->redirect($this->generateUrl('config').'#set2');
0c83fd59
J
105 }
106
f19f9f62
KG
107 // handle tagging rule
108 $taggingRule = new TaggingRule();
bf3dc999
JB
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]);
f19f9f62
KG
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',
4204a06b 133 'flashes.config.notice.tagging_rules_updated'
f19f9f62
KG
134 );
135
c7a4f74f 136 return $this->redirect($this->generateUrl('config').'#set5');
f19f9f62
KG
137 }
138
4094ea47
JB
139 return $this->render('WallabagCoreBundle:Config:index.html.twig', [
140 'form' => [
0c83fd59
J
141 'config' => $configForm->createView(),
142 'rss' => $rssForm->createView(),
143 'pwd' => $pwdForm->createView(),
144 'user' => $userForm->createView(),
f19f9f62 145 'new_tagging_rule' => $newTaggingRule->createView(),
4094ea47
JB
146 ],
147 'rss' => [
0c83fd59
J
148 'username' => $user->getUsername(),
149 'token' => $config->getRssToken(),
4094ea47 150 ],
63e40f2d 151 'twofactor_auth' => $this->getParameter('twofactor_auth'),
bb0c78f4
NL
152 'enabled_users' => $this->getDoctrine()
153 ->getRepository('WallabagUserBundle:User')
154 ->getSumEnabledUsers(),
4094ea47 155 ]);
4d85d7e9
J
156 }
157
0c83fd59
J
158 /**
159 * @param Request $request
160 *
161 * @Route("/generate-token", name="generate_token")
162 *
98568055 163 * @return RedirectResponse|JsonResponse
0c83fd59
J
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()) {
4094ea47 175 return new JsonResponse(['token' => $config->getRssToken()]);
0c83fd59
J
176 }
177
c7a4f74f
JB
178 $this->get('session')->getFlashBag()->add(
179 'notice',
4204a06b 180 'flashes.config.notice.rss_token_updated'
c7a4f74f
JB
181 );
182
183 return $this->redirect($this->generateUrl('config').'#set2');
0c83fd59
J
184 }
185
52e423f3
KG
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 *
98568055 193 * @return RedirectResponse
52e423f3 194 */
5f8a7857 195 public function deleteTaggingRuleAction(TaggingRule $rule)
52e423f3 196 {
8799bde0 197 $this->validateRuleAction($rule);
52e423f3
KG
198
199 $em = $this->getDoctrine()->getManager();
200 $em->remove($rule);
201 $em->flush();
202
203 $this->get('session')->getFlashBag()->add(
204 'notice',
4204a06b 205 'flashes.config.notice.tagging_rules_deleted'
52e423f3
KG
206 );
207
c7a4f74f 208 return $this->redirect($this->generateUrl('config').'#set5');
52e423f3
KG
209 }
210
bf3dc999
JB
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)
8799bde0
JB
221 {
222 $this->validateRuleAction($rule);
223
224 return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5');
225 }
226
206bade5
JB
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':
191564b7
JB
240 $this->getDoctrine()
241 ->getRepository('WallabagAnnotationBundle:Annotation')
242 ->removeAllByUserId($this->getUser()->getId());
206bade5
JB
243 break;
244
245 case 'tags':
191564b7
JB
246 $this->removeAllTagsByUserId($this->getUser()->getId());
247 break;
206bade5 248
191564b7
JB
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());
206bade5
JB
254 }
255
8c61fd12 256 // manually remove tags to avoid orphan tag
f71e55ac
JB
257 $this->removeAllTagsByUserId($this->getUser()->getId());
258
206bade5
JB
259 $this->getDoctrine()
260 ->getRepository('WallabagCoreBundle:Entry')
191564b7 261 ->removeAllByUserId($this->getUser()->getId());
206bade5
JB
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
191564b7 272 /**
8c61fd12 273 * Remove all tags for a given user and cleanup orphan tags.
191564b7 274 *
8c61fd12 275 * @param int $userId
191564b7
JB
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);
f71e55ac 288
8c61fd12 289 // cleanup orphan tags
f71e55ac
JB
290 $em = $this->getDoctrine()->getManager();
291
292 foreach ($tags as $tag) {
293 if (count($tag->getEntries()) === 0) {
294 $em->remove($tag);
295 }
296 }
297
298 $em->flush();
191564b7
JB
299 }
300
8799bde0 301 /**
2455472e 302 * Validate that a rule can be edited/deleted by the current user.
8799bde0 303 *
2455472e 304 * @param TaggingRule $rule
8799bde0
JB
305 */
306 private function validateRuleAction(TaggingRule $rule)
bf3dc999
JB
307 {
308 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
309 throw $this->createAccessDeniedException('You can not access this tagging rule.');
310 }
bf3dc999
JB
311 }
312
d9085c63
J
313 /**
314 * Retrieve config for the current user.
315 * If no config were found, create a new one.
316 *
4094ea47 317 * @return Config
d9085c63 318 */
4d85d7e9
J
319 private function getConfig()
320 {
321 $config = $this->getDoctrine()
322 ->getRepository('WallabagCoreBundle:Config')
323 ->findOneByUser($this->getUser());
324
ca17abce 325 // should NEVER HAPPEN ...
4d85d7e9
J
326 if (!$config) {
327 $config = new Config($this->getUser());
328 }
329
330 return $config;
331 }
e4b46f77 332
7ac3e575
JB
333 /**
334 * Delete account for current user.
335 *
336 * @Route("/account/delete", name="delete_account")
337 *
338 * @param Request $request
339 *
340 * @throws AccessDeniedHttpException
341 *
342 * @return \Symfony\Component\HttpFoundation\RedirectResponse
343 */
eed812af
JB
344 public function deleteAccountAction(Request $request)
345 {
346 $enabledUsers = $this->getDoctrine()
347 ->getRepository('WallabagUserBundle:User')
348 ->getSumEnabledUsers();
349
350 if ($enabledUsers <= 1) {
351 throw new AccessDeniedHttpException();
352 }
353
354 $user = $this->getUser();
355
356 // logout current user
357 $this->get('security.token_storage')->setToken(null);
358 $request->getSession()->invalidate();
359
360 $em = $this->get('fos_user.user_manager');
361 $em->deleteUser($user);
362
363 return $this->redirect($this->generateUrl('fos_user_security_login'));
364 }
4d85d7e9 365}