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