]>
Commit | Line | Data |
---|---|---|
4d85d7e9 J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
0c83fd59 | 7 | use Symfony\Component\HttpFoundation\JsonResponse; |
98568055 | 8 | use Symfony\Component\HttpFoundation\RedirectResponse; |
619cc453 | 9 | use Symfony\Component\HttpFoundation\Request; |
4d85d7e9 | 10 | use Wallabag\CoreBundle\Entity\Config; |
f19f9f62 | 11 | use Wallabag\CoreBundle\Entity\TaggingRule; |
5c895a7f | 12 | use Wallabag\CoreBundle\Form\Type\ConfigType; |
d9085c63 | 13 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
0c83fd59 | 14 | use Wallabag\CoreBundle\Form\Type\RssType; |
619cc453 JB |
15 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; |
16 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | |
0c83fd59 | 17 | use Wallabag\CoreBundle\Tools\Utils; |
4d85d7e9 J |
18 | |
19 | class ConfigController extends Controller | |
20 | { | |
21 | /** | |
22 | * @param Request $request | |
23 | * | |
24 | * @Route("/config", name="config") | |
4d85d7e9 J |
25 | */ |
26 | public function indexAction(Request $request) | |
27 | { | |
d9085c63 | 28 | $em = $this->getDoctrine()->getManager(); |
4d85d7e9 | 29 | $config = $this->getConfig(); |
fcb1fba5 | 30 | $userManager = $this->container->get('fos_user.user_manager'); |
c0d9eba0 | 31 | $user = $this->getUser(); |
4d85d7e9 | 32 | |
32da2a70 | 33 | // handle basic config detail (this form is defined as a service) |
4094ea47 | 34 | $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]); |
d9085c63 | 35 | $configForm->handleRequest($request); |
4d85d7e9 | 36 | |
d9085c63 | 37 | if ($configForm->isValid()) { |
4d85d7e9 J |
38 | $em->persist($config); |
39 | $em->flush(); | |
40 | ||
ece4718f NL |
41 | $request->getSession()->set('_locale', $config->getLanguage()); |
42 | ||
32da2a70 J |
43 | // switch active theme |
44 | $activeTheme = $this->get('liip_theme.active_theme'); | |
45 | $activeTheme->setName($config->getTheme()); | |
46 | ||
4d85d7e9 J |
47 | $this->get('session')->getFlashBag()->add( |
48 | 'notice', | |
4204a06b | 49 | 'flashes.config.notice.config_saved' |
4d85d7e9 J |
50 | ); |
51 | ||
52 | return $this->redirect($this->generateUrl('config')); | |
53 | } | |
54 | ||
d9085c63 | 55 | // handle changing password |
4094ea47 | 56 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']); |
d9085c63 J |
57 | $pwdForm->handleRequest($request); |
58 | ||
59 | if ($pwdForm->isValid()) { | |
a4f42c59 | 60 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { |
4204a06b | 61 | $message = 'flashes.config.notice.password_not_updated_demo'; |
6c9f50a6 | 62 | } else { |
4204a06b | 63 | $message = 'flashes.config.notice.password_updated'; |
b6c00b0b | 64 | |
d8d56448 NL |
65 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); |
66 | $userManager->updateUser($user, true); | |
6c9f50a6 | 67 | } |
d9085c63 | 68 | |
b6c00b0b JB |
69 | $this->get('session')->getFlashBag()->add('notice', $message); |
70 | ||
c7a4f74f | 71 | return $this->redirect($this->generateUrl('config').'#set4'); |
d9085c63 J |
72 | } |
73 | ||
c0d9eba0 | 74 | // handle changing user information |
4094ea47 JB |
75 | $userForm = $this->createForm(UserInformationType::class, $user, [ |
76 | 'validation_groups' => ['Profile'], | |
33fe61f9 | 77 | 'action' => $this->generateUrl('config').'#set3', |
4094ea47 | 78 | ]); |
c0d9eba0 J |
79 | $userForm->handleRequest($request); |
80 | ||
81 | if ($userForm->isValid()) { | |
fcb1fba5 | 82 | $userManager->updateUser($user, true); |
c0d9eba0 J |
83 | |
84 | $this->get('session')->getFlashBag()->add( | |
85 | 'notice', | |
4204a06b | 86 | 'flashes.config.notice.user_updated' |
c0d9eba0 J |
87 | ); |
88 | ||
c7a4f74f | 89 | return $this->redirect($this->generateUrl('config').'#set3'); |
c0d9eba0 J |
90 | } |
91 | ||
0c83fd59 | 92 | // handle rss information |
4094ea47 | 93 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']); |
0c83fd59 J |
94 | $rssForm->handleRequest($request); |
95 | ||
96 | if ($rssForm->isValid()) { | |
97 | $em->persist($config); | |
98 | $em->flush(); | |
99 | ||
100 | $this->get('session')->getFlashBag()->add( | |
101 | 'notice', | |
4204a06b | 102 | 'flashes.config.notice.rss_updated' |
0c83fd59 J |
103 | ); |
104 | ||
c7a4f74f | 105 | return $this->redirect($this->generateUrl('config').'#set2'); |
0c83fd59 J |
106 | } |
107 | ||
f19f9f62 KG |
108 | // handle tagging rule |
109 | $taggingRule = new TaggingRule(); | |
bf3dc999 JB |
110 | $action = $this->generateUrl('config').'#set5'; |
111 | ||
112 | if ($request->query->has('tagging-rule')) { | |
113 | $taggingRule = $this->getDoctrine() | |
114 | ->getRepository('WallabagCoreBundle:TaggingRule') | |
115 | ->find($request->query->get('tagging-rule')); | |
116 | ||
117 | if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) { | |
118 | return $this->redirect($action); | |
119 | } | |
120 | ||
121 | $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5'; | |
122 | } | |
123 | ||
124 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]); | |
f19f9f62 KG |
125 | $newTaggingRule->handleRequest($request); |
126 | ||
127 | if ($newTaggingRule->isValid()) { | |
128 | $taggingRule->setConfig($config); | |
129 | $em->persist($taggingRule); | |
130 | $em->flush(); | |
131 | ||
132 | $this->get('session')->getFlashBag()->add( | |
133 | 'notice', | |
4204a06b | 134 | 'flashes.config.notice.tagging_rules_updated' |
f19f9f62 KG |
135 | ); |
136 | ||
c7a4f74f | 137 | return $this->redirect($this->generateUrl('config').'#set5'); |
f19f9f62 KG |
138 | } |
139 | ||
4094ea47 JB |
140 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ |
141 | 'form' => [ | |
0c83fd59 J |
142 | 'config' => $configForm->createView(), |
143 | 'rss' => $rssForm->createView(), | |
144 | 'pwd' => $pwdForm->createView(), | |
145 | 'user' => $userForm->createView(), | |
f19f9f62 | 146 | 'new_tagging_rule' => $newTaggingRule->createView(), |
4094ea47 JB |
147 | ], |
148 | 'rss' => [ | |
0c83fd59 J |
149 | 'username' => $user->getUsername(), |
150 | 'token' => $config->getRssToken(), | |
4094ea47 | 151 | ], |
63e40f2d | 152 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), |
4094ea47 | 153 | ]); |
4d85d7e9 J |
154 | } |
155 | ||
0c83fd59 J |
156 | /** |
157 | * @param Request $request | |
158 | * | |
159 | * @Route("/generate-token", name="generate_token") | |
160 | * | |
98568055 | 161 | * @return RedirectResponse|JsonResponse |
0c83fd59 J |
162 | */ |
163 | public function generateTokenAction(Request $request) | |
164 | { | |
165 | $config = $this->getConfig(); | |
166 | $config->setRssToken(Utils::generateToken()); | |
167 | ||
168 | $em = $this->getDoctrine()->getManager(); | |
169 | $em->persist($config); | |
170 | $em->flush(); | |
171 | ||
172 | if ($request->isXmlHttpRequest()) { | |
4094ea47 | 173 | return new JsonResponse(['token' => $config->getRssToken()]); |
0c83fd59 J |
174 | } |
175 | ||
c7a4f74f JB |
176 | $this->get('session')->getFlashBag()->add( |
177 | 'notice', | |
4204a06b | 178 | 'flashes.config.notice.rss_token_updated' |
c7a4f74f JB |
179 | ); |
180 | ||
181 | return $this->redirect($this->generateUrl('config').'#set2'); | |
0c83fd59 J |
182 | } |
183 | ||
52e423f3 KG |
184 | /** |
185 | * Deletes a tagging rule and redirect to the config homepage. | |
186 | * | |
187 | * @param TaggingRule $rule | |
188 | * | |
189 | * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule") | |
190 | * | |
98568055 | 191 | * @return RedirectResponse |
52e423f3 | 192 | */ |
5f8a7857 | 193 | public function deleteTaggingRuleAction(TaggingRule $rule) |
52e423f3 | 194 | { |
8799bde0 | 195 | $this->validateRuleAction($rule); |
52e423f3 KG |
196 | |
197 | $em = $this->getDoctrine()->getManager(); | |
198 | $em->remove($rule); | |
199 | $em->flush(); | |
200 | ||
201 | $this->get('session')->getFlashBag()->add( | |
202 | 'notice', | |
4204a06b | 203 | 'flashes.config.notice.tagging_rules_deleted' |
52e423f3 KG |
204 | ); |
205 | ||
c7a4f74f | 206 | return $this->redirect($this->generateUrl('config').'#set5'); |
52e423f3 KG |
207 | } |
208 | ||
bf3dc999 JB |
209 | /** |
210 | * Edit a tagging rule. | |
211 | * | |
212 | * @param TaggingRule $rule | |
213 | * | |
214 | * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule") | |
215 | * | |
216 | * @return RedirectResponse | |
217 | */ | |
218 | public function editTaggingRuleAction(TaggingRule $rule) | |
8799bde0 JB |
219 | { |
220 | $this->validateRuleAction($rule); | |
221 | ||
222 | return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5'); | |
223 | } | |
224 | ||
225 | /** | |
2455472e | 226 | * Validate that a rule can be edited/deleted by the current user. |
8799bde0 | 227 | * |
2455472e | 228 | * @param TaggingRule $rule |
8799bde0 JB |
229 | */ |
230 | private function validateRuleAction(TaggingRule $rule) | |
bf3dc999 JB |
231 | { |
232 | if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) { | |
233 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); | |
234 | } | |
bf3dc999 JB |
235 | } |
236 | ||
d9085c63 J |
237 | /** |
238 | * Retrieve config for the current user. | |
239 | * If no config were found, create a new one. | |
240 | * | |
4094ea47 | 241 | * @return Config |
d9085c63 | 242 | */ |
4d85d7e9 J |
243 | private function getConfig() |
244 | { | |
245 | $config = $this->getDoctrine() | |
246 | ->getRepository('WallabagCoreBundle:Config') | |
247 | ->findOneByUser($this->getUser()); | |
248 | ||
ca17abce | 249 | // should NEVER HAPPEN ... |
4d85d7e9 J |
250 | if (!$config) { |
251 | $config = new Config($this->getUser()); | |
252 | } | |
253 | ||
254 | return $config; | |
255 | } | |
256 | } |