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