]>
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\NewUserType; | |
15 | use Wallabag\CoreBundle\Form\Type\RssType; | |
16 | use Wallabag\CoreBundle\Form\Type\TaggingRuleType; | |
17 | use Wallabag\CoreBundle\Form\Type\UserInformationType; | |
18 | use Wallabag\CoreBundle\Tools\Utils; | |
19 | use Wallabag\UserBundle\Entity\User; | |
20 | ||
21 | class ConfigController extends Controller | |
22 | { | |
23 | /** | |
24 | * @param Request $request | |
25 | * | |
26 | * @Route("/config", name="config") | |
27 | */ | |
28 | public function indexAction(Request $request) | |
29 | { | |
30 | $em = $this->getDoctrine()->getManager(); | |
31 | $config = $this->getConfig(); | |
32 | $userManager = $this->container->get('fos_user.user_manager'); | |
33 | $user = $this->getUser(); | |
34 | ||
35 | // handle basic config detail (this form is defined as a service) | |
36 | $configForm = $this->createForm(ConfigType::class, $config, array('action' => $this->generateUrl('config'))); | |
37 | $configForm->handleRequest($request); | |
38 | ||
39 | if ($configForm->isValid()) { | |
40 | $em->persist($config); | |
41 | $em->flush(); | |
42 | ||
43 | // switch active theme | |
44 | $activeTheme = $this->get('liip_theme.active_theme'); | |
45 | $activeTheme->setName($config->getTheme()); | |
46 | ||
47 | $this->get('session')->getFlashBag()->add( | |
48 | 'notice', | |
49 | 'Config saved. Some parameters will be considered after disconnection.' | |
50 | ); | |
51 | ||
52 | return $this->redirect($this->generateUrl('config')); | |
53 | } | |
54 | ||
55 | // handle changing password | |
56 | $pwdForm = $this->createForm(ChangePasswordType::class, null, array('action' => $this->generateUrl('config').'#set4')); | |
57 | $pwdForm->handleRequest($request); | |
58 | ||
59 | if ($pwdForm->isValid()) { | |
60 | if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) { | |
61 | $this->get('session')->getFlashBag()->add( | |
62 | 'notice', | |
63 | 'In demonstration mode, you can\'t change password for this user.' | |
64 | ); | |
65 | } else { | |
66 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); | |
67 | $userManager->updateUser($user, true); | |
68 | ||
69 | $this->get('session')->getFlashBag()->add( | |
70 | 'notice', | |
71 | 'Password updated' | |
72 | ); | |
73 | } | |
74 | ||
75 | return $this->redirect($this->generateUrl('config').'#set4'); | |
76 | } | |
77 | ||
78 | // handle changing user information | |
79 | $userForm = $this->createForm(UserInformationType::class, $user, array( | |
80 | 'validation_groups' => array('Profile'), | |
81 | 'action' => $this->generateUrl('config').'#set3', | |
82 | )); | |
83 | $userForm->handleRequest($request); | |
84 | ||
85 | if ($userForm->isValid()) { | |
86 | $userManager->updateUser($user, true); | |
87 | ||
88 | $this->get('session')->getFlashBag()->add( | |
89 | 'notice', | |
90 | 'Information updated' | |
91 | ); | |
92 | ||
93 | return $this->redirect($this->generateUrl('config').'#set3'); | |
94 | } | |
95 | ||
96 | // handle rss information | |
97 | $rssForm = $this->createForm(RssType::class, $config, array('action' => $this->generateUrl('config').'#set2')); | |
98 | $rssForm->handleRequest($request); | |
99 | ||
100 | if ($rssForm->isValid()) { | |
101 | $em->persist($config); | |
102 | $em->flush(); | |
103 | ||
104 | $this->get('session')->getFlashBag()->add( | |
105 | 'notice', | |
106 | 'RSS information updated' | |
107 | ); | |
108 | ||
109 | return $this->redirect($this->generateUrl('config').'#set2'); | |
110 | } | |
111 | ||
112 | // handle tagging rule | |
113 | $taggingRule = new TaggingRule(); | |
114 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, array('action' => $this->generateUrl('config').'#set5')); | |
115 | $newTaggingRule->handleRequest($request); | |
116 | ||
117 | if ($newTaggingRule->isValid()) { | |
118 | $taggingRule->setConfig($config); | |
119 | $em->persist($taggingRule); | |
120 | $em->flush(); | |
121 | ||
122 | $this->get('session')->getFlashBag()->add( | |
123 | 'notice', | |
124 | 'Tagging rules updated' | |
125 | ); | |
126 | ||
127 | return $this->redirect($this->generateUrl('config').'#set5'); | |
128 | } | |
129 | ||
130 | // handle adding new user | |
131 | $newUser = $userManager->createUser(); | |
132 | // enable created user by default | |
133 | $newUser->setEnabled(true); | |
134 | $newUserForm = $this->createForm(NewUserType::class, $newUser, array( | |
135 | 'validation_groups' => array('Profile'), | |
136 | 'action' => $this->generateUrl('config').'#set6', | |
137 | )); | |
138 | $newUserForm->handleRequest($request); | |
139 | ||
140 | if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { | |
141 | $userManager->updateUser($newUser, true); | |
142 | ||
143 | $config = new Config($newUser); | |
144 | $config->setTheme($this->getParameter('wallabag_core.theme')); | |
145 | $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page')); | |
146 | $config->setRssLimit($this->getParameter('wallabag_core.rss_limit')); | |
147 | $config->setLanguage($this->getParameter('wallabag_core.language')); | |
148 | ||
149 | $em->persist($config); | |
150 | ||
151 | $em->flush(); | |
152 | ||
153 | $this->get('session')->getFlashBag()->add( | |
154 | 'notice', | |
155 | $this->get('translator')->trans('User "%username%" added', array('%username%' => $newUser->getUsername())) | |
156 | ); | |
157 | ||
158 | return $this->redirect($this->generateUrl('config').'#set6'); | |
159 | } | |
160 | ||
161 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( | |
162 | 'form' => array( | |
163 | 'config' => $configForm->createView(), | |
164 | 'rss' => $rssForm->createView(), | |
165 | 'pwd' => $pwdForm->createView(), | |
166 | 'user' => $userForm->createView(), | |
167 | 'new_user' => $newUserForm->createView(), | |
168 | 'new_tagging_rule' => $newTaggingRule->createView(), | |
169 | ), | |
170 | 'rss' => array( | |
171 | 'username' => $user->getUsername(), | |
172 | 'token' => $config->getRssToken(), | |
173 | ), | |
174 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
175 | )); | |
176 | } | |
177 | ||
178 | /** | |
179 | * @param Request $request | |
180 | * | |
181 | * @Route("/generate-token", name="generate_token") | |
182 | * | |
183 | * @return RedirectResponse|JsonResponse | |
184 | */ | |
185 | public function generateTokenAction(Request $request) | |
186 | { | |
187 | $config = $this->getConfig(); | |
188 | $config->setRssToken(Utils::generateToken()); | |
189 | ||
190 | $em = $this->getDoctrine()->getManager(); | |
191 | $em->persist($config); | |
192 | $em->flush(); | |
193 | ||
194 | if ($request->isXmlHttpRequest()) { | |
195 | return new JsonResponse(array('token' => $config->getRssToken())); | |
196 | } | |
197 | ||
198 | $this->get('session')->getFlashBag()->add( | |
199 | 'notice', | |
200 | 'RSS token updated' | |
201 | ); | |
202 | ||
203 | return $this->redirect($this->generateUrl('config').'#set2'); | |
204 | } | |
205 | ||
206 | /** | |
207 | * Deletes a tagging rule and redirect to the config homepage. | |
208 | * | |
209 | * @param TaggingRule $rule | |
210 | * | |
211 | * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule") | |
212 | * | |
213 | * @return RedirectResponse | |
214 | */ | |
215 | public function deleteTaggingRuleAction(TaggingRule $rule) | |
216 | { | |
217 | if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) { | |
218 | throw $this->createAccessDeniedException('You can not access this tagging ryle.'); | |
219 | } | |
220 | ||
221 | $em = $this->getDoctrine()->getManager(); | |
222 | $em->remove($rule); | |
223 | $em->flush(); | |
224 | ||
225 | $this->get('session')->getFlashBag()->add( | |
226 | 'notice', | |
227 | 'Tagging rule deleted' | |
228 | ); | |
229 | ||
230 | return $this->redirect($this->generateUrl('config').'#set5'); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Retrieve config for the current user. | |
235 | * If no config were found, create a new one. | |
236 | * | |
237 | * @return Wallabag\CoreBundle\Entity\Config | |
238 | */ | |
239 | private function getConfig() | |
240 | { | |
241 | $config = $this->getDoctrine() | |
242 | ->getRepository('WallabagCoreBundle:Config') | |
243 | ->findOneByUser($this->getUser()); | |
244 | ||
245 | if (!$config) { | |
246 | $config = new Config($this->getUser()); | |
247 | } | |
248 | ||
249 | return $config; | |
250 | } | |
251 | } |