]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ConfigController.php
Merge pull request #1915 from wallabag/doc-links
[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;
4d85d7e9 10use Wallabag\CoreBundle\Entity\Config;
f19f9f62 11use Wallabag\CoreBundle\Entity\TaggingRule;
5c895a7f 12use Wallabag\CoreBundle\Form\Type\ConfigType;
d9085c63 13use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
e4977b8a 14use Wallabag\CoreBundle\Form\Type\NewUserType;
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)
5c895a7f 35 $configForm = $this->createForm(ConfigType::class, $config, array('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
5c895a7f 55 $pwdForm = $this->createForm(ChangePasswordType::class, null, array('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
5c895a7f 74 $userForm = $this->createForm(UserInformationType::class, $user, array(
33fe61f9
JB
75 'validation_groups' => array('Profile'),
76 'action' => $this->generateUrl('config').'#set3',
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
5c895a7f 92 $rssForm = $this->createForm(RssType::class, $config, array('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();
5c895a7f 109 $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, array('action' => $this->generateUrl('config').'#set5'));
f19f9f62
KG
110 $newTaggingRule->handleRequest($request);
111
112 if ($newTaggingRule->isValid()) {
113 $taggingRule->setConfig($config);
114 $em->persist($taggingRule);
115 $em->flush();
116
117 $this->get('session')->getFlashBag()->add(
118 'notice',
4204a06b 119 'flashes.config.notice.tagging_rules_updated'
f19f9f62
KG
120 );
121
c7a4f74f 122 return $this->redirect($this->generateUrl('config').'#set5');
f19f9f62
KG
123 }
124
e4977b8a 125 // handle adding new user
fcb1fba5 126 $newUser = $userManager->createUser();
0f30f48b
JB
127 // enable created user by default
128 $newUser->setEnabled(true);
5c895a7f 129 $newUserForm = $this->createForm(NewUserType::class, $newUser, array(
33fe61f9 130 'validation_groups' => array('Profile'),
e56983af 131 'action' => $this->generateUrl('config').'#set6',
33fe61f9 132 ));
e4977b8a
J
133 $newUserForm->handleRequest($request);
134
fcb1fba5
NL
135 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
136 $userManager->updateUser($newUser, true);
0bd2cb1e
J
137
138 $config = new Config($newUser);
67c99849
JB
139 $config->setTheme($this->getParameter('wallabag_core.theme'));
140 $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page'));
141 $config->setRssLimit($this->getParameter('wallabag_core.rss_limit'));
142 $config->setLanguage($this->getParameter('wallabag_core.language'));
0bd2cb1e
J
143
144 $em->persist($config);
145
e4977b8a
J
146 $em->flush();
147
148 $this->get('session')->getFlashBag()->add(
149 'notice',
4204a06b 150 $this->get('translator')->trans('flashes.config.notice.user_added', array('%username%' => $newUser->getUsername()))
e4977b8a
J
151 );
152
c7a4f74f 153 return $this->redirect($this->generateUrl('config').'#set6');
e4977b8a
J
154 }
155
4d85d7e9 156 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
0c83fd59
J
157 'form' => array(
158 'config' => $configForm->createView(),
159 'rss' => $rssForm->createView(),
160 'pwd' => $pwdForm->createView(),
161 'user' => $userForm->createView(),
162 'new_user' => $newUserForm->createView(),
f19f9f62 163 'new_tagging_rule' => $newTaggingRule->createView(),
0c83fd59
J
164 ),
165 'rss' => array(
166 'username' => $user->getUsername(),
167 'token' => $config->getRssToken(),
9744e971 168 ),
63e40f2d 169 'twofactor_auth' => $this->getParameter('twofactor_auth'),
4d85d7e9
J
170 ));
171 }
172
0c83fd59
J
173 /**
174 * @param Request $request
175 *
176 * @Route("/generate-token", name="generate_token")
177 *
98568055 178 * @return RedirectResponse|JsonResponse
0c83fd59
J
179 */
180 public function generateTokenAction(Request $request)
181 {
182 $config = $this->getConfig();
183 $config->setRssToken(Utils::generateToken());
184
185 $em = $this->getDoctrine()->getManager();
186 $em->persist($config);
187 $em->flush();
188
189 if ($request->isXmlHttpRequest()) {
190 return new JsonResponse(array('token' => $config->getRssToken()));
191 }
192
c7a4f74f
JB
193 $this->get('session')->getFlashBag()->add(
194 'notice',
4204a06b 195 'flashes.config.notice.rss_token_updated'
c7a4f74f
JB
196 );
197
198 return $this->redirect($this->generateUrl('config').'#set2');
0c83fd59
J
199 }
200
52e423f3
KG
201 /**
202 * Deletes a tagging rule and redirect to the config homepage.
203 *
204 * @param TaggingRule $rule
205 *
206 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
207 *
98568055 208 * @return RedirectResponse
52e423f3 209 */
5f8a7857 210 public function deleteTaggingRuleAction(TaggingRule $rule)
52e423f3
KG
211 {
212 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
4f9cf232 213 throw $this->createAccessDeniedException('You can not access this tagging rule.');
52e423f3
KG
214 }
215
216 $em = $this->getDoctrine()->getManager();
217 $em->remove($rule);
218 $em->flush();
219
220 $this->get('session')->getFlashBag()->add(
221 'notice',
4204a06b 222 'flashes.config.notice.tagging_rules_deleted'
52e423f3
KG
223 );
224
c7a4f74f 225 return $this->redirect($this->generateUrl('config').'#set5');
52e423f3
KG
226 }
227
d9085c63
J
228 /**
229 * Retrieve config for the current user.
230 * If no config were found, create a new one.
231 *
232 * @return Wallabag\CoreBundle\Entity\Config
233 */
4d85d7e9
J
234 private function getConfig()
235 {
236 $config = $this->getDoctrine()
237 ->getRepository('WallabagCoreBundle:Config')
238 ->findOneByUser($this->getUser());
239
240 if (!$config) {
241 $config = new Config($this->getUser());
242 }
243
244 return $config;
245 }
246}