]>
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 Wallabag\CoreBundle\Entity\Config; | |
11 | use Wallabag\CoreBundle\Entity\TaggingRule; | |
12 | use Wallabag\CoreBundle\Form\Type\ConfigType; | |
13 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; | |
14 | use Wallabag\CoreBundle\Form\Type\RssType; | |
15 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; | |
16 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | |
17 | use Wallabag\CoreBundle\Tools\Utils; | |
18 | ||
19 | class ConfigController extends Controller | |
20 | { | |
21 | /** | |
22 | * @param Request $request | |
23 | * | |
24 | * @Route("/config", name="config") | |
25 | */ | |
26 | public function indexAction(Request $request) | |
27 | { | |
28 | $em = $this->getDoctrine()->getManager(); | |
29 | $config = $this->getConfig(); | |
30 | $userManager = $this->container->get('fos_user.user_manager'); | |
31 | $user = $this->getUser(); | |
32 | ||
33 | // handle basic config detail (this form is defined as a service) | |
34 | $configForm = $this->createForm(ConfigType::class, $config, ['action' => $this->generateUrl('config')]); | |
35 | $configForm->handleRequest($request); | |
36 | ||
37 | if ($configForm->isValid()) { | |
38 | $em->persist($config); | |
39 | $em->flush(); | |
40 | ||
41 | // switch active theme | |
42 | $activeTheme = $this->get('liip_theme.active_theme'); | |
43 | $activeTheme->setName($config->getTheme()); | |
44 | ||
45 | $this->get('session')->getFlashBag()->add( | |
46 | 'notice', | |
47 | 'flashes.config.notice.config_saved' | |
48 | ); | |
49 | ||
50 | return $this->redirect($this->generateUrl('config')); | |
51 | } | |
52 | ||
53 | // handle changing password | |
54 | $pwdForm = $this->createForm(ChangePasswordType::class, null, ['action' => $this->generateUrl('config').'#set4']); | |
55 | $pwdForm->handleRequest($request); | |
56 | ||
57 | if ($pwdForm->isValid()) { | |
58 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { | |
59 | $message = 'flashes.config.notice.password_not_updated_demo'; | |
60 | } else { | |
61 | $message = 'flashes.config.notice.password_updated'; | |
62 | ||
63 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); | |
64 | $userManager->updateUser($user, true); | |
65 | } | |
66 | ||
67 | $this->get('session')->getFlashBag()->add('notice', $message); | |
68 | ||
69 | return $this->redirect($this->generateUrl('config').'#set4'); | |
70 | } | |
71 | ||
72 | // handle changing user information | |
73 | $userForm = $this->createForm(UserInformationType::class, $user, [ | |
74 | 'validation_groups' => ['Profile'], | |
75 | 'action' => $this->generateUrl('config').'#set3', | |
76 | ]); | |
77 | $userForm->handleRequest($request); | |
78 | ||
79 | if ($userForm->isValid()) { | |
80 | $userManager->updateUser($user, true); | |
81 | ||
82 | $this->get('session')->getFlashBag()->add( | |
83 | 'notice', | |
84 | 'flashes.config.notice.user_updated' | |
85 | ); | |
86 | ||
87 | return $this->redirect($this->generateUrl('config').'#set3'); | |
88 | } | |
89 | ||
90 | // handle rss information | |
91 | $rssForm = $this->createForm(RssType::class, $config, ['action' => $this->generateUrl('config').'#set2']); | |
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', | |
100 | 'flashes.config.notice.rss_updated' | |
101 | ); | |
102 | ||
103 | return $this->redirect($this->generateUrl('config').'#set2'); | |
104 | } | |
105 | ||
106 | // handle tagging rule | |
107 | $taggingRule = new TaggingRule(); | |
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]); | |
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', | |
132 | 'flashes.config.notice.tagging_rules_updated' | |
133 | ); | |
134 | ||
135 | return $this->redirect($this->generateUrl('config').'#set5'); | |
136 | } | |
137 | ||
138 | return $this->render('WallabagCoreBundle:Config:index.html.twig', [ | |
139 | 'form' => [ | |
140 | 'config' => $configForm->createView(), | |
141 | 'rss' => $rssForm->createView(), | |
142 | 'pwd' => $pwdForm->createView(), | |
143 | 'user' => $userForm->createView(), | |
144 | 'new_tagging_rule' => $newTaggingRule->createView(), | |
145 | ], | |
146 | 'rss' => [ | |
147 | 'username' => $user->getUsername(), | |
148 | 'token' => $config->getRssToken(), | |
149 | ], | |
150 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
151 | ]); | |
152 | } | |
153 | ||
154 | /** | |
155 | * @param Request $request | |
156 | * | |
157 | * @Route("/generate-token", name="generate_token") | |
158 | * | |
159 | * @return RedirectResponse|JsonResponse | |
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()) { | |
171 | return new JsonResponse(['token' => $config->getRssToken()]); | |
172 | } | |
173 | ||
174 | $this->get('session')->getFlashBag()->add( | |
175 | 'notice', | |
176 | 'flashes.config.notice.rss_token_updated' | |
177 | ); | |
178 | ||
179 | return $this->redirect($this->generateUrl('config').'#set2'); | |
180 | } | |
181 | ||
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 | * | |
189 | * @return RedirectResponse | |
190 | */ | |
191 | public function deleteTaggingRuleAction(TaggingRule $rule) | |
192 | { | |
193 | $this->validateRuleAction($rule); | |
194 | ||
195 | $em = $this->getDoctrine()->getManager(); | |
196 | $em->remove($rule); | |
197 | $em->flush(); | |
198 | ||
199 | $this->get('session')->getFlashBag()->add( | |
200 | 'notice', | |
201 | 'flashes.config.notice.tagging_rules_deleted' | |
202 | ); | |
203 | ||
204 | return $this->redirect($this->generateUrl('config').'#set5'); | |
205 | } | |
206 | ||
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) | |
217 | { | |
218 | $this->validateRuleAction($rule); | |
219 | ||
220 | return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5'); | |
221 | } | |
222 | ||
223 | /** | |
224 | * Validate that a rule can be edited/deleted by the current user. | |
225 | * | |
226 | * @param TaggingRule $rule | |
227 | */ | |
228 | private function validateRuleAction(TaggingRule $rule) | |
229 | { | |
230 | if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) { | |
231 | throw $this->createAccessDeniedException('You can not access this tagging rule.'); | |
232 | } | |
233 | } | |
234 | ||
235 | /** | |
236 | * Retrieve config for the current user. | |
237 | * If no config were found, create a new one. | |
238 | * | |
239 | * @return Config | |
240 | */ | |
241 | private function getConfig() | |
242 | { | |
243 | $config = $this->getDoctrine() | |
244 | ->getRepository('WallabagCoreBundle:Config') | |
245 | ->findOneByUser($this->getUser()); | |
246 | ||
247 | // should NEVER HAPPEN ... | |
248 | if (!$config) { | |
249 | $config = new Config($this->getUser()); | |
250 | } | |
251 | ||
252 | return $config; | |
253 | } | |
254 | } |